Unify code for finding applicable adapter

This commit is contained in:
Johannes Frohnmeyer 2022-11-01 11:44:39 +01:00
parent d4e056401d
commit 0eb5a6aa2f
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 8 additions and 19 deletions

View File

@ -17,6 +17,7 @@ import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import java.io.*;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@SupportedSourceVersion(SourceVersion.RELEASE_17)
@ -288,12 +289,12 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
for (Property.Field field : properties.fields) {
generateComments(field, code);
code.addStatement("writer.name($S)", getSerializedName(field));
generateWrite(field, spec, code, () -> code.add("value.$N", field.getCallableName()), typeVariables, otherAdapters);
withAdapter(field, spec, code, typeVariables, otherAdapters, adapter -> adapter.generateWrite(() -> code.add("value.$N", field.getCallableName())));
}
for (Property.Getter getter : properties.getters) {
generateComments(getter, code);
code.addStatement("writer.name($S)", getSerializedName(getter));
generateWrite(getter, spec, code, () -> code.add("value.$N()", getter.getCallableName()), typeVariables, otherAdapters);
withAdapter(getter, spec, code, typeVariables, otherAdapters, adapter -> adapter.generateWrite(() -> code.add("value.$N()", getter.getCallableName())));
}
code.addStatement("writer.endObject()");
@ -328,7 +329,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
for (Property<?> param : properties.names) {
if (param.getType().getKind().isPrimitive()) {
code.add("case $S -> $L = ", getSerializedName(param), Const.ARG_PREFIX + param.getName());
generateRead(param, spec, code, typeVariables, otherAdapters);
withAdapter(param, spec, code, typeVariables, otherAdapters, Adapter::generateRead);
code.add(";\n");
} else {
code.beginControlFlow("case $S ->", getSerializedName(param))
@ -336,7 +337,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
.addStatement("reader.nextNull()")
.addStatement("$L = null", Const.ARG_PREFIX + param.getName());
code.unindent().add("} else $L = ", Const.ARG_PREFIX + param.getName());
generateRead(param, spec, code, typeVariables, otherAdapters);
withAdapter(param, spec, code, typeVariables, otherAdapters, Adapter::generateRead);
code.add(";\n")
.endControlFlow();
}
@ -390,23 +391,11 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
}
}
private void generateWrite(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, Runnable writeGet, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) {
private void withAdapter(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters, Consumer<Adapter> action) {
for (Adapter adapter : ADAPTERS) {
adapter.hydrate(prop, klazz, code, typeVariables, otherAdapters);
if (adapter.applies()) {
adapter.generateWrite(writeGet);
}
adapter.dehydrate();
return;
}
message.printMessage(Diagnostic.Kind.ERROR, "Could not find applicable adapter for property " + prop);
}
private void generateRead(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) {
for (Adapter adapter : ADAPTERS) {
adapter.hydrate(prop, klazz, code, typeVariables, otherAdapters);
if (adapter.applies()) {
adapter.generateRead();
action.accept(adapter);
adapter.dehydrate();
return;
} else adapter.dehydrate();

View File

@ -8,7 +8,7 @@ public class StringAdapter extends Adapter {
@Override
public void generateWrite(Runnable writeGet) {
code.add("$T $L = ", typeVariables, argName);
code.add("$T $L = ", type, argName);
writeGet.run();
code.add(";\n");
code.beginControlFlow("if ($L == null)", argName)