Important bugfixes

This commit is contained in:
Andrea Cavalli 2021-06-07 16:23:01 +02:00
parent 9340babe61
commit a0e79ab1d9

View File

@ -711,6 +711,10 @@ public class SourcesGenerator {
for (Entry<String, CustomTypesConfiguration> entry : versionConfiguration.customTypes.entrySet()) { for (Entry<String, CustomTypesConfiguration> entry : versionConfiguration.customTypes.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
CustomTypesConfiguration customTypeConfiguration = entry.getValue(); CustomTypesConfiguration customTypeConfiguration = entry.getValue();
Optional<CustomTypesConfiguration> nextVersionCustomTypeConfiguration = nextVersion
.map(s -> Objects.requireNonNull(configuration.versions.get(s).customTypes.get(key),
() -> "Custom type " + key + " not found in version " + s
));
typeOptionalSerializers.put(key, ClassName.bestGuess(customTypeConfiguration.serializer)); typeOptionalSerializers.put(key, ClassName.bestGuess(customTypeConfiguration.serializer));
typeSerializeStatement.put(key, typeSerializeStatement.put(key,
new SerializeCodeBlockGenerator(CodeBlock new SerializeCodeBlockGenerator(CodeBlock
@ -727,8 +731,8 @@ public class SourcesGenerator {
typeMustGenerateSerializer.put(key, false); typeMustGenerateSerializer.put(key, false);
typeTypes.put(key, ClassName.bestGuess(customTypeConfiguration.javaClass)); typeTypes.put(key, ClassName.bestGuess(customTypeConfiguration.javaClass));
typeFamily.put(key, Family.OTHER); typeFamily.put(key, Family.OTHER);
if (nextVersion.isPresent()) { if (nextVersionCustomTypeConfiguration.isPresent()) {
nextVersionTypeTypes.put(key, ClassName.bestGuess(customTypeConfiguration.javaClass)); nextVersionTypeTypes.put(key, ClassName.bestGuess(nextVersionCustomTypeConfiguration.get().javaClass));
nextVersionTypeFamily.put(key, Family.OTHER); nextVersionTypeFamily.put(key, Family.OTHER);
} }
@ -1342,46 +1346,46 @@ public class SourcesGenerator {
.get(newDataTransformation.to); .get(newDataTransformation.to);
TypeName newType = nextVersionTypeTypes.get(newTypeName); TypeName newType = nextVersionTypeTypes.get(newTypeName);
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 = "DATA_INITIALIZER_" + nextDataInitializerInstanceFieldId.getAndIncrement();
var fieldSpec = FieldSpec var fieldSpec = FieldSpec
.builder(dataInitializerType, .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;
} }
); );
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; break;
default: default:
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Unknown transform type: " + transformation.getTransformName()); "Unknown transform type: " + transformation.getTransformName());
@ -1495,6 +1499,12 @@ public class SourcesGenerator {
if (!isFirst.getAndSet(false)) { if (!isFirst.getAndSet(false)) {
deserializeMethod.addCode(", "); deserializeMethod.addCode(", ");
} }
if (currentVarNumber.getInt(field) < 0) {
throw new IllegalStateException(
"Field " + field + " in class " + type + " has an invalid var number ("
+ currentVarNumber.getInt(field) + ") after upgrading from version " + version
+ " to version " + nextVersion.orElse("---"));
}
deserializeMethod.addCode("$$field$$" + currentVarNumber.getInt(field) + "$$" + field); deserializeMethod.addCode("$$field$$" + currentVarNumber.getInt(field) + "$$" + field);
} }
deserializeMethod.addStatement(")"); deserializeMethod.addStatement(")");
@ -2612,12 +2622,17 @@ public class SourcesGenerator {
.add("$T.Nullable" + type + "SerializerInstance.deserialize(dataInput)", versionClassType).build()); .add("$T.Nullable" + type + "SerializerInstance.deserialize(dataInput)", versionClassType).build());
typeMustGenerateSerializer.put("-" + type, true); typeMustGenerateSerializer.put("-" + type, true);
typeTypes.put("-" + type, ClassName.get(joinPackage(versionPackage, "data.nullables"), "Nullable" + type)); typeTypes.put("-" + type, ClassName.get(joinPackage(versionPackage, "data.nullables"), "Nullable" + type));
}
if (typeFamily != null) {
typeFamily.put("-" + type, nullableFamily); typeFamily.put("-" + type, nullableFamily);
} }
if (nextVersionNullableTypeNeeded) { if (nextVersionNullableTypeNeeded) {
assert nextVersionTypeTypes != null; assert nextVersionTypeTypes != null;
assert nextVersionTypeFamily != null; assert nextVersionTypeFamily != null;
nextVersionTypeTypes.put("-" + type, ClassName.get(joinPackage(nextVersionPackage.orElseThrow(), "data.nullables"), "Nullable" + type)); nextVersionTypeTypes.put("-" + type, ClassName.get(joinPackage(nextVersionPackage.orElseThrow(), "data.nullables"), "Nullable" + type));
}
if (nextVersionTypeFamily != null) {
nextVersionTypeFamily.put("-" + type, nullableFamily); nextVersionTypeFamily.put("-" + type, nullableFamily);
} }
@ -2653,12 +2668,17 @@ public class SourcesGenerator {
type type
); );
typeTypes.put("§" + type, getImmutableArrayType(arrayClassName.get())); typeTypes.put("§" + type, getImmutableArrayType(arrayClassName.get()));
}
if (typeFamily != null) {
typeFamily.put("§" + type, Family.I_TYPE_ARRAY); typeFamily.put("§" + type, Family.I_TYPE_ARRAY);
} }
if (nextVersionArrayTypeNeeded) { if (nextVersionArrayTypeNeeded) {
assert nextVersionTypeTypes != null; assert nextVersionTypeTypes != null;
assert nextVersionTypeFamily != null; assert nextVersionTypeFamily != null;
nextVersionTypeTypes.put("§" + type, getImmutableArrayType(nextArrayClassName.get())); nextVersionTypeTypes.put("§" + type, getImmutableArrayType(nextArrayClassName.get()));
}
if (nextVersionTypeFamily != null) {
nextVersionTypeFamily.put("§" + type, Family.I_TYPE_ARRAY); nextVersionTypeFamily.put("§" + type, Family.I_TYPE_ARRAY);
} }