fix(serialize-generator): initial support for some use cases with generics
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-04-24 13:11:54 +02:00
parent 78ec439ccb
commit 1a436343da
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 14 additions and 3 deletions

View File

@ -232,8 +232,8 @@ public class SerializeGeneratorProcessor extends AbstractProcessor2 {
TypeSpec.Builder spec = TypeSpec.classBuilder(toProcess.generatedClassName().simpleName())
.addOriginatingElement(toProcess.classElement())
.addModifiers(Modifier.PUBLIC)
.addTypeVariables(typeVariables);
.addModifiers(Modifier.PUBLIC);
// .addTypeVariables(typeVariables);
seen.put(toProcess.generatedClassName(), spec);
@ -245,7 +245,7 @@ public class SerializeGeneratorProcessor extends AbstractProcessor2 {
if (toProcess.generateAdapter()) {
generatedAdapters.add(processor.generateDelegatingAdapter(spec, classType, toProcess.generatedClassName()));
}
processor.generateSerialisation(spec, toProcess, typeVariables, other);
processor.generateSerialisation(spec, toProcess, typeVariables, other); //TODO fix type annotations
}
processor.generateAuxiliary(spec, classType, toProcess.configure());

View File

@ -102,6 +102,14 @@ public abstract class GProcessor {
}
}
protected TypeName nonGeneric(TypeName from) {
if (from instanceof ParameterizedTypeName pn) {
//TODO be more clever
return ParameterizedTypeName.get(pn.rawType, pn.typeArguments.stream().map(s -> WildcardTypeName.subtypeOf(Object.class)).toArray(TypeName[]::new)).annotated(pn.annotations);
}
return from;
}
public void generateDelegateToAdapter(TypeSpec.Builder spec, TypeName classType, TypeMirror adapter, boolean isStatic) {
if (!isStatic) {
spec.addField(
@ -110,6 +118,7 @@ public abstract class GProcessor {
.build()
);
}
classType = nonGeneric(classType);
spec.addMethod(
extension(MethodSpec.methodBuilder("deserialize"))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
@ -136,6 +145,7 @@ public abstract class GProcessor {
}
public void generateAuxiliary(TypeSpec.Builder spec, TypeName classType, TypeMirror configure) {
classType = nonGeneric(classType);
final UnaryOperator<CodeBlock.Builder> configureReader = cb -> {
if (configure != null) cb.addStatement("$T.configure(reader)", configure);
return cb;

View File

@ -20,6 +20,7 @@ public class InstanceProcessor extends GProcessor {
@Override
public ClassName generateDelegatingAdapter(TypeSpec.Builder spec, TypeName classType, ClassName generatedClassName) {
classType = nonGeneric(classType);
spec.addType(
TypeSpec.classBuilder("Adapter")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)