This commit is contained in:
Andrea Cavalli 2023-01-22 12:39:54 +01:00
parent 9113ae8bf8
commit 8d02729342
8 changed files with 33 additions and 11 deletions

View File

@ -28,6 +28,7 @@ public abstract class ClassGenerator {
private final Path outPath;
protected final boolean deepCheckBeforeCreatingNewEqualInstances;
protected final boolean useRecordBuilders;
protected final boolean generateOldSerializers;
public ClassGenerator(ClassGeneratorParams params) {
this.generatedFilesToDelete = params.generatedFilesToDelete;
@ -36,6 +37,7 @@ public abstract class ClassGenerator {
this.outPath = params.outPath;
this.deepCheckBeforeCreatingNewEqualInstances = params.deepCheckBeforeCreatingNewEqualInstances;
this.useRecordBuilders = params.useRecordBuilders;
this.generateOldSerializers = params.generateOldSerializers;
}
public void run() throws IOException {
@ -86,5 +88,6 @@ public abstract class ClassGenerator {
String basePackageName,
Path outPath,
boolean deepCheckBeforeCreatingNewEqualInstances,
boolean useRecordBuilders) {}
boolean useRecordBuilders,
boolean generateOldSerializers) {}
}

View File

@ -23,6 +23,9 @@ public class MavenPlugin extends AbstractMojo {
@Parameter( required = true, defaultValue = "true")
private String deepCheckBeforeCreatingNewEqualInstances;
@Parameter( required = true, defaultValue = "false")
private String generateOldSerializers;
@Parameter( required = true, defaultValue = "false")
private String useRecordBuilder;
@ -42,7 +45,8 @@ public class MavenPlugin extends AbstractMojo {
Path outPath = genRecordsPath.resolve("java");
this.project.addCompileSourceRoot(outPath.toString());
sourcesGenerator.generateSources(basePackageName, outPath, Boolean.parseBoolean(useRecordBuilder), false, Boolean.parseBoolean(deepCheckBeforeCreatingNewEqualInstances));
sourcesGenerator.generateSources(basePackageName, outPath, Boolean.parseBoolean(useRecordBuilder), false, Boolean.parseBoolean(deepCheckBeforeCreatingNewEqualInstances),
Boolean.parseBoolean(generateOldSerializers));
} catch (IOException e) {
throw new MojoExecutionException("Exception while generating classes", e);
}

View File

@ -100,7 +100,7 @@ public class SourcesGenerator {
* @param force force overwrite
* @param deepCheckBeforeCreatingNewEqualInstances if true, use equals, if false, use ==
*/
public void generateSources(String basePackageName, Path outPath, boolean useRecordBuilders, boolean force, boolean deepCheckBeforeCreatingNewEqualInstances) throws IOException {
public void generateSources(String basePackageName, Path outPath, boolean useRecordBuilders, boolean force, boolean deepCheckBeforeCreatingNewEqualInstances, boolean generateOldSerializers) throws IOException {
Path basePackageNamePath;
{
Path basePackageNamePathPartial = outPath;
@ -113,16 +113,18 @@ public class SourcesGenerator {
var curHash = dataModel.computeHash();
if (Files.isRegularFile(hashPath) && Files.isReadable(hashPath)) {
var lines = Files.readAllLines(hashPath, StandardCharsets.UTF_8);
if (lines.size() >= 4) {
if (lines.size() >= 5) {
var prevBasePackageName = lines.get(0);
var prevRecordBuilders = lines.get(1);
var prevHash = lines.get(2);
var prevDeepCheckBeforeCreatingNewEqualInstances = lines.get(3);
var prevGenerateOldSerializers = lines.get(4);
if (!force
&& prevBasePackageName.equals(basePackageName)
&& (prevRecordBuilders.equalsIgnoreCase("true") == useRecordBuilders)
&& (prevDeepCheckBeforeCreatingNewEqualInstances.equalsIgnoreCase("true") == deepCheckBeforeCreatingNewEqualInstances)
&& (prevGenerateOldSerializers.equalsIgnoreCase("true") == generateOldSerializers)
&& prevHash.equals(Integer.toString(curHash))) {
logger.info("Skipped sources generation because it didn't change");
return;
@ -148,7 +150,7 @@ public class SourcesGenerator {
.collect(Collectors.toCollection(HashSet::new));
}
var genParams = new ClassGeneratorParams(generatedFilesToDelete, dataModel, basePackageName, outPath, deepCheckBeforeCreatingNewEqualInstances, useRecordBuilders);
var genParams = new ClassGeneratorParams(generatedFilesToDelete, dataModel, basePackageName, outPath, deepCheckBeforeCreatingNewEqualInstances, useRecordBuilders, generateOldSerializers);
// Create the Versions class
new GenVersions(genParams).run();

View File

@ -1,12 +1,25 @@
package it.cavallium.data.generator.plugin;
import static java.lang.Boolean.parseBoolean;
import java.io.IOException;
import java.nio.file.Paths;
public class Standalone {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("[PATH] [BASE PACKAGE NAME] [OUT PATH] [USE RECORD BUILDERS] [FORCE] [STANDARD CHECKS] [GENERATE OLD SERIALIZERS]");
System.exit(1);
return;
}
SourcesGenerator sourcesGenerator = SourcesGenerator.load(Paths.get(args[0]));
sourcesGenerator.generateSources(args[1], Paths.get(args[2]), Boolean.parseBoolean(args[3]), false, true);
sourcesGenerator.generateSources(args[1],
Paths.get(args[2]),
parseBoolean(args[3]),
parseBoolean(args[4]),
parseBoolean(args[5]),
parseBoolean(args[6])
);
}
}

View File

@ -71,7 +71,7 @@ public class GenSerializerArrayX extends ClassGenerator {
.addAnnotation(NotNull.class)
.build());
if (version.isCurrent()) {
if (generateOldSerializers || version.isCurrent()) {
method.addStatement("$T.requireNonNull(data)", Objects.class);
method.addCode("\n");
method.addStatement("final int sz = data.size()");

View File

@ -68,7 +68,7 @@ public class GenSerializerBaseX extends ClassGenerator {
.addAnnotation(NotNull.class)
.build());
if (version.isCurrent()) {
if (generateOldSerializers || version.isCurrent()) {
method.addStatement("$T.requireNonNull(data)", Objects.class);
method.addCode("\n");

View File

@ -74,10 +74,10 @@ public class GenSerializerNullableX extends ClassGenerator {
.addAnnotation(NotNull.class)
.build());
if (version.isCurrent()) {
if (generateOldSerializers || version.isCurrent()) {
method.addStatement("$T.requireNonNull(data)", Objects.class);
method.addCode("\n");
method.addStatement("boolean notEmpty = !data.isEmpty()");
method.addStatement("boolean notEmpty = data.getNullable() != null");
method.addStatement("out.writeBoolean(notEmpty)");
method.beginControlFlow("if (notEmpty)");
method.addStatement("$T.$N.serialize(out, ($T) data.getNullable())",

View File

@ -83,7 +83,7 @@ public class GenSerializerSuperX extends ClassGenerator {
.addAnnotation(NotNull.class)
.build());
if (version.isCurrent()) {
if (generateOldSerializers || version.isCurrent()) {
method.addStatement("$T.requireNonNull(data)", Objects.class);
method.addStatement("int id = data.getMetaId$$$N()", typeSuper.getName());
method.addStatement("out.writeByte(id)");