From b3cac10554c23eb3f2e0c190ca1e0651aa8ee37d Mon Sep 17 00:00:00 2001 From: JFronny Date: Tue, 1 Nov 2022 09:55:08 +0100 Subject: [PATCH] Readme, support for enforcing no reflection --- README.md | 28 +++++++++++++++++++ gson-compile-core/build.gradle.kts | 4 --- gson-compile-example/build.gradle.kts | 3 ++ .../java/io/gitlab/jfronny/gson/Main.java | 3 -- .../processor/GsonCompileProcessor.java | 13 +++++---- 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7ffe3f --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Gson-Compile +A java annotation processor implementing automated type adapter generation for gson-comments. +The goal of this AP is to +- Support common features such as getters, setters, fields, constructors +- Support modern language features such as records +- Support json5 through gson-comments +- Be compile-time where possible (ideally compatible with proguard) + +## Currently supported +- Primitive types +- Adapter generation +- Utility methods +- Strict no-reflection enforcement via `-AgsonCompileNoReflect` + +## TODO +- Arrays +- Comments +- Nested serializable types +- Date +- Enums +- Lists +- Maps +- Queues +- Records +- Support for nested types from libraries +- Sets +- Static classes (for configs) +- GPrefer to bypass builder/constructor recovery \ No newline at end of file diff --git a/gson-compile-core/build.gradle.kts b/gson-compile-core/build.gradle.kts index 84f9a08..5f76c00 100644 --- a/gson-compile-core/build.gradle.kts +++ b/gson-compile-core/build.gradle.kts @@ -13,7 +13,3 @@ repositories { dependencies { api("io.gitlab.jfronny:commons-gson:2022.10.22+20-29-33") } - -tasks.getByName("test") { - useJUnitPlatform() -} \ No newline at end of file diff --git a/gson-compile-example/build.gradle.kts b/gson-compile-example/build.gradle.kts index 6b64260..ad6f175 100644 --- a/gson-compile-example/build.gradle.kts +++ b/gson-compile-example/build.gradle.kts @@ -17,3 +17,6 @@ dependencies { implementation(project(":gson-compile-core")) } +tasks.withType { + options.compilerArgs.add("-AgsonCompileNoReflect") +} \ No newline at end of file diff --git a/gson-compile-example/src/main/java/io/gitlab/jfronny/gson/Main.java b/gson-compile-example/src/main/java/io/gitlab/jfronny/gson/Main.java index 45c8083..a1ec9a8 100644 --- a/gson-compile-example/src/main/java/io/gitlab/jfronny/gson/Main.java +++ b/gson-compile-example/src/main/java/io/gitlab/jfronny/gson/Main.java @@ -1,13 +1,10 @@ package io.gitlab.jfronny.gson; import io.gitlab.jfronny.gson.compile.annotations.GSerializable; -import io.gitlab.jfronny.gson.stream.JsonReader; -import io.gitlab.jfronny.gson.stream.JsonWriter; public class Main { public static void main(String[] args) { System.out.println("Hello world!"); - //JsonReader } @GSerializable(generateAdapter = true) diff --git a/gson-compile-processor/src/main/java/io/gitlab/jfronny/gson/compile/processor/GsonCompileProcessor.java b/gson-compile-processor/src/main/java/io/gitlab/jfronny/gson/compile/processor/GsonCompileProcessor.java index 78eb380..f829f77 100644 --- a/gson-compile-processor/src/main/java/io/gitlab/jfronny/gson/compile/processor/GsonCompileProcessor.java +++ b/gson-compile-processor/src/main/java/io/gitlab/jfronny/gson/compile/processor/GsonCompileProcessor.java @@ -20,9 +20,9 @@ import java.lang.reflect.ParameterizedType; import java.util.*; import java.util.stream.Collectors; -//TODO support for records @SupportedSourceVersion(SourceVersion.RELEASE_17) @SupportedAnnotationTypes2({GSerializable.class}) +@SupportedOptions({"gsonCompileNoReflect"}) public class GsonCompileProcessor extends AbstractProcessor2 { private Messager message; private Types typeUtils; @@ -30,6 +30,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 { private Set seen; private ValueCreator valueCreator; private Elements elements; + private Map options; @Override public synchronized void init(ProcessingEnvironment processingEnv) { @@ -38,6 +39,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 { filer = processingEnv.getFiler(); typeUtils = processingEnv.getTypeUtils(); elements = processingEnv.getElementUtils(); + options = processingEnv.getOptions(); seen = new LinkedHashSet<>(); valueCreator = new ValueCreator(processingEnv); } @@ -188,9 +190,9 @@ public class GsonCompileProcessor extends AbstractProcessor2 { .addException(IOException.class) .returns(classType) .addCode(""" - try ($1T reader = new $1T(in)) { + try ($T reader = $T.HOLDER.getGson().newJsonReader(in)) { return read(reader); - }""", Const.GSON_READER) + }""", Const.GSON_READER, Const.CCORE) .build() ); @@ -227,9 +229,9 @@ public class GsonCompileProcessor extends AbstractProcessor2 { .addParameter(classType, "value") .addException(IOException.class) .addCode(""" - try ($1T writer = new $1T(out)) { + try ($T writer = $T.HOLDER.getGson().newJsonWriter(out)) { write(writer, value); - }""", Const.GSON_WRITER) + }""", Const.GSON_WRITER, Const.CCORE) .build() ); @@ -437,6 +439,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 { ); } else message.printMessage(Diagnostic.Kind.ERROR, "@JsonAdapter value must by TypeAdapter or TypeAdapterFactory reference.", prop.getElement()); } else { + message.printMessage(options.containsKey("gsonCompileNoReflect") ? Diagnostic.Kind.ERROR : Diagnostic.Kind.WARNING, "Falling back to adapter detection for unsupported type " + prop.getType(), prop.getElement()); //TODO handle known custom type adapters and return proper static class TypeName typeAdapterType = ParameterizedTypeName.get(Const.TYPE_ADAPTER, TypeName.get(prop.getType()).box()); CodeBlock.Builder block = CodeBlock.builder();