fix(serialize-generator): properly generate with SerializerFor
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
37ed2bf867
commit
f229dd496b
|
@ -146,6 +146,9 @@ public class SerializeGeneratorProcessor extends AbstractProcessor2 {
|
||||||
if (bld.nullSafe != null) throw new IllegalArgumentException("Duplicate annotation parameter: nullSafe");
|
if (bld.nullSafe != null) throw new IllegalArgumentException("Duplicate annotation parameter: nullSafe");
|
||||||
bld.nullSafe = (Boolean) value.getValue();
|
bld.nullSafe = (Boolean) value.getValue();
|
||||||
}
|
}
|
||||||
|
case "hierarchical" -> {
|
||||||
|
// not relevant for this processor
|
||||||
|
}
|
||||||
default -> throw new IllegalArgumentException("Unexpected annotation parameter: " + name);
|
default -> throw new IllegalArgumentException("Unexpected annotation parameter: " + name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
package io.gitlab.jfronny.commons.serialize.generator.adapter.impl;
|
package io.gitlab.jfronny.commons.serialize.generator.adapter.impl;
|
||||||
|
|
||||||
import com.squareup.javapoet.ClassName;
|
import com.squareup.javapoet.ClassName;
|
||||||
|
import com.squareup.javapoet.CodeBlock;
|
||||||
|
import com.squareup.javapoet.FieldSpec;
|
||||||
import com.squareup.javapoet.TypeName;
|
import com.squareup.javapoet.TypeName;
|
||||||
import io.gitlab.jfronny.commons.serialize.generator.AdapterRef;
|
import io.gitlab.jfronny.commons.serialize.generator.AdapterRef;
|
||||||
import io.gitlab.jfronny.commons.serialize.generator.adapter.AdapterAdapter;
|
import io.gitlab.jfronny.commons.serialize.generator.adapter.AdapterAdapter;
|
||||||
import io.gitlab.jfronny.commons.tuple.Tuple;
|
import io.gitlab.jfronny.commons.tuple.Tuple;
|
||||||
|
|
||||||
|
import javax.lang.model.element.Modifier;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
import javax.tools.Diagnostic;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class InferredAdapter extends AdapterAdapter<InferredAdapter.Hydrated> {
|
public class InferredAdapter extends AdapterAdapter<InferredAdapter.Hydrated> {
|
||||||
|
@ -29,13 +33,22 @@ public class InferredAdapter extends AdapterAdapter<InferredAdapter.Hydrated> {
|
||||||
var tmp = findTypeAdapterClass(type, refs);
|
var tmp = findTypeAdapterClass(type, refs);
|
||||||
// Ignore nullSafe for now
|
// Ignore nullSafe for now
|
||||||
if (tmp != null) {
|
if (tmp != null) {
|
||||||
|
message.printMessage(Diagnostic.Kind.WARNING, "Inferred type adapter for " + type + " to " + tmp.left() + " (nullSafe: " + tmp.right() + ")");
|
||||||
this.typeAdapterClass = tmp.left();
|
this.typeAdapterClass = tmp.left();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TypeName createAdapter(String typeAdapterName) {
|
protected String createAdapter(String typeAdapterName) {
|
||||||
return ClassName.get(typeAdapterClass);
|
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) {
|
private static Tuple<TypeElement, Boolean> findTypeAdapterClass(TypeMirror type, Set<AdapterRef> refs) {
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -57,6 +57,8 @@ public class Main {
|
||||||
public String getJoe() {
|
public String getJoe() {
|
||||||
return "A";
|
return "A";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CustomValue cv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GSerializable(configure = Configuration.class)
|
@GSerializable(configure = Configuration.class)
|
||||||
|
@ -126,6 +128,10 @@ public class Main {
|
||||||
public String maybe;
|
public String maybe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class CustomValue {
|
||||||
|
public String nein;
|
||||||
|
}
|
||||||
|
|
||||||
public enum EeE {
|
public enum EeE {
|
||||||
Yes, Yay, Aaee;
|
Yes, Yay, Aaee;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue