@With annotation instead of legacy code for @JsonAdapter
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-01-22 14:59:43 +01:00
parent 6805ff6b6f
commit 5294d82318
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 30 additions and 24 deletions

View File

@ -0,0 +1,9 @@
package io.gitlab.jfronny.gson.compile.annotations;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface GWith {
Class<?> serializer();
}

View File

@ -0,0 +1,16 @@
package io.gitlab.jfronny.gson.compile.example;
import io.gitlab.jfronny.gson.stream.JsonReader;
import io.gitlab.jfronny.gson.stream.JsonWriter;
import java.io.IOException;
public class ExampleAdapter {
public static void write(boolean bool, JsonWriter writer) throws IOException {
writer.value(!bool);
}
public static boolean read(JsonReader reader) throws IOException {
return !reader.nextBoolean();
}
}

View File

@ -58,6 +58,7 @@ public class Main {
@GSerializable(configure = Configuration.class)
public static class ExamplePojo2 {
@GComment("Yes!")
@GWith(serializer = ExampleAdapter.class)
public boolean primitive;
public ExamplePojo2[] recursiveTest;

View File

@ -10,15 +10,15 @@ public class Cl {
public static final ClassName GSON_READER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonReader");
public static final ClassName GSON_TREE_READER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonTreeReader");
public static final ClassName GSON_TREE_WRITER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonTreeWriter");
public static final ClassName JSON_ADAPTER = ClassName.get("io.gitlab.jfronny.gson.annotations", "JsonAdapter");
public static final ClassName TYPE_TOKEN = ClassName.get("io.gitlab.jfronny.gson.reflect", "TypeToken");
public static final ClassName GSON_TOKEN = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonToken");
public static final ClassName SERIALIZED_NAME = ClassName.get("io.gitlab.jfronny.gson.annotations", "SerializedName");
public static final ClassName GSON_SYNTAX_EXCEPTION = ClassName.get("io.gitlab.jfronny.gson", "JsonSyntaxException");
public static final ClassName GCOMMENT = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GComment");
public static final ClassName GISO8601UTILS = ClassName.get("io.gitlab.jfronny.gson.util", "ISO8601Utils");
public static final ClassName CCORE = ClassName.get("io.gitlab.jfronny.gson.compile.core", "CCore");
public static final ClassName GCOMMENT = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GComment");
public static final ClassName GWITH = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GWith");
public static final ClassName MANIFOLD_EXTENSION = ClassName.get("manifold.ext.rt.api", "Extension");
public static final ClassName MANIFOLD_THIS = ClassName.get("manifold.ext.rt.api", "This");

View File

@ -32,33 +32,13 @@ public class DeclaredAdapter extends AdapterAdapter<DeclaredAdapter.Hydrated> {
@Override
protected String createAdapter(String typeAdapterName) {
if (TypeHelper.isInstance(typeAdapterClass, Cl.TYPE_ADAPTER.toString(), typeUtils)) {
klazz.addField(
FieldSpec.builder(TypeName.get(typeAdapterClass), typeAdapterName)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer("new $T()", typeAdapterClass)
.build()
);
} else if (TypeHelper.isInstance(typeAdapterClass, Cl.TYPE_ADAPTER_FACTORY.toString(), typeUtils)) {
TypeName typeAdapterType = ParameterizedTypeName.get(Cl.TYPE_ADAPTER, TypeName.get(type).box());
CodeBlock.Builder block = CodeBlock.builder();
block.add("new $T().create($T.HOLDER.getGson(), ", typeAdapterClass, Cl.CCORE);
appendFieldTypeToken(false);
block.add(")");
klazz.addField(
FieldSpec.builder(typeAdapterType, typeAdapterName)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.initializer(block.build())
.build()
);
} else message.printMessage(Diagnostic.Kind.ERROR, "@JsonAdapter value must by TypeAdapter or TypeAdapterFactory reference.", sourceElement);
return typeAdapterName;
return TypeName.get(typeAdapterClass).toString();
}
private static DeclaredType findTypeAdapterClass(List<? extends AnnotationMirror> annotations) {
for (AnnotationMirror annotation : annotations) {
String typeName = annotation.getAnnotationType().toString();
if (typeName.equals(Cl.JSON_ADAPTER.toString())) {
if (typeName.equals(Cl.GWITH.toString())) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elements = annotation.getElementValues();
if (!elements.isEmpty()) {
AnnotationValue value = elements.values().iterator().next();