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