From cd0306683fb076ed3a96af7cbdcd7173df8f29bd Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 23 Jun 2013 10:07:49 +0200 Subject: [PATCH] #32 Replacing BeanMapping with plain list of mapping methods --- .../mapstruct/ap/MapperGenerationVisitor.java | 301 +++++------------- .../org/mapstruct/ap/model/BeanMapping.java | 48 --- .../java/org/mapstruct/ap/model/Mapper.java | 16 +- .../ap/model/source/MappedProperty.java | 18 +- .../mapstruct/ap/model/source/Mapping.java | 4 + .../org/mapstruct/ap/model/source/Method.java | 33 +- .../org/mapstruct/ap/util/Executables.java | 24 -- .../org.mapstruct.ap.model.BeanMapping.ftl | 25 -- .../org.mapstruct.ap.model.Mapper.ftl | 4 +- 9 files changed, 122 insertions(+), 351 deletions(-) delete mode 100644 processor/src/main/java/org/mapstruct/ap/model/BeanMapping.java delete mode 100644 processor/src/main/resources/org.mapstruct.ap.model.BeanMapping.ftl diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index 1ae8c1407..e4b16b359 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -45,7 +45,6 @@ import javax.tools.JavaFileObject; import org.mapstruct.ap.conversion.Conversion; import org.mapstruct.ap.conversion.Conversions; -import org.mapstruct.ap.model.BeanMapping; import org.mapstruct.ap.model.IterableMappingMethod; import org.mapstruct.ap.model.Mapper; import org.mapstruct.ap.model.MappingMethod; @@ -143,7 +142,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { List methods = retrieveMethods( element, true ); //2.) build up aggregated "target" model - List mappings = getMappings( + List mappings = getMappingMethods( methods, getEffectiveUnmappedTargetPolicy( element ) ); @@ -186,48 +185,55 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { } } - private List getMappings(List methods, - ReportingPolicy unmappedTargetPolicy) { - List mappings = new ArrayList(); - Set processedMethods = new HashSet(); + private List getMappingMethods(List methods, ReportingPolicy unmappedTargetPolicy) { + List mappingMethods = new ArrayList(); for ( Method method : methods ) { - if ( processedMethods.contains( method ) || method.getDeclaringMapper() != null ) { + if ( method.getDeclaringMapper() != null ) { continue; } - Method reverseMappingMethod = getReverseMappingMethod( methods, method ); - - if ( reverseMappingMethod != null ) { - processedMethods.add( reverseMappingMethod ); + if ( method.getMappings().isEmpty() ) { + Method reverseMappingMethod = getReverseMappingMethod( methods, method ); + if ( reverseMappingMethod != null && !reverseMappingMethod.getMappings().isEmpty() ) { + method.setMappings( reverse( reverseMappingMethod.getMappings() ) ); + } } - boolean isIterableMapping = method.getSourceType().isIterableType() && method.getTargetType() - .isIterableType(); - - if ( isIterableMapping ) { - mappings.add( getIterableBeanMapping( methods, method, reverseMappingMethod ) ); + if ( method.isIterableMapping() ) { + mappingMethods.add( getIterableMappingMethod( methods, method ) ); } else { - mappings.add( getSimpleBeanMapping( methods, unmappedTargetPolicy, method, reverseMappingMethod ) ); + mappingMethods.add( getSimpleMappingMethod( methods, method, unmappedTargetPolicy ) ); } } - return mappings; + return mappingMethods; } - private BeanMapping getSimpleBeanMapping(List methods, ReportingPolicy unmappedTargetPolicy, Method method, - Method rawReverseMappingMethod) { + private Map reverse(Map mappings) { + Map reversed = new HashMap(); + + for ( Mapping mapping : mappings.values() ) { + reversed.put( mapping.getTargetName(), mapping.reverse() ); + } + return reversed; + } + + private MappingMethod getSimpleMappingMethod(List methods, Method method, + ReportingPolicy unmappedTargetPolicy) { List propertyMappings = new ArrayList(); - List reversePropertyMappings = new ArrayList(); - Set mappedSourceProperties = new HashSet(); Set mappedTargetProperties = new HashSet(); - for ( MappedProperty property : method.getMappedProperties() ) { - mappedSourceProperties.add( property.getSourceName() ); + List mappedProperties = retrieveMappedProperties( method ); + + for ( MappedProperty property : mappedProperties ) { mappedTargetProperties.add( property.getTargetName() ); - Method propertyMappingMethod = getPropertyMappingMethod( methods, property ); - Method reversePropertyMappingMethod = getReversePropertyMappingMethod( methods, property ); + MappingMethodReference propertyMappingMethod = getMappingMethodReference( + methods, + property.getSourceType(), + property.getTargetType() + ); Conversion conversion = conversions.getConversion( property.getSourceType(), property.getTargetType() @@ -235,10 +241,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { reportErrorIfPropertyCanNotBeMapped( method, - rawReverseMappingMethod, property, propertyMappingMethod, - reversePropertyMappingMethod, conversion ); @@ -250,84 +254,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { property.getSourceType(), property.getTargetWriteAccessorName(), property.getTargetType(), - propertyMappingMethod != null ? new MappingMethodReference( - propertyMappingMethod.getDeclaringMapper(), - propertyMappingMethod.getName(), - propertyMappingMethod.getParameterName(), - property.getSourceType(), - property.getTargetType() - ) : null, + propertyMappingMethod, conversion != null ? conversion.to( method.getParameterName() + "." + property.getSourceReadAccessorName() + "()", property.getTargetType() ) : null ) ); - - if ( rawReverseMappingMethod != null ) { - reversePropertyMappings.add( - new PropertyMapping( - rawReverseMappingMethod.getParameterName(), - Introspector.decapitalize( rawReverseMappingMethod.getTargetType().getName() ), - property.getTargetReadAccessorName(), - property.getTargetType(), - property.getSourceWriteAccessorName(), - property.getSourceType(), - reversePropertyMappingMethod != null ? new MappingMethodReference( - reversePropertyMappingMethod.getDeclaringMapper(), - reversePropertyMappingMethod.getName(), - reversePropertyMappingMethod.getParameterName(), - property.getTargetType(), - property.getSourceType() - ) : null, - conversion != null && rawReverseMappingMethod != null ? conversion.from( - rawReverseMappingMethod.getParameterName() + "." + - property.getTargetReadAccessorName() + - "()", - property.getSourceType() - ) : null - ) - ); - } } - MappingMethod mappingMethod = new SimpleMappingMethod( + reportErrorForUnmappedTargetPropertiesIfRequired( + method, + unmappedTargetPolicy, + mappedTargetProperties + ); + + return new SimpleMappingMethod( method.getName(), method.getParameterName(), method.getSourceType(), method.getTargetType(), propertyMappings ); - - reportErrorForUnmappedTargetPropertiesIfRequired( - method.getExecutable(), - unmappedTargetPolicy, - method.getTargetProeprties(), - mappedTargetProperties - ); - - MappingMethod reverseMappingMethod = null; - if ( rawReverseMappingMethod != null ) { - reverseMappingMethod = new SimpleMappingMethod( - rawReverseMappingMethod.getName(), - rawReverseMappingMethod.getParameterName(), - method.getTargetType(), - method.getSourceType(), - reversePropertyMappings - ); - - reportErrorForUnmappedTargetPropertiesIfRequired( - rawReverseMappingMethod.getExecutable(), - unmappedTargetPolicy, - method.getSourceProperties(), - mappedSourceProperties - ); - } - - return new BeanMapping( mappingMethod, reverseMappingMethod ); } - private BeanMapping getIterableBeanMapping(List methods, Method method, Method rawReverseMappingMethod) { + private MappingMethod getIterableMappingMethod(List methods, Method method) { String toConversionString = getIterableConversionString( conversions, method.getSourceType().getElementType(), @@ -335,59 +286,41 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { true ); - String fromConversionString = getIterableConversionString( - conversions, - method.getTargetType().getElementType(), - method.getSourceType().getElementType(), - false - ); - - MappingMethod mappingMethod = new IterableMappingMethod( + return new IterableMappingMethod( method.getName(), method.getParameterName(), method.getSourceType(), method.getTargetType(), - getElementMappingMethod( methods, method ), + getMappingMethodReference( + methods, + method.getSourceType().getElementType(), + method.getTargetType().getElementType() + ), toConversionString ); - - MappingMethod reverseMappingMethod = null; - - if ( rawReverseMappingMethod != null ) { - reverseMappingMethod = new IterableMappingMethod( - rawReverseMappingMethod.getName(), - rawReverseMappingMethod.getParameterName(), - method.getTargetType(), - method.getSourceType(), - getElementMappingMethod( methods, rawReverseMappingMethod ), - fromConversionString - ); - } - - return new BeanMapping( mappingMethod, reverseMappingMethod ); } - private void reportErrorForUnmappedTargetPropertiesIfRequired(ExecutableElement method, + private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method, ReportingPolicy unmappedTargetPolicy, - Set targetProperties, Set mappedTargetProperties) { - if ( targetProperties.size() > mappedTargetProperties.size() && unmappedTargetPolicy.requiresReport() ) { - targetProperties.removeAll( mappedTargetProperties ); + if ( method.getTargetProeprties().size() > mappedTargetProperties.size() && + unmappedTargetPolicy.requiresReport() ) { + method.getTargetProeprties().removeAll( mappedTargetProperties ); printMessage( unmappedTargetPolicy, MessageFormat.format( "Unmapped target {0,choice,1#property|1 { method.getExecutable() ); } - - if ( reverseMethod == null ) { - return; - } - - if ( !( - reversePropertyMappingMethod != null || - conversion != null || - ( property.getSourceType().isCollectionType() && property.getSourceType() - .getCollectionImplementationType() != null ) ) ) { - - printMessage( - ReportingPolicy.ERROR, - String.format( - "Can't map property \"%s %s\" to \"%s %s\".", - property.getTargetType(), - property.getTargetName(), - property.getSourceType(), - property.getSourceName() - ), - reverseMethod.getExecutable() - ); - } } private String getIterableConversionString(Conversions conversions, Type sourceElementType, Type targetElementType, @@ -460,45 +370,24 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return usedMapperTypes; } - private MappingMethodReference getElementMappingMethod(Iterable methods, Method method) { - Method elementMappingMethod = null; + private MappingMethodReference getMappingMethodReference(Iterable methods, Type parameterType, + Type returnType) { for ( Method oneMethod : methods ) { - if ( oneMethod.getSourceType().equals( method.getSourceType().getElementType() ) ) { - elementMappingMethod = oneMethod; - break; + if ( oneMethod.getSourceType().equals( parameterType ) && oneMethod.getTargetType().equals( returnType ) ) { + return new MappingMethodReference( + oneMethod.getDeclaringMapper(), + oneMethod.getName(), + oneMethod.getParameterName(), + oneMethod.getSourceType(), + oneMethod.getTargetType() + ); } } - return elementMappingMethod == null ? null : new MappingMethodReference( - elementMappingMethod.getDeclaringMapper(), - elementMappingMethod.getName(), - elementMappingMethod.getParameterName(), - elementMappingMethod.getSourceType(), - elementMappingMethod.getTargetType() - ); - } - private Method getPropertyMappingMethod(Iterable rawMethods, MappedProperty property) { - for ( Method oneMethod : rawMethods ) { - if ( oneMethod.getSourceType().equals( property.getSourceType() ) && oneMethod.getTargetType() - .equals( property.getTargetType() ) ) { - return oneMethod; - } - } return null; } - private Method getReversePropertyMappingMethod(Iterable methods, MappedProperty property) { - for ( Method method : methods ) { - if ( method.getSourceType().equals( property.getTargetType() ) && method.getTargetType() - .equals( property.getSourceType() ) ) { - return method; - } - } - return null; - } - - private Method getReverseMappingMethod(List rawMethods, - Method method) { + private Method getReverseMappingMethod(List rawMethods, Method method) { for ( Method oneMethod : rawMethods ) { if ( oneMethod.reverses( method ) ) { return oneMethod; @@ -588,7 +477,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { returnType, sourceProperties, targetProperties, - retrieveMappedProperties( method, sourceProperties, targetProperties ) + getMappings( method ) ) ); } @@ -627,18 +516,19 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { * method. * * @param method The method of interest - * @param targetProperties - * @param sourceProperties * * @return All mapped properties for the given method */ - private List retrieveMappedProperties(ExecutableElement method, Set sourceProperties, - Set targetProperties) { + private List retrieveMappedProperties(Method method) { + Map mappings = method.getMappings(); - Map mappings = getMappings( method ); - - TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getReturnType() ); - TypeElement parameterElement = (TypeElement) typeUtils.asElement( method.getParameters().get( 0 ).asType() ); + TypeElement returnTypeElement = (TypeElement) typeUtils.asElement( method.getExecutable().getReturnType() ); + TypeElement parameterElement = (TypeElement) typeUtils.asElement( + method.getExecutable() + .getParameters() + .get( 0 ) + .asType() + ); List properties = new ArrayList(); @@ -648,14 +538,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { List targetSetters = Filters.setterMethodsIn( elementUtils.getAllMembers( returnTypeElement ) ); - List sourceSetters = Filters.setterMethodsIn( - elementUtils.getAllMembers( parameterElement ) - ); - List targetGetters = Filters.getterMethodsIn( - elementUtils.getAllMembers( returnTypeElement ) - ); - reportErrorIfMappedPropertiesDontExist( method, sourceProperties, targetProperties, mappings ); + reportErrorIfMappedPropertiesDontExist( method ); for ( ExecutableElement getterMethod : sourceGetters ) { String sourcePropertyName = Executables.getPropertyName( getterMethod ); @@ -665,22 +549,12 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { String targetPropertyName = Executables.getPropertyName( setterMethod ); if ( targetPropertyName.equals( mapping != null ? mapping.getTargetName() : sourcePropertyName ) ) { - ExecutableElement correspondingSetter = Executables.getCorrespondingPropertyAccessor( - getterMethod, - sourceSetters - ); - ExecutableElement correspondingGetter = Executables.getCorrespondingPropertyAccessor( - setterMethod, - targetGetters - ); properties.add( new MappedProperty( sourcePropertyName, getterMethod.getSimpleName().toString(), - correspondingSetter != null ? correspondingSetter.getSimpleName().toString() : null, retrieveReturnType( getterMethod ), mapping != null ? mapping.getTargetName() : targetPropertyName, - correspondingGetter != null ? correspondingGetter.getSimpleName().toString() : null, setterMethod.getSimpleName().toString(), retrieveParameter( setterMethod ).getType() ) @@ -692,29 +566,26 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return properties; } - private void reportErrorIfMappedPropertiesDontExist(ExecutableElement method, Set sourcePropertyNames, - Set targetPropertyNames, - Map mappings) { - - for ( Mapping mappedProperty : mappings.values() ) { - if ( !sourcePropertyNames.contains( mappedProperty.getSourceName() ) ) { + private void reportErrorIfMappedPropertiesDontExist(Method method) { + for ( Mapping mappedProperty : method.getMappings().values() ) { + if ( !method.getSourceProperties().contains( mappedProperty.getSourceName() ) ) { printMessage( ReportingPolicy.ERROR, String.format( "Unknown property \"%s\" in parameter type %s.", mappedProperty.getSourceName(), - retrieveParameter( method ).getType() - ), method, mappedProperty.getMirror(), mappedProperty.getSourceAnnotationValue() + method.getSourceType() + ), method.getExecutable(), mappedProperty.getMirror(), mappedProperty.getSourceAnnotationValue() ); } - if ( !targetPropertyNames.contains( mappedProperty.getTargetName() ) ) { + if ( !method.getTargetProeprties().contains( mappedProperty.getTargetName() ) ) { printMessage( ReportingPolicy.ERROR, String.format( "Unknown property \"%s\" in return type %s.", mappedProperty.getTargetName(), - retrieveReturnType( method ) - ), method, mappedProperty.getMirror(), mappedProperty.getTargetAnnotationValue() + method.getTargetType() + ), method.getExecutable(), mappedProperty.getMirror(), mappedProperty.getTargetAnnotationValue() ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/BeanMapping.java b/processor/src/main/java/org/mapstruct/ap/model/BeanMapping.java deleted file mode 100644 index 66f92e346..000000000 --- a/processor/src/main/java/org/mapstruct/ap/model/BeanMapping.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) - * and/or other contributors as indicated by the @authors tag. See the - * copyright.txt file in the distribution for a full listing of all - * contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.mapstruct.ap.model; - -public class BeanMapping extends AbstractModelElement { - - private final MappingMethod mappingMethod; - private final MappingMethod reverseMappingMethod; - - public BeanMapping(MappingMethod mappingMethod, MappingMethod reverseMappingMethod) { - this.mappingMethod = mappingMethod; - this.reverseMappingMethod = reverseMappingMethod; - } - - public MappingMethod getMappingMethod() { - return mappingMethod; - } - - public MappingMethod getReverseMappingMethod() { - return reverseMappingMethod; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder( "BeanMapping {" ); - sb.append( "\n mappingMethod=" + mappingMethod.toString().replaceAll( "\n", "\n " ) + ',' ); - sb.append( "\n reverseMappingMethod=" + reverseMappingMethod + ',' ); - sb.append( "\n}" ); - - return sb.toString(); - } -} diff --git a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java index ddeaeb66f..83db622cc 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java +++ b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java @@ -29,18 +29,18 @@ public class Mapper extends AbstractModelElement { private final String packageName; private final String interfaceName; private final String implementationName; - private final List beanMappings; + private final List mappingMethods; private final List usedMapperTypes; private final Options options; private final SortedSet importedTypes; public Mapper(String packageName, String interfaceName, - String implementationName, List beanMappings, List usedMapperTypes, + String implementationName, List mappingMethods, List usedMapperTypes, Options options) { this.packageName = packageName; this.interfaceName = interfaceName; this.implementationName = implementationName; - this.beanMappings = beanMappings; + this.mappingMethods = mappingMethods; this.usedMapperTypes = usedMapperTypes; this.options = options; this.importedTypes = determineImportedTypes(); @@ -50,9 +50,9 @@ public class Mapper extends AbstractModelElement { SortedSet importedTypes = new TreeSet(); importedTypes.add( Type.forClass( Generated.class ) ); - for ( BeanMapping beanMapping : beanMappings ) { + for ( MappingMethod mappingMethod : mappingMethods ) { - for ( Type type : beanMapping.getMappingMethod().getReferencedTypes() ) { + for ( Type type : mappingMethod.getReferencedTypes() ) { addWithDependents( importedTypes, type ); } } @@ -85,7 +85,7 @@ public class Mapper extends AbstractModelElement { sb.append( "\n implementationName='" + implementationName + "\'," ); sb.append( "\n beanMappings=[" ); - for ( BeanMapping beanMapping : beanMappings ) { + for ( MappingMethod beanMapping : mappingMethods ) { sb.append( "\n " + beanMapping.toString().replaceAll( "\n", "\n " ) ); } sb.append( "\n ]" ); @@ -107,8 +107,8 @@ public class Mapper extends AbstractModelElement { return implementationName; } - public List getBeanMappings() { - return beanMappings; + public List getMappingMethods() { + return mappingMethods; } public List getUsedMapperTypes() { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/MappedProperty.java b/processor/src/main/java/org/mapstruct/ap/model/source/MappedProperty.java index 6988f8b3a..3e6ee0748 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/MappedProperty.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/MappedProperty.java @@ -30,23 +30,17 @@ public class MappedProperty { private final String sourceName; private final String sourceReadAccessorName; - private final String sourceWriteAccessorName; private final Type sourceType; private final String targetName; - private final String targetReadAccessorName; private final String targetWriteAccessorName; private final Type targetType; - public MappedProperty(String sourceName, String sourceReadAccessorName, String sourceWriteAccessorName, - Type sourceType, String targetName, String targetReadAccessorName, - String targetWriteAccessorName, - Type targetType) { + public MappedProperty(String sourceName, String sourceReadAccessorName, Type sourceType, String targetName, + String targetWriteAccessorName, Type targetType) { this.sourceName = sourceName; this.sourceReadAccessorName = sourceReadAccessorName; - this.sourceWriteAccessorName = sourceWriteAccessorName; this.sourceType = sourceType; this.targetName = targetName; - this.targetReadAccessorName = targetReadAccessorName; this.targetWriteAccessorName = targetWriteAccessorName; this.targetType = targetType; } @@ -59,10 +53,6 @@ public class MappedProperty { return sourceReadAccessorName; } - public String getSourceWriteAccessorName() { - return sourceWriteAccessorName; - } - public Type getSourceType() { return sourceType; } @@ -71,10 +61,6 @@ public class MappedProperty { return targetName; } - public String getTargetReadAccessorName() { - return targetReadAccessorName; - } - public String getTargetWriteAccessorName() { return targetWriteAccessorName; } 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 cce5c3a4c..e46106456 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 @@ -89,6 +89,10 @@ public class Mapping { return targetAnnotationValue; } + public Mapping reverse() { + return new Mapping( targetName, sourceName, mirror, sourceAnnotationValue, targetAnnotationValue ); + } + @Override public String toString() { return "Mapping {" + diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java index 470efbab0..13efa0fbe 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java @@ -19,7 +19,7 @@ package org.mapstruct.ap.model.source; import java.util.Collections; -import java.util.List; +import java.util.Map; import java.util.Set; import javax.lang.model.element.ExecutableElement; @@ -38,15 +38,15 @@ public class Method { private final String parameterName; private final Type sourceType; private final Type targetType; - private Set sourceProperties; - private Set targetProeprties; - private final List mappedProperties; + private final Set sourceProperties; + private final Set targetProeprties; + private Map mappings; public static Method forMethodRequiringImplementation(ExecutableElement executable, String parameterName, Type sourceType, Type targetType, Set sourceProperties, Set targetProperties, - List mappedProperties) { + Map mappings) { return new Method( null, @@ -56,13 +56,12 @@ public class Method { targetType, sourceProperties, targetProperties, - mappedProperties + mappings ); } public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, String parameterName, - Type sourceType, - Type targetType) { + Type sourceType, Type targetType) { return new Method( declaringMapper, @@ -72,13 +71,13 @@ public class Method { targetType, Collections.emptySet(), Collections.emptySet(), - Collections.emptyList() + Collections.emptyMap() ); } private Method(Type declaringMapper, ExecutableElement executable, String parameterName, Type sourceType, Type targetType, Set sourceProperties, Set targetProperties, - List mappedProperties) { + Map mappings) { this.declaringMapper = declaringMapper; this.executable = executable; this.parameterName = parameterName; @@ -86,7 +85,7 @@ public class Method { this.targetType = targetType; this.sourceProperties = sourceProperties; this.targetProeprties = targetProperties; - this.mappedProperties = mappedProperties; + this.mappings = mappings; } /** @@ -128,8 +127,12 @@ public class Method { return targetProeprties; } - public List getMappedProperties() { - return mappedProperties; + public Map getMappings() { + return mappings; + } + + public void setMappings(Map mappings) { + this.mappings = mappings; } public boolean reverses(Method method) { @@ -138,6 +141,10 @@ public class Method { equals( targetType, method.getSourceType() ); } + public boolean isIterableMapping() { + return sourceType.isIterableType() && targetType.isIterableType(); + } + private boolean equals(Object o1, Object o2) { return ( o1 == null && o2 == null ) || ( o1 != null ) && o1.equals( o2 ); } diff --git a/processor/src/main/java/org/mapstruct/ap/util/Executables.java b/processor/src/main/java/org/mapstruct/ap/util/Executables.java index f772dcc42..440db5c8a 100644 --- a/processor/src/main/java/org/mapstruct/ap/util/Executables.java +++ b/processor/src/main/java/org/mapstruct/ap/util/Executables.java @@ -88,30 +88,6 @@ public class Executables { throw new IllegalArgumentException( "Executable " + getterOrSetterMethod + " is not getter or setter method." ); } - /** - * Returns that setter or getter from the given list of executables which - * corresponds to the given getter or setter method. - * - * @param getterOrSetter The getter or setter method of interest. - * @param elements A list of executables to retrieve the corresponding accessor - * from. - * - * @return The setter corresponding to the given getter or the getter - * corresponding to the given getter - */ - public static ExecutableElement getCorrespondingPropertyAccessor(ExecutableElement getterOrSetter, - List elements) { - String propertyName = getPropertyName( getterOrSetter ); - - for ( ExecutableElement method : elements ) { - if ( getPropertyName( method ).equals( propertyName ) ) { - return method; - } - } - - return null; - } - public static Set getPropertyNames(List propertyAccessors) { Set propertyNames = new HashSet(); diff --git a/processor/src/main/resources/org.mapstruct.ap.model.BeanMapping.ftl b/processor/src/main/resources/org.mapstruct.ap.model.BeanMapping.ftl deleted file mode 100644 index cc7d33fed..000000000 --- a/processor/src/main/resources/org.mapstruct.ap.model.BeanMapping.ftl +++ /dev/null @@ -1,25 +0,0 @@ -<#-- - - Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/) - and/or other contributors as indicated by the @authors tag. See the - copyright.txt file in the distribution for a full listing of all - contributors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - ---> - <@includeModel object=mappingMethod/> - - <#if reverseMappingMethod??> - <@includeModel object=reverseMappingMethod/> - diff --git a/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl b/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl index a7a39ac1c..b5f90d200 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl @@ -34,7 +34,7 @@ public class ${implementationName} implements ${interfaceName} { private final ${mapperType.name} ${mapperType.name?uncap_first} = new ${mapperType.name}(); -<#list beanMappings as beanMapping> -<@includeModel object=beanMapping/> +<#list mappingMethods as mappingMethod> +<@includeModel object=mappingMethod/> }