diff --git a/commons-serialize-generator-core/src/main/java/io/gitlab/jfronny/commons/serialize/generator/core/value/ValueCreator.java b/commons-serialize-generator-core/src/main/java/io/gitlab/jfronny/commons/serialize/generator/core/value/ValueCreator.java index 7024dda..aa2e8d3 100644 --- a/commons-serialize-generator-core/src/main/java/io/gitlab/jfronny/commons/serialize/generator/core/value/ValueCreator.java +++ b/commons-serialize-generator-core/src/main/java/io/gitlab/jfronny/commons/serialize/generator/core/value/ValueCreator.java @@ -124,10 +124,10 @@ public class ValueCreator { ExecutableElement noArgConstructor = null; List 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 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 messages = new ArrayList<>(); messages.add(new ElementException.Message("More than one constructor or factory method found.", klazz)); diff --git a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/Cl.java b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/Cl.java index e79cf42..e74c822 100644 --- a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/Cl.java +++ b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/Cl.java @@ -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"); diff --git a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/GProcessor.java b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/GProcessor.java index 113bfdd..1e91b57 100644 --- a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/GProcessor.java +++ b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/GProcessor.java @@ -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 typeVariables, Set 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>> toSearch = List.of(properties.params, properties.getters, properties.setters, properties.fields); + for (List> 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())) { diff --git a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/InstanceProcessor.java b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/InstanceProcessor.java index 6c70c18..e79084f 100644 --- a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/InstanceProcessor.java +++ b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/InstanceProcessor.java @@ -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"); diff --git a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/StaticProcessor.java b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/StaticProcessor.java index 1ad425b..5dacaf1 100644 --- a/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/StaticProcessor.java +++ b/commons-serialize-generator/src/main/java/io/gitlab/jfronny/commons/serialize/generator/gprocessor/StaticProcessor.java @@ -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); diff --git a/commons-serialize-generator/src/test/java/io/gitlab/jfronny/commons/serialize/generator/test/Main.java b/commons-serialize-generator/src/test/java/io/gitlab/jfronny/commons/serialize/generator/test/Main.java index 277edb1..9e05fa8 100644 --- a/commons-serialize-generator/src/test/java/io/gitlab/jfronny/commons/serialize/generator/test/Main.java +++ b/commons-serialize-generator/src/test/java/io/gitlab/jfronny/commons/serialize/generator/test/Main.java @@ -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; }