Added spotless plugin

This commit is contained in:
Gaetano_Piazzolla
2023-02-26 16:43:54 +01:00
parent 8a2dcb948e
commit b3287e16a5
15 changed files with 374 additions and 369 deletions

View File

@@ -4,6 +4,7 @@ plugins {
id 'java-gradle-plugin' id 'java-gradle-plugin'
id 'maven-publish' id 'maven-publish'
id 'application' id 'application'
id "com.diffplug.spotless" version "6.15.0"
id 'com.gradle.plugin-publish' version '0.12.0' id 'com.gradle.plugin-publish' version '0.12.0'
} }
@@ -74,3 +75,12 @@ installDist {
exclude 'gradle-api-6.6.1.jar' exclude 'gradle-api-6.6.1.jar'
} }
spotless {
java {
importOrder()
removeUnusedImports()
cleanthat()
googleJavaFormat()
formatAnnotations()
}
}

View File

@@ -2,11 +2,6 @@ package gae.piaz.layer3gen;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import gae.piaz.layer3gen.config.CodeGeneratorConfig; 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.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.URLClassLoader; import java.net.URLClassLoader;
@@ -14,8 +9,11 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional;
import java.util.Set; 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 @Slf4j
public class CodeGenerator { public class CodeGenerator {
@@ -26,7 +24,8 @@ public class CodeGenerator {
private static URLClassLoader classLoader; 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.config = arg;
CodeGenerator.classLoader = classLoader; CodeGenerator.classLoader = classLoader;
@@ -36,7 +35,6 @@ public class CodeGenerator {
Set<Class<?>> entities = getEntityClasses(); Set<Class<?>> entities = getEntityClasses();
log.debug("found {} entities", entities.size()); log.debug("found {} entities", entities.size());
generateCode(entities); generateCode(entities);
} }
private static void generateCode(Set<Class<?>> entities) throws IOException, TemplateException { private static void generateCode(Set<Class<?>> entities) throws IOException, TemplateException {
@@ -64,17 +62,16 @@ public class CodeGenerator {
createControllerDTO(entity); createControllerDTO(entity);
} }
} }
} }
private static Set<Class<?>> getEntityClasses() { 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); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);
classes.addAll(reflections.getTypesAnnotatedWith(jakarta.persistence.Entity.class)); classes.addAll(reflections.getTypesAnnotatedWith(jakarta.persistence.Entity.class));
return classes; return classes;
} }
private static void createControllerDTO(Class<?> entity) throws IOException, TemplateException { private static void createControllerDTO(Class<?> entity) throws IOException, TemplateException {
CodeRenderer.RenderingData data = new CodeRenderer.RenderingData(); CodeRenderer.RenderingData data = new CodeRenderer.RenderingData();
@@ -84,12 +81,15 @@ public class CodeGenerator {
data.setEntityPackage(entity.getPackageName()); data.setEntityPackage(entity.getPackageName());
String code = CodeRenderer.render("controllerdto.ftl", data); 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("\\.", "/"), config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
entity.getSimpleName() + "ControllerDTO.java").toString(); entity.getSimpleName() + "ControllerDTO.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createMapper(Class<?> entity) throws IOException, TemplateException { private static void createMapper(Class<?> entity) throws IOException, TemplateException {
@@ -104,12 +104,15 @@ public class CodeGenerator {
data.setEntityPackage(entity.getPackageName()); data.setEntityPackage(entity.getPackageName());
String code = CodeRenderer.render("mapper.ftl", data); 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("\\.", "/"), config.getOutputPackages().getMappers().replaceAll("\\.", "/"),
entity.getSimpleName() + "Mapper.java").toString(); entity.getSimpleName() + "Mapper.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createDto(Class<?> entity) throws IOException, TemplateException { private static void createDto(Class<?> entity) throws IOException, TemplateException {
@@ -124,12 +127,15 @@ public class CodeGenerator {
data.setEntityFields(Arrays.asList(entity.getDeclaredFields())); data.setEntityFields(Arrays.asList(entity.getDeclaredFields()));
String code = CodeRenderer.render("dto.ftl", data); 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("\\.", "/"), config.getOutputPackages().getDtos().replaceAll("\\.", "/"),
entity.getSimpleName() + "DTO.java").toString(); entity.getSimpleName() + "DTO.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createController(Class<?> entity) throws IOException, TemplateException { private static void createController(Class<?> entity) throws IOException, TemplateException {
@@ -141,12 +147,15 @@ public class CodeGenerator {
data.setEntityPackage(entity.getPackageName()); data.setEntityPackage(entity.getPackageName());
String code = CodeRenderer.render("controller.ftl", data); 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("\\.", "/"), config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
entity.getSimpleName() + "Controller.java").toString(); entity.getSimpleName() + "Controller.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createService(Class<?> entity) throws IOException, TemplateException { private static void createService(Class<?> entity) throws IOException, TemplateException {
@@ -159,15 +168,19 @@ public class CodeGenerator {
String code = CodeRenderer.render("service.ftl", data); 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("\\.", "/"), config.getOutputPackages().getServices().replaceAll("\\.", "/"),
entity.getSimpleName() + "Service.java").toString(); entity.getSimpleName() + "Service.java")
.toString();
writeFile(code, filepath); 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(); CodeRenderer.RenderingData data = new CodeRenderer.RenderingData();
data.setConfig(config); data.setConfig(config);
@@ -177,12 +190,15 @@ public class CodeGenerator {
String code = CodeRenderer.render("serviceInterface.ftl", data); 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("\\.", "/"), config.getOutputPackages().getServices().replaceAll("\\.", "/"),
entity.getSimpleName() + "Service.java").toString(); entity.getSimpleName() + "Service.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createServiceBean(Class<?> entity) throws IOException, TemplateException { private static void createServiceBean(Class<?> entity) throws IOException, TemplateException {
@@ -195,12 +211,16 @@ public class CodeGenerator {
String code = CodeRenderer.render("serviceBean.ftl", data); 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("\\.", "/"), config.getOutputPackages().getServices().replaceAll("\\.", "/"),
"impl", entity.getSimpleName() + "ServiceBean.java").toString(); "impl",
entity.getSimpleName() + "ServiceBean.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createCrudInterfaces() throws IOException, TemplateException { private static void createCrudInterfaces() throws IOException, TemplateException {
@@ -209,15 +229,24 @@ public class CodeGenerator {
data.setConfig(config); data.setConfig(config);
String code = CodeRenderer.render("crudservice.ftl", data); String code = CodeRenderer.render("crudservice.ftl", data);
String filepath = Paths.get(config.getProjectPath() , config.getOutputDirectory(), String filepath =
config.getOutputPackages().getServices().replaceAll("\\.", "/"), "CrudService.java").toString(); Paths.get(
config.getProjectPath(),
config.getOutputDirectory(),
config.getOutputPackages().getServices().replaceAll("\\.", "/"),
"CrudService.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
code = CodeRenderer.render("crudcontroller.ftl", data); code = CodeRenderer.render("crudcontroller.ftl", data);
filepath = Paths.get(config.getProjectPath() , config.getOutputDirectory() , filepath =
config.getOutputPackages().getControllers().replaceAll("\\.", "/"), "CrudController.java").toString(); Paths.get(
config.getProjectPath(),
config.getOutputDirectory(),
config.getOutputPackages().getControllers().replaceAll("\\.", "/"),
"CrudController.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void createRepository(Class<?> entity) throws IOException, TemplateException { private static void createRepository(Class<?> entity) throws IOException, TemplateException {
@@ -229,12 +258,15 @@ public class CodeGenerator {
String code = CodeRenderer.render("repository.ftl", data); 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("\\.", "/"), config.getOutputPackages().getRepositories().replaceAll("\\.", "/"),
entity.getSimpleName() + "Repository.java").toString(); entity.getSimpleName() + "Repository.java")
.toString();
writeFile(code, filepath); writeFile(code, filepath);
} }
private static void writeFile(String code, String filepath) throws IOException { private static void writeFile(String code, String filepath) throws IOException {
@@ -245,7 +277,6 @@ public class CodeGenerator {
} }
Files.write(path, code.getBytes()); Files.write(path, code.getBytes());
log.debug("path: {}, code: {}", path, code); log.debug("path: {}, code: {}", path, code);
} }
private static String getPrimaryKeyClass(Class<?> entity) { private static String getPrimaryKeyClass(Class<?> entity) {
@@ -265,8 +296,5 @@ public class CodeGenerator {
} }
return ids.stream().findFirst().get().getType().getName(); return ids.stream().findFirst().get().getType().getName();
} }
} }

View File

@@ -7,23 +7,19 @@ import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateExceptionHandler;
import gae.piaz.layer3gen.config.CodeGeneratorConfig; import gae.piaz.layer3gen.config.CodeGeneratorConfig;
import lombok.Data;
import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.Data;
/** /** Code renderer. */
* Code renderer.
*/
public class CodeRenderer { public class CodeRenderer {
/** /** Renders source code by using Freemarker template engine. */
* Renders source code by using Freemarker template engine. public static String render(String templatePath, RenderingData data)
*/ throws IOException, TemplateException {
public static String render(String templatePath, RenderingData data) throws IOException, TemplateException {
Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
StringTemplateLoader templateLoader = new StringTemplateLoader(); StringTemplateLoader templateLoader = new StringTemplateLoader();
String source; String source;
@@ -44,9 +40,7 @@ public class CodeRenderer {
} }
} }
/** /** Data used when rendering source code. */
* Data used when rendering source code.
*/
@Data @Data
public static class RenderingData { public static class RenderingData {
@@ -59,7 +53,5 @@ public class CodeRenderer {
private List<Field> entityFields; private List<Field> entityFields;
private Date dateGen = new Date(); private Date dateGen = new Date();
} }
} }

View File

@@ -5,13 +5,10 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /** Utility to reader classpath resources. */
* Utility to reader classpath resources.
*/
public class ResourceReader { public class ResourceReader {
private ResourceReader() { private ResourceReader() {}
}
public static InputStream getResourceAsStream(String path) throws IOException { public static InputStream getResourceAsStream(String path) throws IOException {
InputStream classPathResource = ResourceReader.class.getClassLoader().getResourceAsStream(path); InputStream classPathResource = ResourceReader.class.getClassLoader().getResourceAsStream(path);
@@ -21,5 +18,4 @@ public class ResourceReader {
InputStream fileResource = new FileInputStream(new File(path)); InputStream fileResource = new FileInputStream(new File(path));
return fileResource; return fileResource;
} }
} }

View File

@@ -1,15 +1,14 @@
package gae.piaz.layer3gen.config; package gae.piaz.layer3gen.config;
import gae.piaz.layer3gen.ResourceReader; 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.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; 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 @Data
@Slf4j @Slf4j
@@ -36,17 +35,12 @@ public class CodeGeneratorConfig implements Serializable {
Path a = Paths.get(path); Path a = Paths.get(path);
log.info("Configuration path: {}", a.toString()); log.info("Configuration path: {}", a.toString());
is = Files.newInputStream(a); is = Files.newInputStream(a);
} } else {
else{
is = ResourceReader.getResourceAsStream(path); is = ResourceReader.getResourceAsStream(path);
} }
try (Reader reader = new InputStreamReader(is)) { try (Reader reader = new InputStreamReader(is)) {
return YAML.loadAs(reader, CodeGeneratorConfig.class); return YAML.loadAs(reader, CodeGeneratorConfig.class);
} }
} }
} }

View File

@@ -1,28 +1,24 @@
package gae.piaz.layer3gen.gradle; 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.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 { public final class ClassLoaderBuilderGradle {
private ClassLoaderBuilderGradle(){ private ClassLoaderBuilderGradle() {}
}
public static URLClassLoader getClassLoader(Project project) throws MalformedURLException { public static URLClassLoader getClassLoader(Project project) throws MalformedURLException {
List<URL> listOfURL = new ArrayList<>(); 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(); FileCollection classesDir = ssc.getByName("main").getOutput().getClassesDirs();
for (File file : classesDir) { for (File file : classesDir) {
listOfURL.add(file.toURI().toURL()); listOfURL.add(file.toURI().toURL());

View File

@@ -6,5 +6,4 @@ import lombok.Data;
public class Layer3GenExtension { public class Layer3GenExtension {
private String configPath = "src/main/resources/3layer-settings.yml"; private String configPath = "src/main/resources/3layer-settings.yml";
} }

View File

@@ -1,13 +1,9 @@
package gae.piaz.layer3gen.gradle; package gae.piaz.layer3gen.gradle;
import gae.piaz.layer3gen.CodeGenerator;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.Task;
/** /** entityGen Gradle plugin. */
* entityGen Gradle plugin.
*/
public class Layer3GenPlugin implements Plugin<Project> { public class Layer3GenPlugin implements Plugin<Project> {
@Override @Override

View File

@@ -5,9 +5,7 @@ import gae.piaz.layer3gen.config.CodeGeneratorConfig;
import org.gradle.api.DefaultTask; import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
/** /** entityGen Gradle task. */
* entityGen Gradle task.
*/
public class Layer3GenTask extends DefaultTask { public class Layer3GenTask extends DefaultTask {
@TaskAction @TaskAction

View File

@@ -1,7 +1,6 @@
package gae.piaz.layer3gen.main; package gae.piaz.layer3gen.main;
import gae.piaz.layer3gen.config.CodeGeneratorConfig; import gae.piaz.layer3gen.config.CodeGeneratorConfig;
import java.io.File; import java.io.File;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@@ -13,10 +12,11 @@ public final class ClassLoaderBuilderMain {
private ClassLoaderBuilderMain() {} private ClassLoaderBuilderMain() {}
public static URLClassLoader getClassLoader(CodeGeneratorConfig config) throws MalformedURLException { public static URLClassLoader getClassLoader(CodeGeneratorConfig config)
final File classes = new File(Paths.get(config.getProjectPath(), config.getClassesDirectory()).toString()); throws MalformedURLException {
final File classes =
new File(Paths.get(config.getProjectPath(), config.getClassesDirectory()).toString());
List<URL> listOfURL = List.of(classes.toURI().toURL()); List<URL> listOfURL = List.of(classes.toURI().toURL());
return new URLClassLoader(listOfURL.toArray(new URL[0])); return new URLClassLoader(listOfURL.toArray(new URL[0]));
} }
} }

View File

@@ -14,5 +14,4 @@ public class Layer3GenMain {
CodeGeneratorConfig config = CodeGeneratorConfig.load(configFile, false); CodeGeneratorConfig config = CodeGeneratorConfig.load(configFile, false);
CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config)); CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config));
} }
} }

View File

@@ -1,13 +1,11 @@
package gae.piaz.layer3gen.test; package gae.piaz.layer3gen.test;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import gae.piaz.layer3gen.CodeGenerator; import gae.piaz.layer3gen.CodeGenerator;
import gae.piaz.layer3gen.config.CodeGeneratorConfig; import gae.piaz.layer3gen.config.CodeGeneratorConfig;
import gae.piaz.layer3gen.main.ClassLoaderBuilderMain; import gae.piaz.layer3gen.main.ClassLoaderBuilderMain;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import org.junit.Test;
public class TestMainGenerator { public class TestMainGenerator {
@@ -22,5 +20,4 @@ public class TestMainGenerator {
CodeGeneratorConfig config = CodeGeneratorConfig.load("3layer-settings-jakarta.yml", true); CodeGeneratorConfig config = CodeGeneratorConfig.load("3layer-settings-jakarta.yml", true);
CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config)); CodeGenerator.run(config, ClassLoaderBuilderMain.getClassLoader(config));
} }
} }