Customizable record builders

This commit is contained in:
Andrea Cavalli 2022-03-09 12:53:16 +01:00
parent 8a9122447d
commit 1a90b38ff0
3 changed files with 10 additions and 4 deletions

View File

@ -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);
}

View File

@ -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<String, InterfaceDataConfiguration> 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"));
if (nextVersion.isEmpty() && useRecordBuilders) {
typeClass.addAnnotation(RecordBuilder.class);
}
var getBasicTypeMethod = MethodSpec
.methodBuilder("getBasicType$")
.addModifiers(Modifier.PUBLIC)

View File

@ -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]));
}
}