#32 Extracting template for property mappings

This commit is contained in:
Gunnar Morling 2013-06-22 11:45:04 +02:00
parent eeb811e450
commit 335b9d77ac
4 changed files with 102 additions and 76 deletions

View File

@ -244,6 +244,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
propertyMappings.add( propertyMappings.add(
new PropertyMapping( new PropertyMapping(
method.getParameterName(),
Introspector.decapitalize( method.getTargetType().getName() ),
property.getSourceReadAccessorName(), property.getSourceReadAccessorName(),
property.getSourceType(), property.getSourceType(),
property.getTargetWriteAccessorName(), property.getTargetWriteAccessorName(),
@ -262,8 +264,11 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
) )
); );
if ( rawReverseMappingMethod != null ) {
reversePropertyMappings.add( reversePropertyMappings.add(
new PropertyMapping( new PropertyMapping(
rawReverseMappingMethod.getParameterName(),
Introspector.decapitalize( rawReverseMappingMethod.getTargetType().getName() ),
property.getTargetReadAccessorName(), property.getTargetReadAccessorName(),
property.getTargetType(), property.getTargetType(),
property.getSourceWriteAccessorName(), property.getSourceWriteAccessorName(),
@ -284,6 +289,7 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
) )
); );
} }
}
MappingMethod mappingMethod = new SimpleMappingMethod( MappingMethod mappingMethod = new SimpleMappingMethod(
method.getName(), method.getName(),

View File

@ -26,36 +26,49 @@ package org.mapstruct.ap.model;
* *
* @author Gunnar Morling * @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 Type sourceType;
private final String targetWriteAccessorName; private final String targetAccessorName;
private final Type targetType; private final Type targetType;
private final MappingMethodReference mappingMethod; private final MappingMethodReference mappingMethod;
private final String toConversion; private final String conversion;
public PropertyMapping(String sourceReadAccessorName, Type sourceType, String targetWriteAccessorName, public PropertyMapping(String sourceBeanName, String targetBeanName, String sourceAccessorName, Type sourceType,
Type targetType, MappingMethodReference mappingMethod, String toConversion) { String targetAccessorName, Type targetType, MappingMethodReference mappingMethod,
this.sourceReadAccessorName = sourceReadAccessorName; String conversion) {
this.sourceBeanName = sourceBeanName;
this.targetBeanName = targetBeanName;
this.sourceAccessorName = sourceAccessorName;
this.sourceType = sourceType; this.sourceType = sourceType;
this.targetWriteAccessorName = targetWriteAccessorName; this.targetAccessorName = targetAccessorName;
this.targetType = targetType; this.targetType = targetType;
this.mappingMethod = mappingMethod; this.mappingMethod = mappingMethod;
this.toConversion = toConversion; this.conversion = conversion;
} }
public String getSourceReadAccessorName() { public String getSourceBeanName() {
return sourceReadAccessorName; return sourceBeanName;
}
public String getTargetBeanName() {
return targetBeanName;
}
public String getSourceAccessorName() {
return sourceAccessorName;
} }
public Type getSourceType() { public Type getSourceType() {
return sourceType; return sourceType;
} }
public String getTargetWriteAccessorName() { public String getTargetAccessorName() {
return targetWriteAccessorName; return targetAccessorName;
} }
public Type getTargetType() { public Type getTargetType() {
@ -66,19 +79,19 @@ public class PropertyMapping {
return mappingMethod; return mappingMethod;
} }
public String getToConversion() { public String getConversion() {
return toConversion; return conversion;
} }
@Override @Override
public String toString() { public String toString() {
return "PropertyMapping {" + return "PropertyMapping {" +
"\n sourceName='" + sourceReadAccessorName + "\'," + "\n sourceName='" + sourceAccessorName + "\'," +
"\n sourceType=" + sourceType + "," + "\n sourceType=" + sourceType + "," +
"\n targetName='" + targetWriteAccessorName + "\'," + "\n targetName='" + targetAccessorName + "\'," +
"\n targetType=" + targetType + "," + "\n targetType=" + targetType + "," +
"\n mappingMethod=" + mappingMethod + "," + "\n mappingMethod=" + mappingMethod + "," +
"\n toConversion='" + toConversion + "\'," + "\n Conversion='" + conversion + "\'," +
"\n}"; "\n}";
} }
} }

View File

@ -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}.</#if>${mappingMethod.name}( ${sourceBeanName}.${sourceAccessorName}() ) );
<#-- b) simple conversion -->
<#elseif conversion??>
<#if sourceType.primitive == false>
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
${targetBeanName}.${targetAccessorName}( ${conversion} );
}
<#else>
${targetBeanName}.${targetAccessorName}( ${conversion} );
</#if>
<#-- 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><#if targetType.elementType??><${targetType.elementType.name}></#if>( ${sourceBeanName}.${sourceAccessorName}() ) );
}
<#else>
${targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorName}() );
</#if>
</#if>

View File

@ -27,43 +27,8 @@
${targetType.name} ${targetType.name?uncap_first} = new ${targetType.name}(); ${targetType.name} ${targetType.name?uncap_first} = new ${targetType.name}();
<#list propertyMappings as propertyMapping> <#list propertyMappings as propertyMapping>
<@simpleMap <@includeModel object=propertyMapping/>
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> </#list>
return ${targetType.name?uncap_first}; 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}.</#if>${mappingMethod.name}( ${sourceBeanName}.${sourceAccessorName}() ) );
<#-- b) simple conversion -->
<#elseif conversion != "">
<#if sourceType.primitive == false>
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
${targetBeanName}.${targetAccessorName}( ${conversion} );
}
<#else>
${targetBeanName}.${targetAccessorName}( ${conversion} );
</#if>
<#-- 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><#if targetType.elementType??><${targetType.elementType.name}></#if>( ${sourceBeanName}.${sourceAccessorName}() ) );
}
<#else>
${targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorName}() );
</#if>
</#if>
</#macro>