#35 Extracting Mapping instantitation from visitor class

This commit is contained in:
Gunnar Morling 2013-06-09 11:28:18 +02:00
parent a5fd73f255
commit 895a715727
3 changed files with 59 additions and 34 deletions

View File

@ -442,24 +442,18 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
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<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
Map<String, Mapping> 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<MappedProperty> getMappedProperties(ExecutableElement method, Map<String, Mapping> 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<Void, Void> {
}
}
private Map<String, Mapping> 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<String, Mapping> getMappings(ExecutableElement method) {
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
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<? extends VariableElement> parameters = method.getParameters();

View File

@ -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 {

View File

@ -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<String, Mapping> fromMappingsPrism(MappingsPrism mappingsAnnotation) {
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
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;