feat(serialize-generator): support @Ignore annotation
This commit is contained in:
parent
571bc58f80
commit
2b4b778a4a
@ -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));
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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())) {
|
||||
|
@ -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");
|
||||
|
@ -37,7 +37,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 +57,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));
|
||||
@ -94,6 +97,7 @@ public class StaticProcessor 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", self.getTypeName(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
|
||||
}
|
||||
@ -104,6 +108,7 @@ public class StaticProcessor extends GProcessor {
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.beginControlFlow("switch (reader.nextName())");
|
||||
for (Property<?> param : properties.names) {
|
||||
if (isIgnored(param) && getAlternative(properties, param) == null) continue;
|
||||
if (param.getType().getKind().isPrimitive()) {
|
||||
code.add("case $S -> $T.$N = ", getSerializedName(param), self.getTypeName(), param.getName());
|
||||
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user