From c31d631375c2d69f579087b9bd7a731c28fcf543 Mon Sep 17 00:00:00 2001 From: Dmitrii Tikhomirov Date: Wed, 14 Oct 2020 23:23:29 +0200 Subject: [PATCH] fix for https://github.com/DominoKit/domino-jackson/issues/42 --- .../processor/AbstractMapperGenerator.java | 23 +++++------ .../processor/BeanMapperGenerator.java | 8 ++-- .../processor/BeanReaderGenerator.java | 4 +- .../processor/BeanWriterGenerator.java | 4 +- .../processor/DeserializerGenerator.java | 13 +++--- .../processor/SerializerGenerator.java | 14 +++---- .../dominokit/jacksonapt/processor/Type.java | 10 ++--- .../AptDeserializerBuilder.java | 11 +++-- .../deserialization/DeserializerBuilder.java | 6 +-- .../FieldDeserializersChainBuilder.java | 40 ++---------------- .../ParameterDeserializerBuilder.java | 6 +-- .../serialization/AptSerializerBuilder.java | 15 ++++--- .../FieldSerializerChainBuilder.java | 41 ++----------------- .../serialization/SerializerBuilder.java | 7 +--- .../DeserializerInstanceBuilderTest.java | 2 +- .../SerializerInstanceBuilderTest.java | 2 +- 16 files changed, 69 insertions(+), 137 deletions(-) diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/AbstractMapperGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/AbstractMapperGenerator.java index 7d2022fd..09fec35e 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/AbstractMapperGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/AbstractMapperGenerator.java @@ -1,6 +1,5 @@ package org.dominokit.jacksonapt.processor; -import static org.dominokit.jacksonapt.processor.AbstractMapperProcessor.elementUtils; import static org.dominokit.jacksonapt.processor.AbstractMapperProcessor.filer; import static org.dominokit.jacksonapt.processor.AbstractMapperProcessor.typeUtils; import static org.dominokit.jacksonapt.processor.ObjectMapperProcessor.DEFAULT_WILDCARD; @@ -11,6 +10,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; +import com.google.auto.common.MoreElements; import org.dominokit.jacksonapt.JsonDeserializer; import org.dominokit.jacksonapt.JsonSerializer; import org.dominokit.jacksonapt.ObjectMapper; @@ -36,11 +36,11 @@ public abstract class AbstractMapperGenerator implements MapperGenerator { @Override public void generate(Element element) throws IOException { String className = enclosingName(element, "_") + (useInterface(element) ? element.getSimpleName() : "Mapper") + "Impl"; - packageName = elementUtils.getPackageOf(element).getQualifiedName().toString(); + packageName = MoreElements.getPackage(element).toString(); TypeMirror beanType = getElementType(element); Name beanName = typeUtils.asElement(beanType).getSimpleName(); - generateJsonMappers(beanType, packageName, beanName); + generateJsonMappers(beanType); TypeSpec.Builder builder = TypeSpec.classBuilder(className) .addModifiers(Modifier.PUBLIC, Modifier.FINAL) @@ -116,7 +116,7 @@ protected MethodSpec makeConstructor(Name beanName) { protected MethodSpec makeNewDeserializerMethod(Element element, TypeMirror beanType) { CodeBlock.Builder builder = CodeBlock.builder(); if (Type.isBasicType(typeUtils.erasure(beanType))) { - builder.addStatement("return $L", new FieldDeserializersChainBuilder(packageName, getElementType(element)).getInstance(getElementType(element))); + builder.addStatement("return $L", new FieldDeserializersChainBuilder(getElementType(element)).getInstance(getElementType(element))); } else { builder.addStatement("return new " + deserializerName(beanType)); } @@ -144,7 +144,7 @@ protected MethodSpec makeNewDeserializerMethod(Element element, TypeMirror beanT protected MethodSpec makeNewSerializerMethod(TypeMirror beanType) { CodeBlock.Builder builder = CodeBlock.builder(); if (Type.isBasicType(typeUtils.erasure(beanType))) { - builder.addStatement("return $L", new FieldSerializerChainBuilder(packageName, beanType).getInstance(beanType)); + builder.addStatement("return $L", new FieldSerializerChainBuilder(beanType).getInstance(beanType)); } else { builder.addStatement("return new " + serializerName(beanType)); } @@ -208,10 +208,9 @@ private String serializerName(TypeMirror type) { * already exist. * * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. */ - private void generateJsonMappers(TypeMirror beanType, String packageName, Name beanName) { + private void generateJsonMappers(TypeMirror beanType) { // Root object for ObjectMapper can not have wildcards and/or type parameter. // TypeToken (and JsonRegistry) works only with declared types. if (Type.hasWildcards(beanType) || Type.hasTypeParameter(beanType)) { @@ -220,8 +219,8 @@ private void generateJsonMappers(TypeMirror beanType, String packageName, Name b if (beanType.getKind() == TypeKind.DECLARED && !Type.isBasicType(typeUtils.erasure(beanType))) { - generateDeserializer(beanType, packageName); - generateSerializer(beanType, packageName); + generateDeserializer(beanType); + generateSerializer(beanType); } } @@ -230,17 +229,15 @@ private void generateJsonMappers(TypeMirror beanType, String packageName, Name b * Generate serializer for given beanType and packageName * * @param beanType - * @param packageName */ - protected void generateSerializer(TypeMirror beanType, String packageName) { + protected void generateSerializer(TypeMirror beanType) { } /** * Generate deserializer for given beanType and packageName * * @param beanType - * @param packageName */ - protected void generateDeserializer(TypeMirror beanType, String packageName) { + protected void generateDeserializer(TypeMirror beanType) { } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanMapperGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanMapperGenerator.java index b1a8fa63..36f10d79 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanMapperGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanMapperGenerator.java @@ -29,12 +29,12 @@ protected Iterable getMapperMethods(Element element, TypeMirror bean } @Override - protected void generateSerializer(TypeMirror beanType, String packageName) { - new SerializerGenerator().generate(packageName, beanType); + protected void generateSerializer(TypeMirror beanType) { + new SerializerGenerator().generate(beanType); } @Override - protected void generateDeserializer(TypeMirror beanType, String packageName) { - new DeserializerGenerator().generate(packageName, beanType); + protected void generateDeserializer(TypeMirror beanType) { + new DeserializerGenerator().generate(beanType); } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanReaderGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanReaderGenerator.java index 9c190f7e..9835d220 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanReaderGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanReaderGenerator.java @@ -30,7 +30,7 @@ protected Iterable getMapperMethods(Element element, TypeMirror bean } @Override - protected void generateDeserializer(TypeMirror beanType, String packageName) { - new DeserializerGenerator().generate(packageName, beanType); + protected void generateDeserializer(TypeMirror beanType) { + new DeserializerGenerator().generate(beanType); } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanWriterGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanWriterGenerator.java index 73f54c1b..7a5239e5 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanWriterGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/BeanWriterGenerator.java @@ -30,7 +30,7 @@ protected Iterable getMapperMethods(Element element, TypeMirror bean } @Override - protected void generateSerializer(TypeMirror beanType, String packageName) { - new SerializerGenerator().generate(packageName, beanType); + protected void generateSerializer(TypeMirror beanType) { + new SerializerGenerator().generate(beanType); } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/DeserializerGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/DeserializerGenerator.java index 624b47dc..b93c2795 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/DeserializerGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/DeserializerGenerator.java @@ -1,10 +1,11 @@ package org.dominokit.jacksonapt.processor; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.ClassName; import org.dominokit.jacksonapt.processor.deserialization.AptDeserializerBuilder; import javax.annotation.processing.FilerException; -import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import java.io.IOException; @@ -26,15 +27,15 @@ public class DeserializerGenerator { * generated too. * * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. * @return fully qualified deserialer name */ - public String generate(String packageName, TypeMirror beanType) { + public String generate(TypeMirror beanType) { + String packageName = MoreElements.getPackage(MoreTypes.asTypeElement(beanType)).toString(); String deserializerName = Type.deserializerName(packageName, beanType); if (!TypeRegistry.containsDeserializer(Type.stringifyTypeWithPackage(beanType))) { try { - generateSubTypesDeserializers(beanType, packageName); + generateSubTypesDeserializers(beanType); TypeRegistry.addInActiveGenDeserializer(beanType); new AptDeserializerBuilder(packageName, beanType, filer).generate(); TypeRegistry.registerDeserializer(Type.stringifyTypeWithPackage(beanType), ClassName.bestGuess(deserializerName)); @@ -48,10 +49,10 @@ public String generate(String packageName, TypeMirror beanType) { return deserializerName; } - private void generateSubTypesDeserializers(TypeMirror beanType, String packageName) { + private void generateSubTypesDeserializers(TypeMirror beanType) { SubTypesInfo subTypesInfo= Type.getSubTypes(beanType); for (Map.Entry subtypeEntry: subTypesInfo.getSubTypes().entrySet()) { - new DeserializerGenerator().generate(packageName, subtypeEntry.getValue()); + new DeserializerGenerator().generate(subtypeEntry.getValue()); } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/SerializerGenerator.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/SerializerGenerator.java index 2d9f5448..38d6e53a 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/SerializerGenerator.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/SerializerGenerator.java @@ -1,10 +1,11 @@ package org.dominokit.jacksonapt.processor; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.ClassName; import org.dominokit.jacksonapt.processor.serialization.AptSerializerBuilder; import javax.annotation.processing.FilerException; -import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; @@ -25,15 +26,14 @@ public class SerializerGenerator { * generated too. * * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. * @return fully-qualified serialer name */ - public String generate(String packageName, TypeMirror beanType) { + public String generate(TypeMirror beanType) { + String packageName = MoreElements.getPackage(MoreTypes.asTypeElement(beanType)).toString(); String serializerName = Type.serializerName(packageName, beanType); - if (!TypeRegistry.containsSerializer(Type.stringifyTypeWithPackage(beanType))) { try { - generateSubTypeSerializers(packageName, beanType); + generateSubTypeSerializers(beanType); TypeRegistry.addInActiveGenSerializer(beanType); new AptSerializerBuilder(packageName, beanType, ObjectMapperProcessor.filer).generate(); TypeRegistry.registerSerializer(Type.stringifyTypeWithPackage(beanType), ClassName.bestGuess(serializerName)); @@ -47,10 +47,10 @@ public String generate(String packageName, TypeMirror beanType) { return serializerName; } - private void generateSubTypeSerializers(String packageName, TypeMirror beanType) { + private void generateSubTypeSerializers(TypeMirror beanType) { SubTypesInfo subTypesInfo= Type.getSubTypes(beanType); for (Map.Entry subtypeEntry: subTypesInfo.getSubTypes().entrySet()) { - new SerializerGenerator().generate(packageName, subtypeEntry.getValue()); + new SerializerGenerator().generate(subtypeEntry.getValue()); } } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/Type.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/Type.java index ae109102..aec537f8 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/Type.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/Type.java @@ -352,11 +352,10 @@ public String visitPrimitive(PrimitiveType t, Void p) { * package. * * @param typeMirror a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName {@link java.lang.String} package name for the serializer * @return a fully qualified deserializer name */ - public static String generateDeserializer(String packageName, TypeMirror typeMirror) { - return new DeserializerGenerator().generate(packageName, typeMirror); + public static String generateDeserializer(TypeMirror typeMirror) { + return new DeserializerGenerator().generate(typeMirror); } /** @@ -364,11 +363,10 @@ public static String generateDeserializer(String packageName, TypeMirror typeMir * package. * * @param typeMirror a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName {@link java.lang.String} package name for the serializer * @return a fully qualified serializer name */ - public static String generateSerializer(String packageName, TypeMirror typeMirror) { - return new SerializerGenerator().generate(packageName, typeMirror); + public static String generateSerializer(TypeMirror typeMirror) { + return new SerializerGenerator().generate(typeMirror); } /** diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/AptDeserializerBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/AptDeserializerBuilder.java index 201970d4..12fc8d37 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/AptDeserializerBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/AptDeserializerBuilder.java @@ -20,6 +20,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.*; import org.dominokit.jacksonapt.JacksonContextProvider; import org.dominokit.jacksonapt.JsonDeserializationContext; @@ -141,13 +143,16 @@ protected MethodSpec initSubtypesMethod() { for (Map.Entry subtypeEntry : subTypesInfo.getSubTypes().entrySet()) { // Prepare anonymous BeanTypeSerializer to delegate to the "real" serializer + String pkg = MoreElements.getPackage( + MoreTypes.asTypeElement(subtypeEntry.getValue())).toString(); TypeSpec subtypeType = TypeSpec.anonymousClassBuilder("") .superclass(ClassName.get(BeanSubtypeDeserializer.class)) .addMethod(MethodSpec.methodBuilder("newDeserializer") .addModifiers(Modifier.PROTECTED) .addAnnotation(Override.class) .returns(ParameterizedTypeName.get(ClassName.get(JsonDeserializer.class), WildcardTypeName.subtypeOf(Object.class))) - .addStatement("return new $T()", ClassName.bestGuess(Type.deserializerName(packageName, subtypeEntry.getValue()))) + .addStatement("return new $L()", ClassName.bestGuess( + Type.deserializerName(pkg, subtypeEntry.getValue()))) .build() ).build(); @@ -341,7 +346,7 @@ private MethodSpec buildInitInstanceBuilderMethod() { ExecutableElement creator = getCreator(); List parameterTypes = creator.getParameters(); parameterBuilders = parameterTypes.stream() - .map(o -> new ParameterDeserializerBuilder(typeUtils, beanType, o, packageName)) + .map(o -> new ParameterDeserializerBuilder(typeUtils, beanType, o)) .collect(Collectors.toList()); initInstanceMethodBuilder.addStatement("final $T deserializers = $T.get().mapLikeFactory().make()", deserializersTypeName, JacksonContextProvider.class); buildPropertiesDeserializers(initInstanceMethodBuilder); @@ -507,7 +512,7 @@ private Optional buildInitDeserializersMethod(TypeMirror beanType) { orderedFields().entrySet().stream() .filter(entry -> isEligibleForSerializationDeserialization(entry.getKey())) .forEach(entry -> builder.addStatement("map.put($S, $L)", - getPropertyName(entry.getKey()), new DeserializerBuilder(typeUtils, beanType, packageName, entry.getKey(), entry.getValue()).buildDeserializer())); + getPropertyName(entry.getKey()), new DeserializerBuilder(typeUtils, beanType, entry.getKey(), entry.getValue()).buildDeserializer())); builder.addStatement("return map"); return Optional.of(builder.build()); diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/DeserializerBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/DeserializerBuilder.java index f3e0b72d..4b045262 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/DeserializerBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/DeserializerBuilder.java @@ -40,14 +40,12 @@ class DeserializerBuilder extends AccessorsFilter { private final TypeMirror beanType; private final Element field; private final TypeMirror fieldType; - private final String packageName; - DeserializerBuilder(Types typeUtils, TypeMirror beanType, String packageName, Element field, TypeMirror fieldType) { + DeserializerBuilder(Types typeUtils, TypeMirror beanType, Element field, TypeMirror fieldType) { super(typeUtils); this.beanType = beanType; this.field = field; this.fieldType = fieldType; - this.packageName = packageName; } TypeSpec buildDeserializer() { @@ -85,7 +83,7 @@ private MethodSpec buildDeserializerMethod() { .addModifiers(Modifier.PROTECTED) .addAnnotation(Override.class) .returns(ParameterizedTypeName.get(ClassName.get(JsonDeserializer.class), ObjectMapperProcessor.DEFAULT_WILDCARD)) - .addStatement("return $L", new FieldDeserializersChainBuilder(packageName, beanType).getInstance(fieldType)) + .addStatement("return $L", new FieldDeserializersChainBuilder(beanType).getInstance(fieldType)) .build(); } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/FieldDeserializersChainBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/FieldDeserializersChainBuilder.java index c22682ba..7640686d 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/FieldDeserializersChainBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/FieldDeserializersChainBuilder.java @@ -15,6 +15,8 @@ */ package org.dominokit.jacksonapt.processor.deserialization; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.TypeName; @@ -31,9 +33,6 @@ import java.util.Deque; import java.util.LinkedList; -import static java.util.Objects.isNull; -import static java.util.Objects.nonNull; - /** *

FieldDeserializersChainBuilder class.

* @@ -48,7 +47,6 @@ public class FieldDeserializersChainBuilder implements MappersChainBuilder { private CodeBlock.Builder builder = CodeBlock.builder(); private Deque deserializers = new LinkedList<>(); private final TypeMirror beanType; - private final String packageName; /** *

Constructor for FieldDeserializersChainBuilder.

@@ -57,7 +55,6 @@ public class FieldDeserializersChainBuilder implements MappersChainBuilder { */ public FieldDeserializersChainBuilder(TypeMirror beanType) { this.beanType = beanType; - this.packageName = null; this.rootGenerated = true; } @@ -68,31 +65,6 @@ public FieldDeserializersChainBuilder(TypeMirror beanType) { */ public FieldDeserializersChainBuilder(TypeMirror beanType, boolean rootGenerated) { this.beanType = beanType; - this.packageName = null; - this.rootGenerated = rootGenerated; - } - - /** - *

Constructor for FieldDeserializersChainBuilder.

- * - * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. - */ - public FieldDeserializersChainBuilder(String packageName, TypeMirror beanType) { - this.beanType = beanType; - this.packageName = packageName; - this.rootGenerated = true; - } - - /** - *

Constructor for FieldDeserializersChainBuilder.

- * - * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. - */ - public FieldDeserializersChainBuilder(String packageName, TypeMirror beanType, boolean rootGenerated) { - this.beanType = beanType; - this.packageName = packageName; this.rootGenerated = rootGenerated; } @@ -152,11 +124,7 @@ private String getCustomDeserializer(TypeMirror typeMirror) { } private String getPackageName(TypeMirror typeMirror) { - if (Type.isJsonMapper(typeMirror) || isNull(this.packageName)) { - return ClassName.bestGuess(Type.getTypeQualifiedName(typeMirror)).packageName(); - } else { - return this.packageName; - } + return MoreElements.getPackage(MoreTypes.asTypeElement(typeMirror)).toString(); } private String generateCustomDeserializer(TypeMirror typeMirror) { @@ -164,7 +132,7 @@ private String generateCustomDeserializer(TypeMirror typeMirror) { return Type.deserializerName(getPackageName(typeMirror), typeMirror); } TypeRegistry.addInActiveGenDeserializer(typeMirror); - String deserializerName = Type.generateDeserializer(getPackageName(typeMirror), typeMirror); + String deserializerName = Type.generateDeserializer(typeMirror); TypeRegistry.removeInActiveGenDeserializer(typeMirror); return deserializerName; } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/ParameterDeserializerBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/ParameterDeserializerBuilder.java index 3561a6e9..3d633e6f 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/ParameterDeserializerBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/deserialization/ParameterDeserializerBuilder.java @@ -22,13 +22,11 @@ public class ParameterDeserializerBuilder { private Types typeUtils; private TypeMirror type; private VariableElement parameter; - private String packageName; - public ParameterDeserializerBuilder(Types typeUtils, TypeMirror type, VariableElement parameter, String packageName) { + public ParameterDeserializerBuilder(Types typeUtils, TypeMirror type, VariableElement parameter) { this.typeUtils = typeUtils; this.type = type; this.parameter = parameter; - this.packageName = packageName; } CodeBlock build() { @@ -38,7 +36,7 @@ CodeBlock build() { .addModifiers(Modifier.PROTECTED) .returns(ParameterizedTypeName.get(JsonDeserializer.class)) .addAnnotation(Override.class) - .addStatement("return $L", new FieldDeserializersChainBuilder(packageName, parameter.asType()).getInstance(parameter)) + .addStatement("return $L", new FieldDeserializersChainBuilder(parameter.asType()).getInstance(parameter)) .build(); ParameterizedTypeName deserializerType = ParameterizedTypeName.get(ClassName.get(HasDeserializerAndParameters.class), typeName, ParameterizedTypeName diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/AptSerializerBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/AptSerializerBuilder.java index 63818403..b22c7273 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/AptSerializerBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/AptSerializerBuilder.java @@ -16,6 +16,8 @@ package org.dominokit.jacksonapt.processor.serialization; import com.fasterxml.jackson.annotation.ObjectIdGenerator; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.*; import org.dominokit.jacksonapt.JsonSerializationContext; import org.dominokit.jacksonapt.JsonSerializer; @@ -178,15 +180,13 @@ private TypeSpec generateIdentifierSerializationInfo(BeanIdentityInfo identityIn private void buildBeanPropertySerializerBody(TypeSpec.Builder builder, Map.Entry property) { - - CodeBlock serializerType = new FieldSerializerChainBuilder(property.getValue()).getInstance(property.getValue()); String paramName = "bean"; - AccessorInfo accessorInfo = new SerializerBuilder(typeUtils, beanType, packageName, property.getKey(), property.getValue()).getterInfo(); + AccessorInfo accessorInfo = new SerializerBuilder(typeUtils, beanType, property.getKey(), property.getValue()).getterInfo(); MethodSpec.Builder newSerializerMethodBuilder = MethodSpec.methodBuilder("newSerializer") .addModifiers(Modifier.PROTECTED) .addAnnotation(Override.class) - .addStatement("return $L", serializerType); + .addStatement("return $L", new FieldSerializerChainBuilder(property.getValue()).getInstance(property.getValue())); newSerializerMethodBuilder.returns(ParameterizedTypeName.get(ClassName.get(JsonSerializer.class), DEFAULT_WILDCARD)); builder.addMethod(newSerializerMethodBuilder.build()); @@ -235,13 +235,16 @@ protected MethodSpec initSubtypesMethod() { for (Map.Entry subtypeEntry : subTypesInfo.getSubTypes().entrySet()) { // Prepare anonymous BeanTypeSerializer to delegate to the "real" serializer + String pkg = MoreElements.getPackage( + MoreTypes.asTypeElement(subtypeEntry.getValue())).toString(); TypeSpec subtypeType = TypeSpec.anonymousClassBuilder("") .superclass(ClassName.get(BeanSubtypeSerializer.class)) .addMethod(MethodSpec.methodBuilder("newSerializer") .addModifiers(Modifier.PROTECTED) .addAnnotation(Override.class) .returns(ParameterizedTypeName.get(ClassName.get(JsonSerializer.class), WildcardTypeName.subtypeOf(Object.class))) - .addStatement("return new $T()", ClassName.bestGuess(Type.serializerName(packageName, subtypeEntry.getValue()))) + .addStatement("return new $L()", ClassName.bestGuess( + Type.serializerName(pkg, subtypeEntry.getValue()))) .build() ).build(); @@ -271,7 +274,7 @@ private Optional buildInitSerializersMethod(TypeMirror beanType) { fields.entrySet().stream() .filter(entry -> isEligibleForSerializationDeserialization(entry.getKey())) .forEach(entry -> builder.addStatement("result[$L] = $L", - index[0]++, new SerializerBuilder(typeUtils, beanType, packageName, entry.getKey(), entry.getValue()).buildSerializer())); + index[0]++, new SerializerBuilder(typeUtils, beanType, entry.getKey(), entry.getValue()).buildSerializer())); builder.addStatement("return result"); return Optional.of(builder.build()); diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/FieldSerializerChainBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/FieldSerializerChainBuilder.java index 31a84acf..872a64a7 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/FieldSerializerChainBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/FieldSerializerChainBuilder.java @@ -15,6 +15,8 @@ */ package org.dominokit.jacksonapt.processor.serialization; +import com.google.auto.common.MoreElements; +import com.google.auto.common.MoreTypes; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.TypeName; @@ -33,9 +35,6 @@ import java.util.Deque; import java.util.LinkedList; -import static java.util.Objects.isNull; -import static java.util.Objects.nonNull; - /** *

FieldSerializerChainBuilder class.

* @@ -51,8 +50,6 @@ public class FieldSerializerChainBuilder implements MappersChainBuilder { private CodeBlock.Builder builder = CodeBlock.builder(); private Deque serializers = new LinkedList<>(); private final TypeMirror beanType; - private final String packageName; - /** *

Constructor for FieldSerializerChainBuilder.

@@ -61,7 +58,6 @@ public class FieldSerializerChainBuilder implements MappersChainBuilder { */ public FieldSerializerChainBuilder(TypeMirror beanType) { this.beanType = beanType; - this.packageName = null; this.rootGenerated = true; } @@ -72,31 +68,6 @@ public FieldSerializerChainBuilder(TypeMirror beanType) { */ public FieldSerializerChainBuilder(TypeMirror beanType, boolean rootGenerated) { this.beanType = beanType; - this.packageName = null; - this.rootGenerated = rootGenerated; - } - - /** - *

Constructor for FieldSerializerChainBuilder.

- * - * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. - */ - public FieldSerializerChainBuilder(String packageName, TypeMirror beanType) { - this.beanType = beanType; - this.packageName = packageName; - this.rootGenerated = true; - } - - /** - *

Constructor for FieldSerializerChainBuilder.

- * - * @param beanType a {@link javax.lang.model.type.TypeMirror} object. - * @param packageName a {@link java.lang.String} object. - */ - public FieldSerializerChainBuilder(String packageName, TypeMirror beanType, boolean rootGenerated) { - this.beanType = beanType; - this.packageName = packageName; this.rootGenerated = rootGenerated; } @@ -154,11 +125,7 @@ private String getCustomSerializer(TypeMirror typeMirror) { } private String getPackageName(TypeMirror typeMirror) { - if (Type.isJsonMapper(typeMirror) || isNull(this.packageName)) { - return ClassName.bestGuess(Type.getTypeQualifiedName(typeMirror)).packageName(); - } else { - return this.packageName; - } + return MoreElements.getPackage(MoreTypes.asTypeElement(typeMirror)).toString(); } private String generateCustomSerializer(TypeMirror typeMirror) { @@ -166,7 +133,7 @@ private String generateCustomSerializer(TypeMirror typeMirror) { return Type.serializerName(getPackageName(typeMirror), typeMirror); } TypeRegistry.addInActiveGenSerializer(typeMirror); - String serializerName = Type.generateSerializer(getPackageName(typeMirror), typeMirror); + String serializerName = Type.generateSerializer(typeMirror); TypeRegistry.removeInActiveGenSerializer(typeMirror); return serializerName; } diff --git a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/SerializerBuilder.java b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/SerializerBuilder.java index f2ac2e55..c01077f0 100644 --- a/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/SerializerBuilder.java +++ b/jackson-apt-processor/src/main/java/org/dominokit/jacksonapt/processor/serialization/SerializerBuilder.java @@ -46,14 +46,12 @@ class SerializerBuilder extends AccessorsFilter { private final TypeMirror beanType; private final Element field; private final TypeMirror fieldType; - private final String packageName; - SerializerBuilder(Types typeUtils, TypeMirror beanType, String packageName, Element field, TypeMirror fieldType) { + SerializerBuilder(Types typeUtils, TypeMirror beanType, Element field, TypeMirror fieldType) { super(typeUtils); this.beanType = beanType; this.field = field; this.fieldType = fieldType; - this.packageName = packageName; } TypeSpec buildSerializer() { @@ -94,7 +92,7 @@ private MethodSpec buildSerializerMethod() { .addModifiers(Modifier.PROTECTED) .addAnnotation(Override.class) .returns(ParameterizedTypeName.get(ClassName.get(JsonSerializer.class), ObjectMapperProcessor.DEFAULT_WILDCARD)) - .addStatement("return $L", new FieldSerializerChainBuilder(packageName, beanType).getInstance(fieldType)) + .addStatement("return $L", new FieldSerializerChainBuilder(beanType).getInstance(fieldType)) .build(); } @@ -154,5 +152,4 @@ private String upperCaseFirstLetter(String name) { return name.substring(0, 1).toUpperCase() + name.substring(1); } - } diff --git a/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/DeserializerInstanceBuilderTest.java b/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/DeserializerInstanceBuilderTest.java index 18eca5f0..2f4f5cf9 100644 --- a/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/DeserializerInstanceBuilderTest.java +++ b/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/DeserializerInstanceBuilderTest.java @@ -43,7 +43,7 @@ public class DeserializerInstanceBuilderTest extends BaseInstanceBuilderTest { @Override MappersChainBuilder getMappersChainBuilder(TypeMirror beanType) { - return new FieldDeserializersChainBuilder(Type.getPackage(beanType), beanType); + return new FieldDeserializersChainBuilder(beanType); } @Test diff --git a/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/SerializerInstanceBuilderTest.java b/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/SerializerInstanceBuilderTest.java index 0d139b39..d042da2b 100644 --- a/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/SerializerInstanceBuilderTest.java +++ b/jackson-apt-processor/src/test/java/org/dominokit/jacksonapt/processor/SerializerInstanceBuilderTest.java @@ -32,7 +32,7 @@ public class SerializerInstanceBuilderTest extends BaseInstanceBuilderTest { @Override MappersChainBuilder getMappersChainBuilder(TypeMirror beanType) { - return new FieldSerializerChainBuilder(Type.getPackage(beanType), beanType); + return new FieldSerializerChainBuilder(beanType); } @Test