diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index a6ee19c83..1ae8c1407 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -244,6 +244,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { propertyMappings.add( new PropertyMapping( + method.getParameterName(), + Introspector.decapitalize( method.getTargetType().getName() ), property.getSourceReadAccessorName(), property.getSourceType(), property.getTargetWriteAccessorName(), @@ -262,27 +264,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { ) ); - reversePropertyMappings.add( - new PropertyMapping( - property.getTargetReadAccessorName(), - property.getTargetType(), - property.getSourceWriteAccessorName(), - property.getSourceType(), - reversePropertyMappingMethod != null ? new MappingMethodReference( - reversePropertyMappingMethod.getDeclaringMapper(), - reversePropertyMappingMethod.getName(), - reversePropertyMappingMethod.getParameterName(), + if ( rawReverseMappingMethod != null ) { + reversePropertyMappings.add( + new PropertyMapping( + rawReverseMappingMethod.getParameterName(), + Introspector.decapitalize( rawReverseMappingMethod.getTargetType().getName() ), + property.getTargetReadAccessorName(), property.getTargetType(), - property.getSourceType() - ) : null, - conversion != null && rawReverseMappingMethod != null ? conversion.from( - rawReverseMappingMethod.getParameterName() + "." + - property.getTargetReadAccessorName() + - "()", - property.getSourceType() - ) : null - ) - ); + 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( diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index 3103c897e..5c65d2808 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -26,36 +26,49 @@ package org.mapstruct.ap.model; * * @author Gunnar Morling */ -public class PropertyMapping { +public class PropertyMapping extends AbstractModelElement { - private final String sourceReadAccessorName; + private final String sourceBeanName; + private final String targetBeanName; + private final String sourceAccessorName; private final Type sourceType; - private final String targetWriteAccessorName; + private final String targetAccessorName; private final Type targetType; private final MappingMethodReference mappingMethod; - private final String toConversion; + private final String conversion; - public PropertyMapping(String sourceReadAccessorName, Type sourceType, String targetWriteAccessorName, - Type targetType, MappingMethodReference mappingMethod, String toConversion) { - this.sourceReadAccessorName = sourceReadAccessorName; + public PropertyMapping(String sourceBeanName, String targetBeanName, String sourceAccessorName, Type sourceType, + String targetAccessorName, Type targetType, MappingMethodReference mappingMethod, + String conversion) { + this.sourceBeanName = sourceBeanName; + this.targetBeanName = targetBeanName; + this.sourceAccessorName = sourceAccessorName; this.sourceType = sourceType; - this.targetWriteAccessorName = targetWriteAccessorName; + this.targetAccessorName = targetAccessorName; this.targetType = targetType; this.mappingMethod = mappingMethod; - this.toConversion = toConversion; + this.conversion = conversion; } - public String getSourceReadAccessorName() { - return sourceReadAccessorName; + public String getSourceBeanName() { + return sourceBeanName; + } + + public String getTargetBeanName() { + return targetBeanName; + } + + public String getSourceAccessorName() { + return sourceAccessorName; } public Type getSourceType() { return sourceType; } - public String getTargetWriteAccessorName() { - return targetWriteAccessorName; + public String getTargetAccessorName() { + return targetAccessorName; } public Type getTargetType() { @@ -66,19 +79,19 @@ public class PropertyMapping { return mappingMethod; } - public String getToConversion() { - return toConversion; + public String getConversion() { + return conversion; } @Override public String toString() { return "PropertyMapping {" + - "\n sourceName='" + sourceReadAccessorName + "\'," + + "\n sourceName='" + sourceAccessorName + "\'," + "\n sourceType=" + sourceType + "," + - "\n targetName='" + targetWriteAccessorName + "\'," + + "\n targetName='" + targetAccessorName + "\'," + "\n targetType=" + targetType + "," + "\n mappingMethod=" + mappingMethod + "," + - "\n toConversion='" + toConversion + "\'," + + "\n Conversion='" + conversion + "\'," + "\n}"; } } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl new file mode 100644 index 000000000..dec73c084 --- /dev/null +++ b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl @@ -0,0 +1,42 @@ +<#-- + + 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. + +--> + <#-- a) invoke mapping method --> + <#if mappingMethod??> + ${targetBeanName}.${targetAccessorName}( <#if mappingMethod.declaringMapper??>${mappingMethod.declaringMapper.name?uncap_first}.${mappingMethod.name}( ${sourceBeanName}.${sourceAccessorName}() ) ); + <#-- b) simple conversion --> + <#elseif conversion??> + <#if sourceType.primitive == false> + if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { + ${targetBeanName}.${targetAccessorName}( ${conversion} ); + } + <#else> + ${targetBeanName}.${targetAccessorName}( ${conversion} ); + + <#-- c) simply set --> + <#else> + <#if targetType.collectionType == true> + if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { + ${targetBeanName}.${targetAccessorName}( new <#if targetType.collectionImplementationType??>${targetType.collectionImplementationType.name}<#else>${targetType.name}<#if targetType.elementType??><${targetType.elementType.name}>( ${sourceBeanName}.${sourceAccessorName}() ) ); + } + <#else> + ${targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorName}() ); + + diff --git a/processor/src/main/resources/org.mapstruct.ap.model.SimpleMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.SimpleMappingMethod.ftl index db66167a4..11acaeb51 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.SimpleMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.SimpleMappingMethod.ftl @@ -26,44 +26,9 @@ ${targetType.name} ${targetType.name?uncap_first} = new ${targetType.name}(); - <#list propertyMappings as propertyMapping> - <@simpleMap - sourceBeanName=parameterName - sourceType=propertyMapping.sourceType - sourceAccessorName=propertyMapping.sourceReadAccessorName - targetBeanName=targetType.name?uncap_first - targetType=propertyMapping.targetType - targetAccessorName=propertyMapping.targetWriteAccessorName - conversion=propertyMapping.toConversion - mappingMethod=propertyMapping.mappingMethod - /> - + <#list propertyMappings as propertyMapping> + <@includeModel object=propertyMapping/> + return ${targetType.name?uncap_first}; } - -<#-- Generates the mapping of one bean property --> -<#macro simpleMap sourceBeanName sourceType sourceAccessorName targetBeanName targetType targetAccessorName conversion="" mappingMethod=""> - <#-- a) invoke mapping method --> - <#if mappingMethod != ""> - ${targetBeanName}.${targetAccessorName}( <#if mappingMethod.declaringMapper??>${mappingMethod.declaringMapper.name?uncap_first}.${mappingMethod.name}( ${sourceBeanName}.${sourceAccessorName}() ) ); - <#-- b) simple conversion --> - <#elseif conversion != ""> - <#if sourceType.primitive == false> - if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { - ${targetBeanName}.${targetAccessorName}( ${conversion} ); - } - <#else> - ${targetBeanName}.${targetAccessorName}( ${conversion} ); - - <#-- c) simply set --> - <#else> - <#if targetType.collectionType == true> - if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { - ${targetBeanName}.${targetAccessorName}( new <#if targetType.collectionImplementationType??>${targetType.collectionImplementationType.name}<#else>${targetType.name}<#if targetType.elementType??><${targetType.elementType.name}>( ${sourceBeanName}.${sourceAccessorName}() ) ); - } - <#else> - ${targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorName}() ); - - -