mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#32 Extracting template for property mappings
This commit is contained in:
parent
eeb811e450
commit
335b9d77ac
@ -244,6 +244,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
|
||||
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<Void, Void> {
|
||||
)
|
||||
);
|
||||
|
||||
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(
|
||||
|
@ -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}";
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
@ -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>
|
||||
<#list propertyMappings as propertyMapping>
|
||||
<@includeModel object=propertyMapping/>
|
||||
</#list>
|
||||
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user