This commit is contained in:
Johannes Frohnmeyer 2022-11-01 10:08:03 +01:00
parent b3cac10554
commit b6d000962c
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 37 additions and 5 deletions

View File

@ -11,10 +11,10 @@ The goal of this AP is to
- Adapter generation
- Utility methods
- Strict no-reflection enforcement via `-AgsonCompileNoReflect`
- Comments via `@GComment`
## TODO
- Arrays
- Comments
- Nested serializable types
- Date
- Enums

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 GComment {
String value() default "";
}

View File

@ -1,5 +1,7 @@
package io.gitlab.jfronny.gson;
import io.gitlab.jfronny.gson.annotations.SerializedName;
import io.gitlab.jfronny.gson.compile.annotations.GComment;
import io.gitlab.jfronny.gson.compile.annotations.GSerializable;
public class Main {
@ -9,7 +11,15 @@ public class Main {
@GSerializable(generateAdapter = true)
public static class ExamplePojo {
@SerializedName("someBalue")
public String someValue;
@GComment("Yes!")
public Boolean someBool;
@GComment("Halal")
@SerializedName("bingChiller")
public String getBass() {
return "Yes";
}
}
}

View File

@ -16,9 +16,11 @@ public class Const {
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.internal.bind", "JsonTreeReader");
public static final ClassName GSON_TREE_WRITER = ClassName.get("io.gitlab.jfronny.gson.internal.bind", "JsonTreeWriter");
static final ClassName JSON_ADAPTER = ClassName.get("io.gitlab.jfronny.gson.annotations", "JsonAdapter");
static final ClassName TYPE_TOKEN = ClassName.get("io.gitlab.jfronny.gson.reflect", "TypeToken");
static final ClassName GSON_TOKEN = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonToken");
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 GCOMMENT = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GComment");
public static final ClassName CCORE = ClassName.get("io.gitlab.jfronny.gson.compile.core", "CCore");
}

View File

@ -279,10 +279,12 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
code.addStatement("writer.beginObject()");
for (Property.Field field : properties.fields) {
generateComments(field, code);
code.addStatement("writer.name($S)", getSerializedName(field));
generateWrite(field, spec, code, "writer", "value.$N", typeVariables);
}
for (Property.Getter getter : properties.getters) {
generateComments(getter, code);
code.addStatement("writer.name($S)", getSerializedName(getter));
generateWrite(getter, spec, code, "writer", "value.$N()", typeVariables);
}
@ -370,6 +372,15 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
}
}
private void generateComments(Property<?> prop, CodeBlock.Builder code) {
for (AnnotationMirror annotation : prop.getAnnotations()) {
if (annotation.getAnnotationType().asElement().toString().equals(Const.GCOMMENT.toString())) {
String comment = (String) annotation.getElementValues().values().iterator().next().getValue();
code.addStatement("writer.comment($S)", comment);
}
}
}
private void generateWrite(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, String writer, String get, List<TypeVariableName> typeVariables) {
TypeMirror unboxed = unbox(prop.getType());
if (unbox(prop.getType()).getKind().isPrimitive() || prop.getType().toString().equals(String.class.getCanonicalName())) {
@ -542,7 +553,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
private static String getSerializedName(Property<?> property) {
for (AnnotationMirror annotationMirror : property.getAnnotations()) {
if (annotationMirror.getAnnotationType().asElement().toString().equals("com.google.gson.annotations.SerializedName")) {
if (annotationMirror.getAnnotationType().asElement().toString().equals(Const.SERIALIZED_NAME.toString())) {
return (String) annotationMirror.getElementValues().values().iterator().next().getValue();
}
}