fix(serialize-generator): properly generate with SerializerFor
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-04-24 14:55:38 +02:00
parent 37ed2bf867
commit f229dd496b
Signed by: Johannes
GPG Key ID: E76429612C2929F4
4 changed files with 46 additions and 2 deletions

View File

@ -146,6 +146,9 @@ public class SerializeGeneratorProcessor extends AbstractProcessor2 {
if (bld.nullSafe != null) throw new IllegalArgumentException("Duplicate annotation parameter: nullSafe");
bld.nullSafe = (Boolean) value.getValue();
}
case "hierarchical" -> {
// not relevant for this processor
}
default -> throw new IllegalArgumentException("Unexpected annotation parameter: " + name);
}
});

View File

@ -1,13 +1,17 @@
package io.gitlab.jfronny.commons.serialize.generator.adapter.impl;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.TypeName;
import io.gitlab.jfronny.commons.serialize.generator.AdapterRef;
import io.gitlab.jfronny.commons.serialize.generator.adapter.AdapterAdapter;
import io.gitlab.jfronny.commons.tuple.Tuple;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import java.util.Set;
public class InferredAdapter extends AdapterAdapter<InferredAdapter.Hydrated> {
@ -29,13 +33,22 @@ public class InferredAdapter extends AdapterAdapter<InferredAdapter.Hydrated> {
var tmp = findTypeAdapterClass(type, refs);
// Ignore nullSafe for now
if (tmp != null) {
message.printMessage(Diagnostic.Kind.WARNING, "Inferred type adapter for " + type + " to " + tmp.left() + " (nullSafe: " + tmp.right() + ")");
this.typeAdapterClass = tmp.left();
}
}
@Override
protected TypeName createAdapter(String typeAdapterName) {
return ClassName.get(typeAdapterClass);
protected String createAdapter(String typeAdapterName) {
CodeBlock.Builder block = CodeBlock.builder();
block.add("new $T()", typeAdapterClass);
klazz.addField(
FieldSpec.builder(ClassName.get(typeAdapterClass), typeAdapterName)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer(block.build())
.build()
);
return typeAdapterName;
}
private static Tuple<TypeElement, Boolean> findTypeAdapterClass(TypeMirror type, Set<AdapterRef> refs) {

View File

@ -0,0 +1,22 @@
package io.gitlab.jfronny.commons.serialize.generator.test;
import io.gitlab.jfronny.commons.serialize.MalformedDataException;
import io.gitlab.jfronny.commons.serialize.SerializeReader;
import io.gitlab.jfronny.commons.serialize.SerializeWriter;
import io.gitlab.jfronny.commons.serialize.databind.api.SerializerFor;
import io.gitlab.jfronny.commons.serialize.databind.api.TypeAdapter;
@SerializerFor(targets = Main.CustomValue.class)
public class ExampleAdapter2 extends TypeAdapter<Main.CustomValue> {
@Override
public <TEx extends Exception, Writer extends SerializeWriter<TEx, Writer>> void serialize(Main.CustomValue value, Writer writer) throws TEx, MalformedDataException {
writer.value(value.nein);
}
@Override
public <TEx extends Exception, Reader extends SerializeReader<TEx, Reader>> Main.CustomValue deserialize(Reader reader) throws TEx, MalformedDataException {
Main.CustomValue cv = new Main.CustomValue();
cv.nein = reader.nextString();
return cv;
}
}

View File

@ -57,6 +57,8 @@ public class Main {
public String getJoe() {
return "A";
}
public CustomValue cv;
}
@GSerializable(configure = Configuration.class)
@ -126,6 +128,10 @@ public class Main {
public String maybe;
}
public static class CustomValue {
public String nein;
}
public enum EeE {
Yes, Yay, Aaee;
}