From 1a90b38ff082b7580a717e92616d5171f861f2ce Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 9 Mar 2022 12:53:16 +0100 Subject: [PATCH] Customizable record builders --- src/main/java/it/cavallium/data/generator/MavenPlugin.java | 5 ++++- .../java/it/cavallium/data/generator/SourcesGenerator.java | 7 +++++-- src/main/java/it/cavallium/data/generator/Standalone.java | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/cavallium/data/generator/MavenPlugin.java b/src/main/java/it/cavallium/data/generator/MavenPlugin.java index a9f86b7..b07f2ce 100644 --- a/src/main/java/it/cavallium/data/generator/MavenPlugin.java +++ b/src/main/java/it/cavallium/data/generator/MavenPlugin.java @@ -21,6 +21,9 @@ public class MavenPlugin extends AbstractMojo { @Parameter( required = true) private String basePackageName; + @Parameter( required = true, defaultValue = "false") + private String useRecordBuilder; + /** * @parameter default-value="${project}" * @required @@ -38,7 +41,7 @@ public class MavenPlugin extends AbstractMojo { Path outPath = genRecordsPath.resolve("java"); this.project.addCompileSourceRoot(outPath.toString()); - sourcesGenerator.generateSources(basePackageName, outPath); + sourcesGenerator.generateSources(basePackageName, outPath, Boolean.parseBoolean(useRecordBuilder)); } catch (IOException e) { throw new MojoExecutionException("Exception while generating classes", e); } diff --git a/src/main/java/it/cavallium/data/generator/SourcesGenerator.java b/src/main/java/it/cavallium/data/generator/SourcesGenerator.java index d54b53d..4cde490 100644 --- a/src/main/java/it/cavallium/data/generator/SourcesGenerator.java +++ b/src/main/java/it/cavallium/data/generator/SourcesGenerator.java @@ -93,8 +93,9 @@ public class SourcesGenerator { /** * @param basePackageName org.example * @param outPath path/to/output + * @param useRecordBuilders if true, the data will have @RecordBuilder annotation */ - public void generateSources(String basePackageName, Path outPath) throws IOException { + public void generateSources(String basePackageName, Path outPath, boolean useRecordBuilders) throws IOException { // Fix the configuration for (Entry interfacesDatum : configuration.interfacesData.entrySet()) { @@ -2117,7 +2118,9 @@ public class SourcesGenerator { typeClass.addModifiers(Modifier.STATIC); typeClass.addModifiers(Modifier.FINAL); typeClass.addSuperinterface(ClassName.get(joinPackage(versionPackage, "data"), "IBasicType")); - typeClass.addAnnotation(RecordBuilder.class); + if (nextVersion.isEmpty() && useRecordBuilders) { + typeClass.addAnnotation(RecordBuilder.class); + } var getBasicTypeMethod = MethodSpec .methodBuilder("getBasicType$") .addModifiers(Modifier.PUBLIC) diff --git a/src/main/java/it/cavallium/data/generator/Standalone.java b/src/main/java/it/cavallium/data/generator/Standalone.java index 5c3ab81..0ed2b16 100644 --- a/src/main/java/it/cavallium/data/generator/Standalone.java +++ b/src/main/java/it/cavallium/data/generator/Standalone.java @@ -7,6 +7,6 @@ public class Standalone { public static void main(String[] args) throws IOException { SourcesGenerator sourcesGenerator = SourcesGenerator.load(Paths.get(args[0])); - sourcesGenerator.generateSources(args[1], Paths.get(args[2])); + sourcesGenerator.generateSources(args[1], Paths.get(args[2]), Boolean.parseBoolean(args[3])); } }