From 895a71572731e0e53d7d15de1ceec7daa3d5eed2 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 9 Jun 2013 11:28:18 +0200 Subject: [PATCH] #35 Extracting Mapping instantitation from visitor class --- .../mapstruct/ap/MapperGenerationVisitor.java | 57 +++++++++---------- .../org/mapstruct/ap/MappingProcessor.java | 6 +- .../mapstruct/ap/model/source/Mapping.java | 30 +++++++++- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index c050d948c..7a9f6c133 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -442,24 +442,18 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return methods; } + /** + * Returns all properties of the parameter type of the given method which + * are mapped to a corresponding property of the return type of the given + * method. + * + * @param method The method of interest + * + * @return All mapped properties for the given method + */ private List retrieveMappedProperties(ExecutableElement method) { - Map mappings = new HashMap(); + Map mappings = getMappings( method ); - MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method ); - MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method ); - - if ( mappingAnnotation != null ) { - mappings.put( mappingAnnotation.source(), getMapping( mappingAnnotation ) ); - } - - if ( mappingsAnnotation != null ) { - mappings.putAll( getMappings( mappingsAnnotation ) ); - } - - return getMappedProperties( method, mappings ); - } - - private List getMappedProperties(ExecutableElement method, Map mappings) { TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getReturnType() ); TypeElement parameterElement = (TypeElement) typeUtils.asElement( method.getParameters().get( 0 ).asType() ); @@ -544,26 +538,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { } } - private Map getMappings(MappingsPrism mappingsAnnotation) { + /** + * Retrieves the mappings configured via {@code @Mapping} from the given + * method. + * + * @param method The method of interest + * + * @return The mappings for the given method, keyed by source property name + */ + private Map getMappings(ExecutableElement method) { Map mappings = new HashMap(); - for ( MappingPrism mapping : mappingsAnnotation.value() ) { - mappings.put( mapping.source(), getMapping( mapping ) ); + MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method ); + MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method ); + + if ( mappingAnnotation != null ) { + mappings.put( mappingAnnotation.source(), Mapping.fromMappingPrism( mappingAnnotation ) ); + } + + if ( mappingsAnnotation != null ) { + mappings.putAll( Mapping.fromMappingsPrism( mappingsAnnotation ) ); } return mappings; } - private Mapping getMapping(MappingPrism mapping) { - return new Mapping( - mapping.source(), - mapping.target(), - mapping.mirror, - mapping.values.source(), - mapping.values.target() - ); - } - private Parameter retrieveParameter(ExecutableElement method) { List parameters = method.getParameters(); diff --git a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java index 2484aeac4..d9608abbf 100644 --- a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java @@ -38,9 +38,9 @@ import org.mapstruct.ap.model.Options; @SupportedAnnotationTypes("org.mapstruct.Mapper") @GeneratePrisms({ - @GeneratePrism(value = Mapper.class), - @GeneratePrism(value = Mapping.class), - @GeneratePrism(value = Mappings.class) + @GeneratePrism(value = Mapper.class, publicAccess = true), + @GeneratePrism(value = Mapping.class, publicAccess = true), + @GeneratePrism(value = Mappings.class, publicAccess = true) }) @SupportedOptions(MappingProcessor.SUPPRESS_GENERATOR_TIMESTAMP) public class MappingProcessor extends AbstractProcessor { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java index 3c2defc5e..cce5c3a4c 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java @@ -18,9 +18,14 @@ */ package org.mapstruct.ap.model.source; +import java.util.HashMap; +import java.util.Map; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; +import org.mapstruct.ap.MappingPrism; +import org.mapstruct.ap.MappingsPrism; + /** * Represents a property mapping as configured via {@code @Mapping}. * @@ -34,8 +39,29 @@ public class Mapping { private final AnnotationValue sourceAnnotationValue; private final AnnotationValue targetAnnotationValue; - public Mapping(String sourceName, String targetName, AnnotationMirror mirror, AnnotationValue sourceAnnotationValue, - AnnotationValue targetAnnotationValue) { + public static Map fromMappingsPrism(MappingsPrism mappingsAnnotation) { + Map mappings = new HashMap(); + + for ( MappingPrism mapping : mappingsAnnotation.value() ) { + mappings.put( mapping.source(), fromMappingPrism( mapping ) ); + } + + return mappings; + } + + public static Mapping fromMappingPrism(MappingPrism mapping) { + return new Mapping( + mapping.source(), + mapping.target(), + mapping.mirror, + mapping.values.source(), + mapping.values.target() + ); + } + + private Mapping(String sourceName, String targetName, AnnotationMirror mirror, + AnnotationValue sourceAnnotationValue, + AnnotationValue targetAnnotationValue) { this.sourceName = sourceName; this.targetName = targetName; this.mirror = mirror;