From 861d92dc64c7b33270990030ad09c74e615676b2 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 3 Mar 2013 14:34:54 +0100 Subject: [PATCH] #7 Using Hickory for dealing with MapStruct annotations --- parent/pom.xml | 17 ++- processor/pom.xml | 54 ++++++- .../mapstruct/ap/MapperGenerationVisitor.java | 138 +++--------------- .../org/mapstruct/ap/MappingProcessor.java | 11 ++ .../java/org/mapstruct/ap/util/TypeUtil.java | 5 +- 5 files changed, 101 insertions(+), 124 deletions(-) diff --git a/parent/pom.xml b/parent/pom.xml index c3e925d46..d01a3e72a 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -30,6 +30,7 @@ UTF-8 + 1.0.0 @@ -49,7 +50,11 @@ fest-assert 1.4 - + + com.jolira + hickory + ${com.jolira.hickory.version} + ${project.groupId} mapstruct @@ -70,6 +75,11 @@ 1.6 + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + org.apache.maven.plugins maven-dependency-plugin @@ -85,6 +95,11 @@ maven-license-plugin 1.9.0 + + org.bsc.maven + maven-processor-plugin + 2.0.2 + diff --git a/processor/pom.xml b/processor/pom.xml index 871dde86c..154bcd497 100644 --- a/processor/pom.xml +++ b/processor/pom.xml @@ -31,11 +31,20 @@ jar MapStruct Processor + + ${project.build.directory}/generated-sources/prims + + org.freemarker freemarker + + com.jolira + hickory + provided + org.testng testng @@ -49,7 +58,6 @@ ${project.groupId} mapstruct - test @@ -80,6 +88,50 @@ -proc:none + + org.bsc.maven + maven-processor-plugin + + ${prism.directory} + + net.java.dev.hickory.prism.internal.PrismGenerator + + + + + process + generate-sources + + process + + + + + + com.jolira + hickory + ${com.jolira.hickory.version} + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-source + generate-sources + + add-source + + + + ${prism.directory} + + + + + diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index c7bef65ea..ce7572dc5 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -23,21 +23,15 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import javax.annotation.processing.ProcessingEnvironment; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; -import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; -import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementKindVisitor6; import javax.lang.model.util.Elements; -import javax.lang.model.util.SimpleAnnotationValueVisitor6; import javax.lang.model.util.Types; import javax.tools.JavaFileObject; @@ -61,9 +55,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { private final static String IMPLEMENTATION_SUFFIX = "Impl"; - private final static String MAPPING_ANNOTATION = "org.mapstruct.Mapping"; - private final static String MAPPINGS_ANNOTATION = "org.mapstruct.Mappings"; - private final ProcessingEnvironment processingEnvironment; private final Types typeUtils; private final Elements elementUtils; @@ -263,22 +254,17 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { } private List retrieveMappedProperties(ExecutableElement method) { - Map mappings = new HashMap(); - for ( AnnotationMirror annotationMirror : method.getAnnotationMirrors() ) { + MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method ); + MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method ); - String annotationName = annotationMirror.getAnnotationType() - .asElement() - .accept( new NameDeterminationVisitor(), null ); + if ( mappingAnnotation != null ) { + mappings.put( mappingAnnotation.source(), getMapping( mappingAnnotation ) ); + } - if ( MAPPING_ANNOTATION.equals( annotationName ) ) { - Mapping mapping = getMapping( annotationMirror ); - mappings.put( mapping.getSourceName(), mapping ); - } - else if ( MAPPINGS_ANNOTATION.equals( annotationName ) ) { - mappings.putAll( getMappings( annotationMirror ) ); - } + if ( mappingsAnnotation != null ) { + mappings.putAll( getMappings( mappingsAnnotation ) ); } return getMappedProperties( method, mappings ); @@ -323,35 +309,23 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { ); } - private Map getMappings(AnnotationMirror annotationMirror) { + private Map getMappings(MappingsPrism mappingsAnnotation) { Map mappings = new HashMap(); - List values = getAnnotationValueListValue( annotationMirror, "value" ); - - for ( AnnotationValue oneAnnotationValue : values ) { - AnnotationMirror oneAnnotation = oneAnnotationValue.accept( - new AnnotationRetrievingVisitor(), - null - ); - Mapping mapping = getMapping( oneAnnotation ); - mappings.put( mapping.getSourceName(), mapping ); + for ( MappingPrism mapping : mappingsAnnotation.value() ) { + mappings.put( mapping.source(), getMapping( mapping ) ); } return mappings; } - private Mapping getMapping(AnnotationMirror annotationMirror) { - String sourcePropertyName = getStringValue( annotationMirror, "source" ); - String targetPropertyName = getStringValue( annotationMirror, "target" ); - TypeMirror converterTypeMirror = getTypeMirrorValue( annotationMirror, "converter" ); - - Type converterType = null; - - if ( converterTypeMirror != null ) { - converterType = typeUtil.getType( (DeclaredType) converterTypeMirror ); - } - - return new Mapping( sourcePropertyName, targetPropertyName, converterType ); + private Mapping getMapping(MappingPrism mapping) { + Type converterType = typeUtil.retrieveType( mapping.converter() ); + return new Mapping( + mapping.source(), + mapping.target(), + converterType.getName().equals( "NoOpConverter" ) ? null : converterType + ); } private Parameter retrieveParameter(ExecutableElement method) { @@ -374,42 +348,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return typeUtil.retrieveType( method.getReturnType() ); } - private String getStringValue(AnnotationMirror annotationMirror, String attributeName) { - for ( Entry oneAttribute : annotationMirror.getElementValues() - .entrySet() ) { - - if ( oneAttribute.getKey().getSimpleName().contentEquals( attributeName ) ) { - return oneAttribute.getValue().accept( new StringValueRetrievingVisitor(), null ); - } - } - - return null; - } - - private TypeMirror getTypeMirrorValue(AnnotationMirror annotationMirror, String attributeName) { - for ( Entry oneAttribute : annotationMirror.getElementValues() - .entrySet() ) { - - if ( oneAttribute.getKey().getSimpleName().contentEquals( attributeName ) ) { - return oneAttribute.getValue().accept( new TypeRetrievingVisitor(), null ); - } - } - - return null; - } - - private List getAnnotationValueListValue(AnnotationMirror annotationMirror, String attributeName) { - for ( Entry oneAttribute : annotationMirror.getElementValues() - .entrySet() ) { - - if ( oneAttribute.getKey().getSimpleName().contentEquals( "value" ) ) { - return oneAttribute.getValue().accept( new AnnotationValueListRetrievingVisitor(), null ); - } - } - - return null; - } - private List getterMethodsIn(Iterable elements) { List getterMethods = new LinkedList(); @@ -441,46 +379,4 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return setterMethods; } - - private static class NameDeterminationVisitor extends ElementKindVisitor6 { - - @Override - public String visitType(TypeElement element, Void p) { - return element.getQualifiedName().toString(); - } - } - - private static class StringValueRetrievingVisitor extends SimpleAnnotationValueVisitor6 { - - @Override - public String visitString(String value, Void p) { - return value; - } - } - - private static class TypeRetrievingVisitor - extends SimpleAnnotationValueVisitor6 { - - @Override - public TypeMirror visitType(TypeMirror value, Void p) { - return value; - } - } - - private static class AnnotationValueListRetrievingVisitor - extends SimpleAnnotationValueVisitor6, Void> { - - @Override - public List visitArray(List value, Void p) { - return value; - } - } - - private static class AnnotationRetrievingVisitor extends SimpleAnnotationValueVisitor6 { - - @Override - public AnnotationMirror visitAnnotation(AnnotationMirror value, Void p) { - return value; - } - } } diff --git a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java index b93c7aef3..ccc42b276 100644 --- a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java @@ -25,7 +25,18 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; +import net.java.dev.hickory.prism.GeneratePrism; +import net.java.dev.hickory.prism.GeneratePrisms; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; + @SupportedAnnotationTypes("org.mapstruct.Mapper") +@GeneratePrisms({ + @GeneratePrism(value = Mapper.class), + @GeneratePrism(value = Mapping.class), + @GeneratePrism(value = Mappings.class) +}) public class MappingProcessor extends AbstractProcessor { /** diff --git a/processor/src/main/java/org/mapstruct/ap/util/TypeUtil.java b/processor/src/main/java/org/mapstruct/ap/util/TypeUtil.java index c91c6797a..ccc1f0441 100644 --- a/processor/src/main/java/org/mapstruct/ap/util/TypeUtil.java +++ b/processor/src/main/java/org/mapstruct/ap/util/TypeUtil.java @@ -51,7 +51,10 @@ public class TypeUtil { } public Type retrieveType(TypeMirror mirror) { - if ( mirror.getKind() == TypeKind.DECLARED ) { + if ( mirror == null ) { + return null; + } + else if ( mirror.getKind() == TypeKind.DECLARED ) { return getType( ( (DeclaredType) mirror ) ); } else {