Compare commits

...

2 Commits

Author SHA1 Message Date
Johannes Frohnmeyer d129e6950d
feat(serialize-generator): support getters and setters in StaticProcessor
ci/woodpecker/push/woodpecker Pipeline was successful Details
2024-04-23 22:48:02 +02:00
Johannes Frohnmeyer 2b4b778a4a
feat(serialize-generator): support @Ignore annotation 2024-04-23 22:27:39 +02:00
7 changed files with 100 additions and 19 deletions

View File

@ -124,10 +124,10 @@ public class ValueCreator {
ExecutableElement noArgConstructor = null;
List<ExecutableElement> constructors = ElementFilter.constructorsIn(klazz.getEnclosedElements());
if (constructors.size() == 1) {
ExecutableElement constructor = constructors.get(0);
ExecutableElement constructor = constructors.getFirst();
if (constructor.getParameters().isEmpty()) {
noArgConstructor = constructor;
constructors.remove(0);
constructors.removeFirst();
}
}
for (ExecutableElement method : ElementFilter.methodsIn(klazz.getEnclosedElements())) {
@ -142,7 +142,7 @@ public class ValueCreator {
if (noArgConstructor != null) return noArgConstructor;
else throw new ElementException("Lacking constructor or factory method", klazz);
}
if (constructors.size() == 1) return constructors.get(0);
if (constructors.size() == 1) return constructors.getFirst();
if (noArgConstructor != null) constructors.add(noArgConstructor);
if (preferAnnotation != null) {
List<ExecutableElement> preferred = new ArrayList<>();
@ -151,7 +151,7 @@ public class ValueCreator {
preferred.add(constructor);
}
}
if (preferred.size() == 1) return preferred.get(0);
if (preferred.size() == 1) return preferred.getFirst();
}
List<ElementException.Message> messages = new ArrayList<>();
messages.add(new ElementException.Message("More than one constructor or factory method found.", klazz));

View File

@ -11,6 +11,7 @@ public class Cl {
public static final ClassName EMULATED_WRITER = ClassName.get("io.gitlab.jfronny.commons.serialize.emulated", "EmulatedWriter");
public static final ClassName GSON_TOKEN = ClassName.get("io.gitlab.jfronny.commons.serialize", "Token");
public static final ClassName SERIALIZED_NAME = ClassName.get("io.gitlab.jfronny.commons.serialize.annotations", "SerializedName");
public static final ClassName IGNORE = ClassName.get("io.gitlab.jfronny.commons.serialize.annotations", "Ignore");
public static final ClassName MALFORMED_DATA_EXCEPTION = ClassName.get("io.gitlab.jfronny.commons.serialize", "MalformedDataException");
public static final ClassName TRANSPORT = ClassName.get("io.gitlab.jfronny.commons.serialize", "Transport");

View File

@ -4,6 +4,7 @@ import com.squareup.javapoet.*;
import io.gitlab.jfronny.commons.serialize.generator.Cl;
import io.gitlab.jfronny.commons.serialize.generator.SerializableClass;
import io.gitlab.jfronny.commons.serialize.generator.core.value.ElementException;
import io.gitlab.jfronny.commons.serialize.generator.core.value.Properties;
import io.gitlab.jfronny.commons.serialize.generator.core.value.Property;
import io.gitlab.jfronny.commons.serialize.generator.core.value.ValueCreator;
@ -48,6 +49,25 @@ public abstract class GProcessor {
public abstract ClassName generateDelegatingAdapter(TypeSpec.Builder spec, TypeName classType, ClassName generatedClassName);
public abstract void generateSerialisation(TypeSpec.Builder spec, SerializableClass self, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) throws ElementException;
protected boolean isIgnored(Property<?> property) {
for (AnnotationMirror annotationMirror : property.getAnnotations()) {
if (annotationMirror.getAnnotationType().asElement().toString().equals(Cl.IGNORE.toString())) {
return true;
}
}
return false;
}
protected Property<?> getAlternative(Properties properties, Property<?> ignored) {
List<List<? extends Property<?>>> toSearch = List.of(properties.params, properties.getters, properties.setters, properties.fields);
for (List<? extends Property<?>> search : toSearch) {
Property<?> prop = Properties.findName(search, ignored);
if (prop == null || isIgnored(prop)) continue;
return prop;
}
return null;
}
protected String getSerializedName(Property<?> property) {
for (AnnotationMirror annotationMirror : property.getAnnotations()) {
if (annotationMirror.getAnnotationType().asElement().toString().equals(Cl.SERIALIZED_NAME.toString())) {

View File

@ -73,7 +73,9 @@ public class InstanceProcessor extends GProcessor {
code.addStatement("writer.beginObject()");
for (Property.Field param : properties.fields) {
if (Properties.containsName(properties.getters, param)) continue;
Property<?> altGetter = Properties.findName(properties.getters, param);
if (altGetter != null && !isIgnored(altGetter)) continue;
if (isIgnored(param)) continue;
Runnable writeGet = () -> code.add("value.$N", param.getCallableName());
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
@ -91,6 +93,7 @@ public class InstanceProcessor extends GProcessor {
}
}
for (Property.Getter param : properties.getters) {
if (isIgnored(param)) continue;
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
@ -130,6 +133,7 @@ public class InstanceProcessor extends GProcessor {
boolean isEmpty = true;
for (Property<?> param : properties.names) {
if (isIgnored(param) && getAlternative(properties, param) == null) continue;
isEmpty = false;
code.addStatement("$T _$N = $L", param.getType(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
code.addStatement("boolean has_$N = false", param.getName());
@ -141,6 +145,7 @@ public class InstanceProcessor extends GProcessor {
.beginControlFlow("while (reader.hasNext())")
.beginControlFlow("switch (reader.nextName())");
for (Property<?> param : properties.names) {
if (isIgnored(param) && getAlternative(properties, param) == null) continue;
code.beginControlFlow("case $S ->", getSerializedName(param));
if (param.getType().getKind().isPrimitive()) {
code.add("_$N = ", param.getName());
@ -174,12 +179,13 @@ public class InstanceProcessor extends GProcessor {
}
code.add("$T builder = ", builder.getBuilderClass());
if (constructionSource.isConstructor()) {
code.add("new $T($L)", builder.getBuilderClass(), args.length() > 0 ? args.substring(2) : "");
code.add("new $T($L)", builder.getBuilderClass(), !args.isEmpty() ? args.substring(2) : "");
} else {
code.add("$T.$N($L)", creatorName, self.classElement().getSimpleName(), args.length() > 0 ? args.substring(2) : "");
code.add("$T.$N($L)", creatorName, self.classElement().getSimpleName(), !args.isEmpty() ? args.substring(2) : "");
}
code.add(";\n");
for (Property.Setter param : properties.builderParams) {
if (isIgnored(param)) continue;
code.addStatement("if (has_$N) builder.$N(_$N)", param.getName(), param.getCallableName(), param.getName());
}
code.addStatement("result = builder.$N()", builder.getBuildMethod().getSimpleName());
@ -189,17 +195,19 @@ public class InstanceProcessor extends GProcessor {
args.append(", _").append(param.getName());
}
if (constructionSource.isConstructor()) {
code.addStatement("result = new $T($L)", self.getTypeName(), args.length() > 0 ? args.substring(2) : "");
code.addStatement("result = new $T($L)", self.getTypeName(), !args.isEmpty() ? args.substring(2) : "");
} else {
code.addStatement("result = $T.$N($L)", creatorName, constructionSource.getConstructionElement().getSimpleName(), args.length() > 0 ? args.substring(2) : "");
code.addStatement("result = $T.$N($L)", creatorName, constructionSource.getConstructionElement().getSimpleName(), !args.isEmpty() ? args.substring(2) : "");
}
}
for (Property.Setter setter : properties.setters) {
if (isIgnored(setter)) continue;
code.addStatement("if (has_$N) result.$N(_$N)", setter.getName(), setter.getCallableName(), setter.getName());
}
for (Property.Field field : properties.fields) {
if (Properties.containsName(properties.setters, field)) continue;
if (Properties.containsName(properties.params, field)) continue;
if (isIgnored(field)) continue;
code.addStatement("if (has_$N) result.$N = _$N", field.getName(), field.getCallableName(), field.getName());
}
code.addStatement("return result");

View File

@ -9,7 +9,9 @@ import io.gitlab.jfronny.commons.serialize.generator.core.value.*;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class StaticProcessor extends GProcessor {
@ -37,7 +39,9 @@ public class StaticProcessor extends GProcessor {
CodeBlock.Builder code = CodeBlock.builder();
code.addStatement("writer.beginObject()");
for (Property.Field param : properties.fields) {
if (Properties.containsName(properties.getters, param)) continue;
Property<?> altGetter = Properties.findName(properties.getters, param);
if (altGetter != null && !isIgnored(altGetter)) continue;
if (isIgnored(param)) continue;
Runnable writeGet = () -> code.add("$T.$N", self.getTypeName(), param.getCallableName());
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
@ -55,6 +59,7 @@ public class StaticProcessor extends GProcessor {
}
}
for (Property.Getter param : properties.getters) {
if (isIgnored(param)) continue;
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
@ -92,10 +97,23 @@ public class StaticProcessor extends GProcessor {
.addStatement("return")
.endControlFlow();
Map<Property<?>, Property<?>> altMap = new HashMap<>();
boolean isEmpty = true;
for (Property<?> param : properties.names) {
if (isIgnored(param)) {
Property<?> alt = getAlternative(properties, param);
altMap.put(param, alt);
if (alt == null) continue;
param = alt;
}
isEmpty = false;
code.addStatement("$T.$N = $L", self.getTypeName(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
if (param instanceof Property.Field) {
code.addStatement("$T.$N = $L", self.getTypeName(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
} else {
code.addStatement("$T _$N = $L", param.getType(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
code.addStatement("boolean has_$N = false", param.getName());
}
}
if (isEmpty) {
code.addStatement("reader.skipValue()");
@ -104,20 +122,31 @@ public class StaticProcessor extends GProcessor {
.beginControlFlow("while (reader.hasNext())")
.beginControlFlow("switch (reader.nextName())");
for (Property<?> param : properties.names) {
if (altMap.containsKey(param)) {
param = altMap.get(param);
if (param == null) continue;
}
code.beginControlFlow("case $S ->", getSerializedName(param));
if (param.getType().getKind().isPrimitive()) {
code.add("case $S -> $T.$N = ", getSerializedName(param), self.getTypeName(), param.getName());
if (param instanceof Property.Field) code.add("$T.$N = ", self.getTypeName(), param.getName());
else code.add("_$N = ", param.getName());
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n");
} else {
code.beginControlFlow("case $S ->", getSerializedName(param))
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
.addStatement("reader.nextNull()")
.addStatement("$T.$N = null", self.getTypeName(), param.getName());
code.unindent().add("} else $T.$N = ", self.getTypeName(), param.getName());
code.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
.addStatement("reader.nextNull()");
if (param instanceof Property.Field) {
code.addStatement("$T.$N = null", self.getTypeName(), param.getName());
code.unindent().add("} else $T.$N = ", self.getTypeName(), param.getName());
} else {
code.addStatement("_$N = null", param.getName());
code.unindent().add("} else _$N = ", param.getName());
}
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n")
.endControlFlow();
code.add(";\n");
}
if (!(param instanceof Property.Field)) code.addStatement("has_$N = true", param.getName());
code.endControlFlow();
}
code.add("default -> ")
.addStatement("reader.skipValue()");
@ -125,6 +154,11 @@ public class StaticProcessor extends GProcessor {
code.endControlFlow()
.endControlFlow()
.addStatement("reader.endObject()");
for (Property.Setter setter : properties.setters) {
if (isIgnored(setter)) continue;
code.addStatement("if (has_$N) $T.$N(_$N)", setter.getName(), self.getTypeName(), setter.getCallableName(), setter.getName());
}
}
spec.addMethod(extension(MethodSpec.methodBuilder("deserialize"))

View File

@ -2,6 +2,7 @@ package io.gitlab.jfronny.commons.serialize.generator.test;
import io.gitlab.jfronny.commons.serialize.SerializeReader;
import io.gitlab.jfronny.commons.serialize.SerializeWriter;
import io.gitlab.jfronny.commons.serialize.annotations.Ignore;
import io.gitlab.jfronny.commons.serialize.annotations.SerializedName;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GComment;
import io.gitlab.jfronny.commons.serialize.generator.annotations.GPrefer;
@ -116,6 +117,15 @@ public class Main {
}
}
@GSerializable
public static class WithIgnored {
@Ignore
public String yes;
public String no;
@Ignore
public String maybe;
}
public enum EeE {
Yes, Yay, Aaee;
}

View File

@ -6,4 +6,12 @@ import io.gitlab.jfronny.commons.serialize.generator.annotations.GSerializable;
public class Static {
public boolean nonStatic;
public static boolean joe;
public static void setNine(boolean nine) {
joe = !nine;
}
public static String getSerde() {
return "serde? " + joe;
}
}