Skip to content

Commit

Permalink
fix for DominoKit#42
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Tikhomirov committed Oct 14, 2020
1 parent 3f920f6 commit c31d631
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 137 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);

}
}
Expand All @@ -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) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ protected Iterable<MethodSpec> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected Iterable<MethodSpec> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected Iterable<MethodSpec> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
Expand All @@ -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<String, TypeMirror> subtypeEntry: subTypesInfo.getSubTypes().entrySet()) {
new DeserializerGenerator().generate(packageName, subtypeEntry.getValue());
new DeserializerGenerator().generate(subtypeEntry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;


Expand All @@ -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));
Expand All @@ -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<String, TypeMirror> subtypeEntry: subTypesInfo.getSubTypes().entrySet()) {
new SerializerGenerator().generate(packageName, subtypeEntry.getValue());
new SerializerGenerator().generate(subtypeEntry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,23 +352,21 @@ 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);
}

/**
* Generate serializer for given TypeMirror. Serializer is situated in the specified
* 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,13 +143,16 @@ protected MethodSpec initSubtypesMethod() {

for (Map.Entry<String, TypeMirror> 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();

Expand Down Expand Up @@ -341,7 +346,7 @@ private MethodSpec buildInitInstanceBuilderMethod() {
ExecutableElement creator = getCreator();
List<? extends VariableElement> 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);
Expand Down Expand Up @@ -507,7 +512,7 @@ private Optional<MethodSpec> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit c31d631

Please sign in to comment.