Code cleanup

This commit is contained in:
Andrea Cavalli 2022-06-22 12:48:43 +02:00
parent 54daed1dc8
commit cec8b6b184

View File

@ -1285,7 +1285,7 @@ public class SourcesGenerator {
deserializeMethod.addComment("TRANSFORMATION #" + transformationNumber.incrementAndGet() + ": " deserializeMethod.addComment("TRANSFORMATION #" + transformationNumber.incrementAndGet() + ": "
+ transformation.getTransformName()); + transformation.getTransformName());
switch (transformation.getTransformName()) { switch (transformation.getTransformName()) {
case "remove-data": case "remove-data" -> {
var removeDataTransformation = (RemoveDataConfiguration) transformation; var removeDataTransformation = (RemoveDataConfiguration) transformation;
{ {
deserializeMethod.addComment( deserializeMethod.addComment(
@ -1298,13 +1298,11 @@ public class SourcesGenerator {
currentVarUpgraded.remove(removeDataTransformation.from); currentVarUpgraded.remove(removeDataTransformation.from);
currentVarDeleted.add(removeDataTransformation.from); currentVarDeleted.add(removeDataTransformation.from);
} }
Objects.requireNonNull(currentTransformedFieldTypes.remove( Objects.requireNonNull(currentTransformedFieldTypes.remove(
removeDataTransformation.transformClass + "." + removeDataTransformation.from)); removeDataTransformation.transformClass + "." + removeDataTransformation.from));
break; }
case "move-data": case "move-data" -> {
var moveDataTransformation = (MoveDataConfiguration) transformation; var moveDataTransformation = (MoveDataConfiguration) transformation;
{ {
currentVarNumber.addTo(moveDataTransformation.to, 1); currentVarNumber.addTo(moveDataTransformation.to, 1);
currentVarTypeName.put(moveDataTransformation.to, currentVarTypeName.put(moveDataTransformation.to,
@ -1336,14 +1334,13 @@ public class SourcesGenerator {
currentVarUpgraded.remove(moveDataTransformation.from); currentVarUpgraded.remove(moveDataTransformation.from);
currentVarDeleted.add(moveDataTransformation.from); currentVarDeleted.add(moveDataTransformation.from);
} }
currentTransformedFieldTypes.put( currentTransformedFieldTypes.put(
moveDataTransformation.transformClass + "." + moveDataTransformation.to, moveDataTransformation.transformClass + "." + moveDataTransformation.to,
Objects.requireNonNull(currentTransformedFieldTypes.remove( Objects.requireNonNull(currentTransformedFieldTypes.remove(
moveDataTransformation.transformClass + "." + moveDataTransformation.from)) moveDataTransformation.transformClass + "." + moveDataTransformation.from))
); );
break; }
case "upgrade-data": case "upgrade-data" -> {
var upgradeDataTransformation = (UpgradeDataConfiguration) transformation; var upgradeDataTransformation = (UpgradeDataConfiguration) transformation;
TypeName fromType = currentTransformedFieldTypes.get( TypeName fromType = currentTransformedFieldTypes.get(
upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from); upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from);
@ -1365,8 +1362,7 @@ public class SourcesGenerator {
toTypeBoxed toTypeBoxed
); );
var fieldName = "DATA_UPGRADER_" + nextDataUpgraderInstanceFieldId.getAndIncrement(); var fieldName = "DATA_UPGRADER_" + nextDataUpgraderInstanceFieldId.getAndIncrement();
var fieldSpec = FieldSpec var fieldSpec = FieldSpec.builder(dataUpgraderType,
.builder(dataUpgraderType,
fieldName, fieldName,
Modifier.PRIVATE, Modifier.PRIVATE,
Modifier.STATIC, Modifier.STATIC,
@ -1378,13 +1374,9 @@ public class SourcesGenerator {
} }
); );
deserializeMethod.addStatement( deserializeMethod.addStatement(
"$T upgraded = ($T) " + dataUpgraderFieldName + ".upgrade(($T) $$field$$" + currentVarNumber.getInt( "$T upgraded = ($T) " + dataUpgraderFieldName + ".upgrade(($T) $$field$$"
upgradeDataTransformation.from) + "$$" + upgradeDataTransformation.from + ")", + currentVarNumber.getInt(upgradeDataTransformation.from) + "$$"
toType, + upgradeDataTransformation.from + ")", toType, toTypeBoxed, fromTypeBoxed);
toTypeBoxed,
fromTypeBoxed
);
deserializeMethod.addStatement( deserializeMethod.addStatement(
"$$field$$" + (currentVarNumber.getInt(upgradeDataTransformation.from) + 1) + "$$" "$$field$$" + (currentVarNumber.getInt(upgradeDataTransformation.from) + 1) + "$$"
+ upgradeDataTransformation.from + " = upgraded"); + upgradeDataTransformation.from + " = upgraded");
@ -1392,7 +1384,6 @@ public class SourcesGenerator {
upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from)); upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from));
currentTransformedFieldTypes.put( currentTransformedFieldTypes.put(
upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from, toType); upgradeDataTransformation.transformClass + "." + upgradeDataTransformation.from, toType);
currentVarNumber.addTo(upgradeDataTransformation.from, 1); currentVarNumber.addTo(upgradeDataTransformation.from, 1);
currentVarTypeName.put(upgradeDataTransformation.from, toTypeName); currentVarTypeName.put(upgradeDataTransformation.from, toTypeName);
currentVarTypeClass.put(upgradeDataTransformation.from, toType); currentVarTypeClass.put(upgradeDataTransformation.from, toType);
@ -1403,35 +1394,42 @@ public class SourcesGenerator {
); );
currentVarUpgraded.add(upgradeDataTransformation.from); currentVarUpgraded.add(upgradeDataTransformation.from);
currentVarDeleted.remove(upgradeDataTransformation.from); currentVarDeleted.remove(upgradeDataTransformation.from);
break; }
case "new-data": case "new-data" -> {
var newDataTransformation = (NewDataConfiguration) transformation; var newDataTransformation = (NewDataConfiguration) transformation;
String newTypeName = configuration.versions.get(nextVersion.get()).classes String newTypeName = configuration.versions.get(nextVersion.get()).classes
.get(newDataTransformation.transformClass) .get(newDataTransformation.transformClass)
.getData() .getData()
.get(newDataTransformation.to); .get(newDataTransformation.to);
TypeName newType = nextVersionTypeTypes.get(newTypeName); TypeName newType = nextVersionTypeTypes.get(newTypeName);
Objects.requireNonNull(newType, () -> "Type \"" + newTypeName + "\" is not present from next version " + version + " to version " + nextVersion.get() + " in upgrader " + newDataTransformation.transformClass + "." + newDataTransformation.to); Objects.requireNonNull(newType,
() -> "Type \"" + newTypeName + "\" is not present from next version " + version
+ " to version " + nextVersion.get() + " in upgrader "
+ newDataTransformation.transformClass + "." + newDataTransformation.to
);
TypeName newTypeBoxed = newType.isPrimitive() ? newType.box() : newType; TypeName newTypeBoxed = newType.isPrimitive() ? newType.box() : newType;
{ {
currentVarNumber.addTo(newDataTransformation.to, 1); currentVarNumber.addTo(newDataTransformation.to, 1);
currentVarTypeName.put(newDataTransformation.to, newTypeName); currentVarTypeName.put(newDataTransformation.to, newTypeName);
currentVarTypeClass.put(newDataTransformation.to, newType); currentVarTypeClass.put(newDataTransformation.to, newType);
currentVarFamily.put(newDataTransformation.to, Objects.requireNonNull(typeFamily.get(newTypeName), currentVarFamily.put(newDataTransformation.to,
Objects.requireNonNull(typeFamily.get(newTypeName),
() -> "Type \"" + newTypeName + "\" has no type family!" () -> "Type \"" + newTypeName + "\" has no type family!"
)); )
);
currentVarUpgraded.add(newDataTransformation.to); currentVarUpgraded.add(newDataTransformation.to);
currentVarDeleted.remove(newDataTransformation.to); currentVarDeleted.remove(newDataTransformation.to);
var dataInitializerClass = ClassName.bestGuess(newDataTransformation.initializer); var dataInitializerClass = ClassName.bestGuess(newDataTransformation.initializer);
var dataInitializerFieldName = dataInitializerInstanceFieldName.computeIfAbsent(dataInitializerClass, var dataInitializerFieldName = dataInitializerInstanceFieldName.computeIfAbsent(
dataInitializerClass,
_unused -> { _unused -> {
var dataInitializerType = ParameterizedTypeName.get(ClassName.get(DataInitializer.class), var dataInitializerType = ParameterizedTypeName.get(ClassName.get(DataInitializer.class),
newTypeBoxed newTypeBoxed
); );
var fieldName = "DATA_INITIALIZER_" + nextDataInitializerInstanceFieldId.getAndIncrement(); var fieldName =
var fieldSpec = FieldSpec "DATA_INITIALIZER_" + nextDataInitializerInstanceFieldId.getAndIncrement();
.builder(dataInitializerType, var fieldSpec = FieldSpec.builder(dataInitializerType,
fieldName, fieldName,
Modifier.PRIVATE, Modifier.PRIVATE,
Modifier.STATIC, Modifier.STATIC,
@ -1445,16 +1443,14 @@ public class SourcesGenerator {
deserializeMethod.addStatement( deserializeMethod.addStatement(
"var $$field$$" + currentVarNumber.getInt(newDataTransformation.to) + "$$" "var $$field$$" + currentVarNumber.getInt(newDataTransformation.to) + "$$"
+ newDataTransformation.to + " = " + dataInitializerFieldName + ".initialize()" + newDataTransformation.to + " = " + dataInitializerFieldName + ".initialize()");
);
} }
if (currentTransformedFieldTypes.put( if (currentTransformedFieldTypes.put(
newDataTransformation.transformClass + "." + newDataTransformation.to, newType) != null) { newDataTransformation.transformClass + "." + newDataTransformation.to, newType) != null) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
break; }
default: default -> throw new UnsupportedOperationException(
throw new UnsupportedOperationException(
"Unknown transform type: " + transformation.getTransformName()); "Unknown transform type: " + transformation.getTransformName());
} }
} }
@ -1491,9 +1487,16 @@ public class SourcesGenerator {
currentVarDeleted.remove(key); currentVarDeleted.remove(key);
switch (currentFamily) { switch (currentFamily) {
case BASIC: case BASIC, GENERIC -> deserializeMethod.addCode(buildStatementUpgradeBasicType(versionPackage,
case GENERIC: nextVersionPackage.get(),
deserializeMethod.addCode(buildStatementUpgradeBasicType( number,
key,
toTypeName,
toFamily,
toType,
toTypeBoxed
));
case I_TYPE_ARRAY -> deserializeMethod.addCode(buildStatementUpgradeITypeArrayField(
versionPackage, versionPackage,
nextVersionPackage.get(), nextVersionPackage.get(),
number, number,
@ -1503,21 +1506,7 @@ public class SourcesGenerator {
toType, toType,
toTypeBoxed toTypeBoxed
)); ));
break; case NULLABLE_BASIC -> deserializeMethod.addCode(buildStatementUpgradeNullableBasicField(
case I_TYPE_ARRAY:
deserializeMethod.addCode(buildStatementUpgradeITypeArrayField(
versionPackage,
nextVersionPackage.get(),
number,
key,
toTypeName,
toFamily,
toType,
toTypeBoxed
));
break;
case NULLABLE_BASIC:
deserializeMethod.addCode(buildStatementUpgradeNullableBasicField(
versionPackage, versionPackage,
nextVersionPackage.get(), nextVersionPackage.get(),
number, number,
@ -1528,9 +1517,7 @@ public class SourcesGenerator {
toTypeBoxed, toTypeBoxed,
nextVersionTypeTypes.get(toTypeName.substring(1)) nextVersionTypeTypes.get(toTypeName.substring(1))
)); ));
break; case NULLABLE_GENERIC -> deserializeMethod.addCode(buildStatementUpgradeNullableGenericField(
case NULLABLE_GENERIC:
deserializeMethod.addCode(buildStatementUpgradeNullableGenericField(
versionPackage, versionPackage,
nextVersionPackage.get(), nextVersionPackage.get(),
number, number,
@ -1541,9 +1528,7 @@ public class SourcesGenerator {
toTypeBoxed, toTypeBoxed,
nextVersionTypeTypes.get(toTypeName.substring(1)) nextVersionTypeTypes.get(toTypeName.substring(1))
)); ));
break; case NULLABLE_OTHER -> deserializeMethod.addCode(buildStatementUpgradeNullableOtherField(
case NULLABLE_OTHER:
deserializeMethod.addCode(buildStatementUpgradeNullableOtherField(
versionPackage, versionPackage,
nextVersionPackage.get(), nextVersionPackage.get(),
number, number,
@ -1553,9 +1538,7 @@ public class SourcesGenerator {
toType, toType,
toTypeBoxed toTypeBoxed
)); ));
break; default -> throw new IllegalStateException("Unexpected value: " + currentFamily);
default:
throw new IllegalStateException("Unexpected value: " + currentFamily);
} }
} }
} }
@ -2633,7 +2616,7 @@ public class SourcesGenerator {
return deserializeMethod; return deserializeMethod;
} }
public static record NeededTypes( public record NeededTypes(
boolean nullableTypeNeeded, boolean nullableTypeNeeded,
boolean nextVersionNullableTypeNeeded, boolean nextVersionNullableTypeNeeded,
boolean arrayTypeNeeded, boolean arrayTypeNeeded,