Added spotless plugin
This commit is contained in:
10
build.gradle
10
build.gradle
@@ -4,6 +4,7 @@ plugins {
|
||||
id 'java-gradle-plugin'
|
||||
id 'maven-publish'
|
||||
id 'application'
|
||||
id "com.diffplug.spotless" version "6.15.0"
|
||||
id 'com.gradle.plugin-publish' version '0.12.0'
|
||||
}
|
||||
|
||||
@@ -74,3 +75,12 @@ installDist {
|
||||
exclude 'gradle-api-6.6.1.jar'
|
||||
}
|
||||
|
||||
spotless {
|
||||
java {
|
||||
importOrder()
|
||||
removeUnusedImports()
|
||||
cleanthat()
|
||||
googleJavaFormat()
|
||||
formatAnnotations()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,6 @@ package gae.piaz.layer3gen;
|
||||
|
||||
import freemarker.template.TemplateException;
|
||||
import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.FieldAnnotationsScanner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URLClassLoader;
|
||||
@@ -14,8 +9,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.FieldAnnotationsScanner;
|
||||
|
||||
@Slf4j
|
||||
public class CodeGenerator {
|
||||
@@ -26,7 +24,8 @@ public class CodeGenerator {
|
||||
|
||||
private static URLClassLoader classLoader;
|
||||
|
||||
public static void run(CodeGeneratorConfig arg, URLClassLoader classLoader) throws IOException, TemplateException {
|
||||
public static void run(CodeGeneratorConfig arg, URLClassLoader classLoader)
|
||||
throws IOException, TemplateException {
|
||||
|
||||
CodeGenerator.config = arg;
|
||||
CodeGenerator.classLoader = classLoader;
|
||||
@@ -36,7 +35,6 @@ public class CodeGenerator {
|
||||
Set<Class<?>> entities = getEntityClasses();
|
||||
log.debug("found {} entities", entities.size());
|
||||
generateCode(entities);
|
||||
|
||||
}
|
||||
|
||||
private static void generateCode(Set<Class<?>> entities) throws IOException, TemplateException {
|
||||
@@ -64,17 +62,16 @@ public class CodeGenerator {
|
||||
createControllerDTO(entity);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Set<Class<?>> getEntityClasses() {
|
||||
Reflections reflections = new Reflections(config.getInputPackages().getJpaEntities(), classLoader);
|
||||
Reflections reflections =
|
||||
new Reflections(config.getInputPackages().getJpaEntities(), classLoader);
|
||||
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);
|
||||
classes.addAll(reflections.getTypesAnnotatedWith(jakarta.persistence.Entity.class));
|
||||
return classes;
|
||||
}
|
||||
|
||||
|
||||
private static void createControllerDTO(Class<?> entity) throws IOException, TemplateException {
|
||||
|
||||
CodeRenderer.RenderingData data = new CodeRenderer.RenderingData();
|
||||
@@ -84,12 +81,15 @@ public class CodeGenerator {
|
||||
data.setEntityPackage(entity.getPackageName());
|
||||
String code = CodeRenderer.render("controllerdto.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "ControllerDTO.java").toString();
|
||||
entity.getSimpleName() + "ControllerDTO.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createMapper(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -104,12 +104,15 @@ public class CodeGenerator {
|
||||
data.setEntityPackage(entity.getPackageName());
|
||||
|
||||
String code = CodeRenderer.render("mapper.ftl", data);
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getMappers().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "Mapper.java").toString();
|
||||
entity.getSimpleName() + "Mapper.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createDto(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -124,12 +127,15 @@ public class CodeGenerator {
|
||||
data.setEntityFields(Arrays.asList(entity.getDeclaredFields()));
|
||||
|
||||
String code = CodeRenderer.render("dto.ftl", data);
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getDtos().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "DTO.java").toString();
|
||||
entity.getSimpleName() + "DTO.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createController(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -141,12 +147,15 @@ public class CodeGenerator {
|
||||
data.setEntityPackage(entity.getPackageName());
|
||||
String code = CodeRenderer.render("controller.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "Controller.java").toString();
|
||||
entity.getSimpleName() + "Controller.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createService(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -159,15 +168,19 @@ public class CodeGenerator {
|
||||
|
||||
String code = CodeRenderer.render("service.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getServices().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "Service.java").toString();
|
||||
entity.getSimpleName() + "Service.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createServiceInterface(Class<?> entity) throws IOException, TemplateException {
|
||||
private static void createServiceInterface(Class<?> entity)
|
||||
throws IOException, TemplateException {
|
||||
|
||||
CodeRenderer.RenderingData data = new CodeRenderer.RenderingData();
|
||||
data.setConfig(config);
|
||||
@@ -177,12 +190,15 @@ public class CodeGenerator {
|
||||
|
||||
String code = CodeRenderer.render("serviceInterface.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getServices().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "Service.java").toString();
|
||||
entity.getSimpleName() + "Service.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createServiceBean(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -195,12 +211,16 @@ public class CodeGenerator {
|
||||
|
||||
String code = CodeRenderer.render("serviceBean.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath(), config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getServices().replaceAll("\\.", "/"),
|
||||
"impl", entity.getSimpleName() + "ServiceBean.java").toString();
|
||||
"impl",
|
||||
entity.getSimpleName() + "ServiceBean.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createCrudInterfaces() throws IOException, TemplateException {
|
||||
@@ -209,15 +229,24 @@ public class CodeGenerator {
|
||||
data.setConfig(config);
|
||||
|
||||
String code = CodeRenderer.render("crudservice.ftl", data);
|
||||
String filepath = Paths.get(config.getProjectPath() , config.getOutputDirectory(),
|
||||
config.getOutputPackages().getServices().replaceAll("\\.", "/"), "CrudService.java").toString();
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getServices().replaceAll("\\.", "/"),
|
||||
"CrudService.java")
|
||||
.toString();
|
||||
writeFile(code, filepath);
|
||||
|
||||
code = CodeRenderer.render("crudcontroller.ftl", data);
|
||||
filepath = Paths.get(config.getProjectPath() , config.getOutputDirectory() ,
|
||||
config.getOutputPackages().getControllers().replaceAll("\\.", "/"), "CrudController.java").toString();
|
||||
filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
|
||||
"CrudController.java")
|
||||
.toString();
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void createRepository(Class<?> entity) throws IOException, TemplateException {
|
||||
@@ -229,12 +258,15 @@ public class CodeGenerator {
|
||||
|
||||
String code = CodeRenderer.render("repository.ftl", data);
|
||||
|
||||
String filepath = Paths.get(config.getProjectPath() , config.getOutputDirectory(),
|
||||
String filepath =
|
||||
Paths.get(
|
||||
config.getProjectPath(),
|
||||
config.getOutputDirectory(),
|
||||
config.getOutputPackages().getRepositories().replaceAll("\\.", "/"),
|
||||
entity.getSimpleName() + "Repository.java").toString();
|
||||
entity.getSimpleName() + "Repository.java")
|
||||
.toString();
|
||||
|
||||
writeFile(code, filepath);
|
||||
|
||||
}
|
||||
|
||||
private static void writeFile(String code, String filepath) throws IOException {
|
||||
@@ -245,7 +277,6 @@ public class CodeGenerator {
|
||||
}
|
||||
Files.write(path, code.getBytes());
|
||||
log.debug("path: {}, code: {}", path, code);
|
||||
|
||||
}
|
||||
|
||||
private static String getPrimaryKeyClass(Class<?> entity) {
|
||||
@@ -265,8 +296,5 @@ public class CodeGenerator {
|
||||
}
|
||||
|
||||
return ids.stream().findFirst().get().getType().getName();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,23 +7,19 @@ import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Code renderer.
|
||||
*/
|
||||
/** Code renderer. */
|
||||
public class CodeRenderer {
|
||||
|
||||
/**
|
||||
* Renders source code by using Freemarker template engine.
|
||||
*/
|
||||
public static String render(String templatePath, RenderingData data) throws IOException, TemplateException {
|
||||
/** Renders source code by using Freemarker template engine. */
|
||||
public static String render(String templatePath, RenderingData data)
|
||||
throws IOException, TemplateException {
|
||||
Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
|
||||
StringTemplateLoader templateLoader = new StringTemplateLoader();
|
||||
String source;
|
||||
@@ -44,9 +40,7 @@ public class CodeRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data used when rendering source code.
|
||||
*/
|
||||
/** Data used when rendering source code. */
|
||||
@Data
|
||||
public static class RenderingData {
|
||||
|
||||
@@ -59,7 +53,5 @@ public class CodeRenderer {
|
||||
private List<Field> entityFields;
|
||||
|
||||
private Date dateGen = new Date();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,10 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Utility to reader classpath resources.
|
||||
*/
|
||||
/** Utility to reader classpath resources. */
|
||||
public class ResourceReader {
|
||||
|
||||
private ResourceReader() {
|
||||
}
|
||||
private ResourceReader() {}
|
||||
|
||||
public static InputStream getResourceAsStream(String path) throws IOException {
|
||||
InputStream classPathResource = ResourceReader.class.getClassLoader().getResourceAsStream(path);
|
||||
@@ -21,5 +18,4 @@ public class ResourceReader {
|
||||
InputStream fileResource = new FileInputStream(new File(path));
|
||||
return fileResource;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
package gae.piaz.layer3gen.config;
|
||||
|
||||
import gae.piaz.layer3gen.ResourceReader;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
@Data
|
||||
@Slf4j
|
||||
@@ -36,17 +35,12 @@ public class CodeGeneratorConfig implements Serializable {
|
||||
Path a = Paths.get(path);
|
||||
log.info("Configuration path: {}", a.toString());
|
||||
is = Files.newInputStream(a);
|
||||
}
|
||||
|
||||
else{
|
||||
} else {
|
||||
is = ResourceReader.getResourceAsStream(path);
|
||||
}
|
||||
|
||||
try (Reader reader = new InputStreamReader(is)) {
|
||||
return YAML.loadAs(reader, CodeGeneratorConfig.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
package gae.piaz.layer3gen.gradle;
|
||||
|
||||
import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
import org.gradle.api.invocation.Gradle;
|
||||
import org.gradle.api.plugins.JavaPluginConvention;
|
||||
import org.gradle.api.tasks.SourceSetContainer;
|
||||
import org.gradle.tooling.model.GradleProject;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
import org.gradle.api.plugins.JavaPluginConvention;
|
||||
import org.gradle.api.tasks.SourceSetContainer;
|
||||
|
||||
public final class ClassLoaderBuilderGradle {
|
||||
|
||||
private ClassLoaderBuilderGradle(){
|
||||
}
|
||||
private ClassLoaderBuilderGradle() {}
|
||||
|
||||
public static URLClassLoader getClassLoader(Project project) throws MalformedURLException {
|
||||
List<URL> listOfURL = new ArrayList<>();
|
||||
SourceSetContainer ssc = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
|
||||
SourceSetContainer ssc =
|
||||
project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
|
||||
FileCollection classesDir = ssc.getByName("main").getOutput().getClassesDirs();
|
||||
for (File file : classesDir) {
|
||||
listOfURL.add(file.toURI().toURL());
|
||||
|
||||
@@ -6,5 +6,4 @@ import lombok.Data;
|
||||
public class Layer3GenExtension {
|
||||
|
||||
private String configPath = "src/main/resources/3layer-settings.yml";
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package gae.piaz.layer3gen.gradle;
|
||||
|
||||
import gae.piaz.layer3gen.CodeGenerator;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
|
||||
/**
|
||||
* entityGen Gradle plugin.
|
||||
*/
|
||||
/** entityGen Gradle plugin. */
|
||||
public class Layer3GenPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,9 +5,7 @@ import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
import org.gradle.api.DefaultTask;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
|
||||
/**
|
||||
* entityGen Gradle task.
|
||||
*/
|
||||
/** entityGen Gradle task. */
|
||||
public class Layer3GenTask extends DefaultTask {
|
||||
|
||||
@TaskAction
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package gae.piaz.layer3gen.main;
|
||||
|
||||
import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
@@ -13,10 +12,11 @@ public final class ClassLoaderBuilderMain {
|
||||
|
||||
private ClassLoaderBuilderMain() {}
|
||||
|
||||
public static URLClassLoader getClassLoader(CodeGeneratorConfig config) throws MalformedURLException {
|
||||
final File classes = new File(Paths.get(config.getProjectPath(), config.getClassesDirectory()).toString());
|
||||
public static URLClassLoader getClassLoader(CodeGeneratorConfig config)
|
||||
throws MalformedURLException {
|
||||
final File classes =
|
||||
new File(Paths.get(config.getProjectPath(), config.getClassesDirectory()).toString());
|
||||
List<URL> listOfURL = List.of(classes.toURI().toURL());
|
||||
return new URLClassLoader(listOfURL.toArray(new URL[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,5 +14,4 @@ public class Layer3GenMain {
|
||||
CodeGeneratorConfig config = CodeGeneratorConfig.load(configFile, false);
|
||||
CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
package gae.piaz.layer3gen.test;
|
||||
|
||||
|
||||
import freemarker.template.TemplateException;
|
||||
import gae.piaz.layer3gen.CodeGenerator;
|
||||
import gae.piaz.layer3gen.config.CodeGeneratorConfig;
|
||||
import gae.piaz.layer3gen.main.ClassLoaderBuilderMain;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestMainGenerator {
|
||||
|
||||
@@ -22,5 +20,4 @@ public class TestMainGenerator {
|
||||
CodeGeneratorConfig config = CodeGeneratorConfig.load("3layer-settings-jakarta.yml", true);
|
||||
CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user