Use generic TypedNullable<?> instead of the specific implementation in records
This commit is contained in:
parent
5afe301750
commit
ea4f6db7a9
@ -12,6 +12,7 @@ public sealed interface ComputedType permits VersionedComputedType, ComputedType
|
||||
String getName();
|
||||
|
||||
TypeName getJTypeName(String basePackageName);
|
||||
TypeName getJTypeNameGeneric(String basePackageName);
|
||||
|
||||
TypeName getJSerializerName(String basePackageName);
|
||||
|
||||
|
@ -71,6 +71,11 @@ public final class ComputedTypeArrayFixed implements ComputedTypeArray {
|
||||
computedTypeSupplier.get(baseType).getJTypeName(basePackageName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(currentVersion.getSerializersPackage(basePackageName), "Array" + baseType + "Serializer");
|
||||
|
@ -3,6 +3,7 @@ package it.cavallium.datagen.plugin;
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.ParameterizedTypeName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import it.cavallium.datagen.NativeNullable;
|
||||
import it.cavallium.datagen.nativedata.ArrayInt52Serializer;
|
||||
import it.cavallium.datagen.nativedata.ArrayStringSerializer;
|
||||
import it.cavallium.datagen.nativedata.ArraybooleanSerializer;
|
||||
@ -96,6 +97,11 @@ public final class ComputedTypeArrayNative implements ComputedTypeArray {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return switch (baseType) {
|
||||
|
@ -88,6 +88,11 @@ public final class ComputedTypeArrayVersioned implements VersionedComputedType,
|
||||
computedTypeSupplier.get(baseType).getJTypeName(basePackageName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(baseType.version().getSerializersPackage(basePackageName), "Array" + baseType.type() + "Serializer");
|
||||
|
@ -2,6 +2,7 @@ package it.cavallium.datagen.plugin;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Objects;
|
||||
@ -101,6 +102,11 @@ public final class ComputedTypeBase implements VersionedComputedType {
|
||||
return ClassName.get(getVersion().getDataPackage(basePackageName), type.type());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(type.version().getSerializersPackage(basePackageName), type.type() + "Serializer");
|
||||
|
@ -93,6 +93,11 @@ public final class ComputedTypeCustom implements ComputedType {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJSerializerName(String basePackageName) {
|
||||
return ClassName.bestGuess(serializer);
|
||||
|
@ -46,6 +46,11 @@ public final class ComputedTypeNative implements ComputedType {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJSerializerName(String basePackageName) {
|
||||
return switch (type) {
|
||||
|
@ -2,7 +2,9 @@ package it.cavallium.datagen.plugin;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
import com.squareup.javapoet.ParameterizedTypeName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import it.cavallium.datagen.TypedNullable;
|
||||
import it.cavallium.datagen.nativedata.UpgradeUtil;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
@ -68,11 +70,21 @@ public final class ComputedTypeNullableFixed implements ComputedTypeNullable {
|
||||
return getJTypeNameOfVersion(currentVersion, basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeNameGenericOfVersion(currentVersion, basePackageName);
|
||||
}
|
||||
|
||||
private TypeName getJTypeNameOfVersion(ComputedVersion version, String basePackageName) {
|
||||
return ClassName.get(version.getDataNullablesPackage(basePackageName),
|
||||
"Nullable" + baseType);
|
||||
}
|
||||
|
||||
private TypeName getJTypeNameGenericOfVersion(ComputedVersion version, String basePackageName) {
|
||||
return ParameterizedTypeName.get(ClassName.get(TypedNullable.class),
|
||||
ClassName.get(version.getDataPackage(basePackageName), baseType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(currentVersion.getSerializersPackage(basePackageName),
|
||||
@ -102,7 +114,7 @@ public final class ComputedTypeNullableFixed implements ComputedTypeNullable {
|
||||
var upgraderInstance = getBase().getJUpgraderInstance(basePackageName);
|
||||
builder.add("new $T($T.upgradeNullable(", next.getJTypeName(basePackageName), UpgradeUtil.class);
|
||||
builder.add(content);
|
||||
builder.add(".value(), $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
|
||||
builder.add(".getNullable(), $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
|
||||
builder.add(")");
|
||||
return builder.build();
|
||||
}
|
||||
|
@ -99,6 +99,11 @@ public final class ComputedTypeNullableNative implements ComputedTypeNullable {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeName(basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return switch (baseType) {
|
||||
|
@ -2,7 +2,9 @@ package it.cavallium.datagen.plugin;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
import com.squareup.javapoet.ParameterizedTypeName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import it.cavallium.datagen.TypedNullable;
|
||||
import it.cavallium.datagen.nativedata.UpgradeUtil;
|
||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -81,11 +83,21 @@ public final class ComputedTypeNullableVersioned implements ComputedTypeNullable
|
||||
return getJTypeNameOfVersion(baseType.version(), basePackageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return getJTypeNameGenericOfVersion(baseType.version(), basePackageName);
|
||||
}
|
||||
|
||||
private TypeName getJTypeNameOfVersion(ComputedVersion version, String basePackageName) {
|
||||
return ClassName.get(version.getDataNullablesPackage(basePackageName),
|
||||
"Nullable" + baseType.type());
|
||||
}
|
||||
|
||||
private TypeName getJTypeNameGenericOfVersion(ComputedVersion version, String basePackageName) {
|
||||
return ParameterizedTypeName.get(ClassName.get(TypedNullable.class),
|
||||
ClassName.get(version.getDataPackage(basePackageName), baseType.type()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(baseType.version().getSerializersPackage(basePackageName),
|
||||
@ -115,7 +127,7 @@ public final class ComputedTypeNullableVersioned implements ComputedTypeNullable
|
||||
var upgraderInstance = getBase().getJUpgraderInstance(basePackageName);
|
||||
builder.add("new $T($T.upgradeNullable(", next.getJTypeName(basePackageName), UpgradeUtil.class);
|
||||
builder.add(content);
|
||||
builder.add(".value(), $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
|
||||
builder.add(".getNullable(), $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
|
||||
builder.add(")");
|
||||
return builder.build();
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package it.cavallium.datagen.plugin;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import it.cavallium.datagen.plugin.ComputedType.VersionedComputedType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -97,6 +98,11 @@ public final class ComputedTypeSuper implements VersionedComputedType {
|
||||
return ClassName.get(getVersion().getDataPackage(basePackageName), type.type());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeName getJTypeNameGeneric(String basePackageName) {
|
||||
return ClassName.get(getVersion().getDataPackage(basePackageName), type.type());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassName getJSerializerName(String basePackageName) {
|
||||
return ClassName.get(type.version().getSerializersPackage(basePackageName), type.type() + "Serializer");
|
||||
|
@ -69,7 +69,7 @@ public class SourcesGenerator {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SourcesGenerator.class);
|
||||
private static final boolean OVERRIDE_ALL_NULLABLE_METHODS = false;
|
||||
private static final String SERIAL_VERSION = "2";
|
||||
private static final String SERIAL_VERSION = "5";
|
||||
|
||||
private final DataModel dataModel;
|
||||
|
||||
|
@ -68,7 +68,7 @@ public class GenDataBaseX extends ClassGenerator {
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
|
||||
|
||||
base.getData().forEach((fieldName, fieldType) -> {
|
||||
var fieldTypeName = fieldType.getJTypeName(basePackageName);
|
||||
var fieldTypeName = fieldType.getJTypeNameGeneric(basePackageName);
|
||||
|
||||
var param = ParameterSpec
|
||||
.builder(fieldTypeName, fieldName)
|
||||
|
@ -61,7 +61,7 @@ public class GenDataSuperX extends ClassGenerator {
|
||||
Stream
|
||||
.concat(dataModel.getCommonInterfaceData(typeSuper), dataModel.getCommonInterfaceGetters(typeSuper))
|
||||
.forEach(superType -> {
|
||||
var returnType = superType.getValue().getJTypeName(basePackageName);
|
||||
var returnType = superType.getValue().getJTypeNameGeneric(basePackageName);
|
||||
var getter = MethodSpec
|
||||
.methodBuilder(superType.getKey())
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
|
||||
@ -73,7 +73,7 @@ public class GenDataSuperX extends ClassGenerator {
|
||||
});
|
||||
|
||||
dataModel.getCommonInterfaceData(typeSuper).forEach(superType -> {
|
||||
var returnType = superType.getValue().getJTypeName(basePackageName);
|
||||
var returnType = superType.getValue().getJTypeNameGeneric(basePackageName);
|
||||
|
||||
var setter = MethodSpec
|
||||
.methodBuilder("set" + StringUtils.capitalize(superType.getKey()))
|
||||
|
@ -88,8 +88,9 @@ public class GenNullableX extends ClassGenerator {
|
||||
classBuilder.addField(FieldSpec
|
||||
.builder(type, "NULL").addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).initializer("new $T(null)", type).build());
|
||||
|
||||
classBuilder.addSuperinterfaces(List.of(iNullableITypeClass, iNullableClass, typedNullable));
|
||||
|
||||
if (version.isCurrent()) {
|
||||
classBuilder.addSuperinterfaces(List.of(iNullableITypeClass, iNullableClass, typedNullable));
|
||||
|
||||
classBuilder.addMethod(MethodSpec
|
||||
.methodBuilder("of")
|
||||
|
@ -42,7 +42,7 @@ public class GenSerializerNullableX extends ClassGenerator {
|
||||
|
||||
private GeneratedClass generateTypeVersioned(ComputedVersion version, ComputedTypeNullable typeNullable) {
|
||||
ClassName serializerClassName = typeNullable.getJSerializerName(basePackageName);
|
||||
var typeNullableClassName = typeNullable.getJTypeName(basePackageName);
|
||||
var typeNullableClassName = typeNullable.getJTypeNameGeneric(basePackageName);
|
||||
|
||||
var classBuilder = TypeSpec.classBuilder(serializerClassName.simpleName());
|
||||
|
||||
@ -68,7 +68,7 @@ public class GenSerializerNullableX extends ClassGenerator {
|
||||
|
||||
method.addParameter(ParameterSpec.builder(SafeDataOutput.class, "out").build());
|
||||
method.addParameter(ParameterSpec
|
||||
.builder(typeNullable.getJTypeName(basePackageName), "data")
|
||||
.builder(typeNullable.getJTypeNameGeneric(basePackageName), "data")
|
||||
.addAnnotation(NotNull.class)
|
||||
.build());
|
||||
|
||||
|
@ -286,7 +286,7 @@ public class GenUpgraderBaseX extends ClassGenerator {
|
||||
boolean first = true;
|
||||
for (String contextParameter : contextParameters) {
|
||||
var fieldType = typeBase.getData().get(contextParameter);
|
||||
contextTypeClassBuilder.addRecordComponent(ParameterSpec.builder(fieldType.getJTypeName(basePackageName), contextParameter).build());
|
||||
contextTypeClassBuilder.addRecordComponent(ParameterSpec.builder(fieldType.getJTypeNameGeneric(basePackageName), contextParameter).build());
|
||||
|
||||
if (first) {
|
||||
first = false;
|
||||
|
@ -165,7 +165,7 @@ public class GenVersion extends ClassGenerator {
|
||||
var serializerClassName = type.getJSerializerName(basePackageName);
|
||||
|
||||
var fieldBuilder = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(DataSerializer.class),
|
||||
type.getJTypeName(basePackageName)
|
||||
type.getJTypeNameGeneric(basePackageName)
|
||||
), serializerFieldLocation.fieldName(), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
|
||||
fieldBuilder.initializer("new $T()", serializerClassName);
|
||||
classBuilder.addField(fieldBuilder.build());
|
||||
|
@ -34,10 +34,11 @@ public interface TypedNullable<T> extends NativeNullable<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
default @NotNull NativeNullable<? extends T> or(@NotNull NativeNullable<? extends T> fallback) {
|
||||
default @NotNull TypedNullable<T> or(@NotNull NativeNullable<? extends T> fallback) {
|
||||
var value = getNullable();
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
//noinspection unchecked
|
||||
return (TypedNullable<T>) fallback;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
|
@ -0,0 +1,87 @@
|
||||
package it.cavallium.datagen.nativedata;
|
||||
|
||||
import it.cavallium.datagen.TypedNullable;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestTypedNullable {
|
||||
@Test
|
||||
public void testNullableOr() {
|
||||
class TypeA {
|
||||
|
||||
}
|
||||
|
||||
class TypeAB extends TypeA {
|
||||
|
||||
}
|
||||
|
||||
var a = new TypedNullable<TypeA>() {
|
||||
|
||||
@Override
|
||||
public @Nullable TypeA getNullable() {
|
||||
return new TypeA();
|
||||
}
|
||||
};
|
||||
|
||||
var aNull = new TypedNullable<TypeA>() {
|
||||
|
||||
@Override
|
||||
public @Nullable TypeA getNullable() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
var ab = new TypedNullable<TypeAB>() {
|
||||
|
||||
@Override
|
||||
public @Nullable TypeAB getNullable() {
|
||||
return new TypeAB();
|
||||
}
|
||||
};
|
||||
|
||||
var abNull = new TypedNullable<TypeAB>() {
|
||||
|
||||
@Override
|
||||
public @Nullable TypeAB getNullable() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
a.or(ab).getNullable();
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(ab).getNullable();
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
a.or(abNull).getNullable();
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(abNull).getNullable();
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(abNull).orElse(new TypeA());
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(ab).orElse(new TypeA());
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(ab).orElse(new TypeAB());
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
aNull.or(abNull).orElse(new TypeAB());
|
||||
});
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> {
|
||||
abNull.or(abNull).orElse(new TypeAB());
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user