Code cleanup

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

View File

@ -1285,65 +1285,62 @@ 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(
"Deleted $$field$$" + currentVarNumber.getInt(removeDataTransformation.from) + "$$" "Deleted $$field$$" + currentVarNumber.getInt(removeDataTransformation.from) + "$$"
+ removeDataTransformation.from); + removeDataTransformation.from);
currentVarNumber.addTo(removeDataTransformation.from, 1); currentVarNumber.addTo(removeDataTransformation.from, 1);
currentVarTypeName.remove(removeDataTransformation.from); currentVarTypeName.remove(removeDataTransformation.from);
currentVarTypeClass.remove(removeDataTransformation.from); currentVarTypeClass.remove(removeDataTransformation.from);
currentVarFamily.remove(removeDataTransformation.from); currentVarFamily.remove(removeDataTransformation.from);
currentVarUpgraded.remove(removeDataTransformation.from); currentVarUpgraded.remove(removeDataTransformation.from);
currentVarDeleted.add(removeDataTransformation.from); currentVarDeleted.add(removeDataTransformation.from);
}
Objects.requireNonNull(currentTransformedFieldTypes.remove(
removeDataTransformation.transformClass + "." + removeDataTransformation.from));
break;
case "move-data":
var moveDataTransformation = (MoveDataConfiguration) transformation;
{
currentVarNumber.addTo(moveDataTransformation.to, 1);
currentVarTypeName.put(moveDataTransformation.to,
Objects.requireNonNull(currentVarTypeName.get(moveDataTransformation.from))
);
currentVarTypeClass.put(moveDataTransformation.to,
Objects.requireNonNull(currentVarTypeClass.get(moveDataTransformation.from))
);
currentVarFamily.put(moveDataTransformation.to,
Objects.requireNonNull(currentVarFamily.get(moveDataTransformation.from))
);
if (currentVarUpgraded.remove(moveDataTransformation.from)) {
currentVarUpgraded.add(moveDataTransformation.to);
} }
currentVarDeleted.remove(moveDataTransformation.to); Objects.requireNonNull(currentTransformedFieldTypes.remove(
deserializeMethod.addStatement( removeDataTransformation.transformClass + "." + removeDataTransformation.from));
"var $$field$$" + currentVarNumber.getInt(moveDataTransformation.to) + "$$"
+ moveDataTransformation.to + " = $$field$$" + currentVarNumber.getInt(
moveDataTransformation.from) + "$$" + moveDataTransformation.from);
} }
{ case "move-data" -> {
deserializeMethod.addComment( var moveDataTransformation = (MoveDataConfiguration) transformation;
"Deleted $$field$$" + currentVarNumber.getInt(moveDataTransformation.from) + "$$" {
+ moveDataTransformation.from); currentVarNumber.addTo(moveDataTransformation.to, 1);
currentVarNumber.addTo(moveDataTransformation.from, 1); currentVarTypeName.put(moveDataTransformation.to,
currentVarTypeName.remove(moveDataTransformation.from); Objects.requireNonNull(currentVarTypeName.get(moveDataTransformation.from))
currentVarTypeClass.remove(moveDataTransformation.from); );
currentVarFamily.remove(moveDataTransformation.from); currentVarTypeClass.put(moveDataTransformation.to,
currentVarUpgraded.remove(moveDataTransformation.from); Objects.requireNonNull(currentVarTypeClass.get(moveDataTransformation.from))
currentVarDeleted.add(moveDataTransformation.from); );
currentVarFamily.put(moveDataTransformation.to,
Objects.requireNonNull(currentVarFamily.get(moveDataTransformation.from))
);
if (currentVarUpgraded.remove(moveDataTransformation.from)) {
currentVarUpgraded.add(moveDataTransformation.to);
}
currentVarDeleted.remove(moveDataTransformation.to);
deserializeMethod.addStatement(
"var $$field$$" + currentVarNumber.getInt(moveDataTransformation.to) + "$$"
+ moveDataTransformation.to + " = $$field$$" + currentVarNumber.getInt(
moveDataTransformation.from) + "$$" + moveDataTransformation.from);
}
{
deserializeMethod.addComment(
"Deleted $$field$$" + currentVarNumber.getInt(moveDataTransformation.from) + "$$"
+ moveDataTransformation.from);
currentVarNumber.addTo(moveDataTransformation.from, 1);
currentVarTypeName.remove(moveDataTransformation.from);
currentVarTypeClass.remove(moveDataTransformation.from);
currentVarFamily.remove(moveDataTransformation.from);
currentVarUpgraded.remove(moveDataTransformation.from);
currentVarDeleted.add(moveDataTransformation.from);
}
currentTransformedFieldTypes.put(
moveDataTransformation.transformClass + "." + moveDataTransformation.to,
Objects.requireNonNull(currentTransformedFieldTypes.remove(
moveDataTransformation.transformClass + "." + moveDataTransformation.from))
);
} }
case "upgrade-data" -> {
currentTransformedFieldTypes.put(
moveDataTransformation.transformClass + "." + moveDataTransformation.to,
Objects.requireNonNull(currentTransformedFieldTypes.remove(
moveDataTransformation.transformClass + "." + moveDataTransformation.from))
);
break;
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,26 +1362,21 @@ 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, Modifier.FINAL
Modifier.FINAL );
);
fieldSpec.initializer("($T) new $T()", dataUpgraderType, dataUpgraderClass); fieldSpec.initializer("($T) new $T()", dataUpgraderType, dataUpgraderClass);
upgraderClass.addField(fieldSpec.build()); upgraderClass.addField(fieldSpec.build());
return fieldName; return fieldName;
} }
); );
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,40 +1394,47 @@ 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,
() -> "Type \"" + newTypeName + "\" has no type family!" Objects.requireNonNull(typeFamily.get(newTypeName),
)); () -> "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,
Modifier.FINAL Modifier.FINAL
); );
fieldSpec.initializer("($T) new $T()", dataInitializerType, dataInitializerClass); fieldSpec.initializer("($T) new $T()", dataInitializerType, dataInitializerClass);
upgraderClass.addField(fieldSpec.build()); upgraderClass.addField(fieldSpec.build());
return fieldName; return fieldName;
@ -1445,17 +1443,15 @@ 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());
} }
} }
deserializeMethod.addCode("\n"); deserializeMethod.addCode("\n");
@ -1491,71 +1487,58 @@ 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,
versionPackage, key,
nextVersionPackage.get(), toTypeName,
number, toFamily,
key, toType,
toTypeName, toTypeBoxed
toFamily, ));
toType, case I_TYPE_ARRAY -> deserializeMethod.addCode(buildStatementUpgradeITypeArrayField(
toTypeBoxed versionPackage,
)); nextVersionPackage.get(),
break; number,
case I_TYPE_ARRAY: key,
deserializeMethod.addCode(buildStatementUpgradeITypeArrayField( toTypeName,
versionPackage, toFamily,
nextVersionPackage.get(), toType,
number, toTypeBoxed
key, ));
toTypeName, case NULLABLE_BASIC -> deserializeMethod.addCode(buildStatementUpgradeNullableBasicField(
toFamily, versionPackage,
toType, nextVersionPackage.get(),
toTypeBoxed number,
)); key,
break; toTypeName,
case NULLABLE_BASIC: toFamily,
deserializeMethod.addCode(buildStatementUpgradeNullableBasicField( toType,
versionPackage, toTypeBoxed,
nextVersionPackage.get(), nextVersionTypeTypes.get(toTypeName.substring(1))
number, ));
key, case NULLABLE_GENERIC -> deserializeMethod.addCode(buildStatementUpgradeNullableGenericField(
toTypeName, versionPackage,
toFamily, nextVersionPackage.get(),
toType, number,
toTypeBoxed, key,
nextVersionTypeTypes.get(toTypeName.substring(1)) toTypeName,
)); toFamily,
break; toType,
case NULLABLE_GENERIC: toTypeBoxed,
deserializeMethod.addCode(buildStatementUpgradeNullableGenericField( nextVersionTypeTypes.get(toTypeName.substring(1))
versionPackage, ));
nextVersionPackage.get(), case NULLABLE_OTHER -> deserializeMethod.addCode(buildStatementUpgradeNullableOtherField(
number, versionPackage,
key, nextVersionPackage.get(),
toTypeName, number,
toFamily, key,
toType, toTypeName,
toTypeBoxed, toFamily,
nextVersionTypeTypes.get(toTypeName.substring(1)) toType,
)); toTypeBoxed
break; ));
case NULLABLE_OTHER: default -> throw new IllegalStateException("Unexpected value: " + currentFamily);
deserializeMethod.addCode(buildStatementUpgradeNullableOtherField(
versionPackage,
nextVersionPackage.get(),
number,
key,
toTypeName,
toFamily,
toType,
toTypeBoxed
));
break;
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,