mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#32 Creating dedicated template for MappingMethod
This commit is contained in:
parent
c071434c6e
commit
517817fbfc
@ -193,27 +193,31 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
continue;
|
||||
}
|
||||
|
||||
MappingMethod mappingMethod = new MappingMethod(
|
||||
method.getDeclaringMapper(),
|
||||
method.getName(),
|
||||
method.getParameterName(),
|
||||
getElementMappingMethod( methods, method )
|
||||
);
|
||||
|
||||
MappingMethod reverseMappingMethod = null;
|
||||
Method rawReverseMappingMethod = getReverseMappingMethod( methods, method );
|
||||
if ( rawReverseMappingMethod != null ) {
|
||||
processedMethods.add( rawReverseMappingMethod );
|
||||
|
||||
reverseMappingMethod = new MappingMethod(
|
||||
rawReverseMappingMethod.getDeclaringMapper(),
|
||||
rawReverseMappingMethod.getName(),
|
||||
rawReverseMappingMethod.getParameterName(),
|
||||
getElementMappingMethod( methods, rawReverseMappingMethod )
|
||||
String toConversionString = null;
|
||||
String fromConversionString = null;
|
||||
|
||||
boolean isIterableMapping = method.getSourceType().isIterableType() && method.getTargetType()
|
||||
.isIterableType();
|
||||
|
||||
if ( isIterableMapping ) {
|
||||
toConversionString = getIterableConversionString(
|
||||
conversions,
|
||||
method.getSourceType().getElementType(),
|
||||
method.getTargetType().getElementType(),
|
||||
true
|
||||
);
|
||||
fromConversionString = getIterableConversionString(
|
||||
conversions,
|
||||
method.getTargetType().getElementType(),
|
||||
method.getSourceType().getElementType(),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>();
|
||||
List<PropertyMapping> reversePropertyMappings = new ArrayList<PropertyMapping>();
|
||||
Set<String> mappedSourceProperties = new HashSet<String>();
|
||||
Set<String> mappedTargetProperties = new HashSet<String>();
|
||||
|
||||
@ -237,35 +241,71 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
propertyMappings.add(
|
||||
new PropertyMapping(
|
||||
property.getSourceReadAccessorName(),
|
||||
property.getSourceWriteAccessorName(),
|
||||
property.getSourceType(),
|
||||
property.getTargetReadAccessorName(),
|
||||
property.getTargetWriteAccessorName(),
|
||||
property.getTargetType(),
|
||||
propertyMappingMethod != null ? new MappingMethod(
|
||||
propertyMappingMethod.getDeclaringMapper(),
|
||||
propertyMappingMethod.getName(),
|
||||
propertyMappingMethod.getParameterName()
|
||||
propertyMappingMethod.getParameterName(),
|
||||
property.getSourceType(),
|
||||
property.getTargetType()
|
||||
) : null,
|
||||
conversion != null ? conversion.to(
|
||||
method.getParameterName() + "." + property.getSourceReadAccessorName() + "()",
|
||||
property.getTargetType()
|
||||
) : null
|
||||
)
|
||||
);
|
||||
|
||||
reversePropertyMappings.add(
|
||||
new PropertyMapping(
|
||||
property.getTargetReadAccessorName(),
|
||||
property.getTargetType(),
|
||||
property.getSourceWriteAccessorName(),
|
||||
property.getSourceType(),
|
||||
reversePropertyMappingMethod != null ? new MappingMethod(
|
||||
reversePropertyMappingMethod.getDeclaringMapper(),
|
||||
reversePropertyMappingMethod.getName(),
|
||||
reversePropertyMappingMethod.getParameterName()
|
||||
reversePropertyMappingMethod.getParameterName(),
|
||||
property.getTargetType(),
|
||||
property.getSourceType()
|
||||
) : null,
|
||||
conversion != null ? conversion.to(
|
||||
mappingMethod.getParameterName() + "." + property.getSourceReadAccessorName() + "()",
|
||||
property.getTargetType()
|
||||
) : null,
|
||||
conversion != null && reverseMappingMethod != null ? conversion.from(
|
||||
reverseMappingMethod.getParameterName() + "." + property.getTargetReadAccessorName() + "()",
|
||||
conversion != null && rawReverseMappingMethod != null ? conversion.from(
|
||||
rawReverseMappingMethod.getParameterName() + "." + property.getTargetReadAccessorName() +
|
||||
"()",
|
||||
property.getSourceType()
|
||||
) : null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
boolean isIterableMapping = method.getSourceType().isIterableType() && method.getTargetType()
|
||||
.isIterableType();
|
||||
MappingMethod mappingMethod = new MappingMethod(
|
||||
method.getDeclaringMapper(),
|
||||
method.getName(),
|
||||
method.getParameterName(),
|
||||
method.getSourceType(),
|
||||
method.getTargetType(),
|
||||
propertyMappings,
|
||||
getElementMappingMethod( methods, method ),
|
||||
toConversionString
|
||||
);
|
||||
|
||||
MappingMethod reverseMappingMethod = null;
|
||||
if ( rawReverseMappingMethod != null ) {
|
||||
processedMethods.add( rawReverseMappingMethod );
|
||||
|
||||
reverseMappingMethod = new MappingMethod(
|
||||
rawReverseMappingMethod.getDeclaringMapper(),
|
||||
rawReverseMappingMethod.getName(),
|
||||
rawReverseMappingMethod.getParameterName(),
|
||||
method.getTargetType(),
|
||||
method.getSourceType(),
|
||||
reversePropertyMappings,
|
||||
getElementMappingMethod( methods, rawReverseMappingMethod ),
|
||||
fromConversionString
|
||||
);
|
||||
}
|
||||
|
||||
if ( mappingMethod.isGenerationRequired() && !isIterableMapping ) {
|
||||
reportErrorForUnmappedTargetPropertiesIfRequired(
|
||||
@ -284,32 +324,9 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
);
|
||||
}
|
||||
|
||||
String toConversionString = null;
|
||||
String fromConversionString = null;
|
||||
|
||||
if ( isIterableMapping ) {
|
||||
toConversionString = getIterableConversionString(
|
||||
conversions,
|
||||
method.getSourceType().getElementType(),
|
||||
method.getTargetType().getElementType(),
|
||||
true
|
||||
);
|
||||
fromConversionString = getIterableConversionString(
|
||||
conversions,
|
||||
method.getTargetType().getElementType(),
|
||||
method.getSourceType().getElementType(),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
BeanMapping mapping = new BeanMapping(
|
||||
method.getSourceType(),
|
||||
method.getTargetType(),
|
||||
propertyMappings,
|
||||
mappingMethod,
|
||||
reverseMappingMethod,
|
||||
toConversionString,
|
||||
fromConversionString
|
||||
reverseMappingMethod
|
||||
);
|
||||
|
||||
mappings.add( mapping );
|
||||
@ -421,7 +438,9 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
return elementMappingMethod == null ? null : new MappingMethod(
|
||||
elementMappingMethod.getDeclaringMapper(),
|
||||
elementMappingMethod.getName(),
|
||||
elementMappingMethod.getParameterName()
|
||||
elementMappingMethod.getParameterName(),
|
||||
elementMappingMethod.getSourceType(),
|
||||
elementMappingMethod.getTargetType()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -18,42 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BeanMapping extends AbstractModelElement {
|
||||
|
||||
private final Type sourceType;
|
||||
private final Type targetType;
|
||||
private final List<PropertyMapping> propertyMappings;
|
||||
private final MappingMethod mappingMethod;
|
||||
private final MappingMethod reverseMappingMethod;
|
||||
private final boolean isIterableMapping;
|
||||
private final String toConversion;
|
||||
private final String fromConversion;
|
||||
|
||||
public BeanMapping(Type sourceType, Type targetType, List<PropertyMapping> propertyMappings,
|
||||
MappingMethod mappingMethod,
|
||||
MappingMethod reverseMappingMethod, String toConversion, String fromConversion) {
|
||||
this.sourceType = sourceType;
|
||||
this.targetType = targetType;
|
||||
this.propertyMappings = propertyMappings;
|
||||
public BeanMapping(MappingMethod mappingMethod, MappingMethod reverseMappingMethod) {
|
||||
this.mappingMethod = mappingMethod;
|
||||
this.reverseMappingMethod = reverseMappingMethod;
|
||||
this.isIterableMapping = sourceType.isIterableType() && targetType.isIterableType();
|
||||
this.toConversion = toConversion;
|
||||
this.fromConversion = fromConversion;
|
||||
}
|
||||
|
||||
public Type getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
public Type getTargetType() {
|
||||
return targetType;
|
||||
}
|
||||
|
||||
public List<PropertyMapping> getPropertyMappings() {
|
||||
return propertyMappings;
|
||||
}
|
||||
|
||||
public MappingMethod getMappingMethod() {
|
||||
@ -64,37 +36,11 @@ public class BeanMapping extends AbstractModelElement {
|
||||
return reverseMappingMethod;
|
||||
}
|
||||
|
||||
public boolean isIterableMapping() {
|
||||
return isIterableMapping;
|
||||
}
|
||||
|
||||
public String getToConversion() {
|
||||
return toConversion;
|
||||
}
|
||||
|
||||
public String getFromConversion() {
|
||||
return fromConversion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder( "BeanMapping {" );
|
||||
|
||||
sb.append( "\n sourceType=" + sourceType + ',' );
|
||||
sb.append( "\n targetType=" + targetType + ',' );
|
||||
|
||||
sb.append( "\n propertyMappings=[\n" );
|
||||
|
||||
for ( PropertyMapping propertyMapping : propertyMappings ) {
|
||||
sb.append( " " + propertyMapping.toString().replaceAll( "\n", "\n " ) );
|
||||
}
|
||||
sb.append( "\n ]" );
|
||||
|
||||
sb.append( "\n mappingMethod=" + mappingMethod.toString().replaceAll( "\n", "\n " ) + ',' );
|
||||
sb.append( "\n reverseMappingMethod=" + reverseMappingMethod + ',' );
|
||||
sb.append( "\n toConversion=" + toConversion + ',' );
|
||||
sb.append( "\n fromConversion=" + fromConversion + ',' );
|
||||
sb.append( "\n isIterableMapping=" + isIterableMapping );
|
||||
sb.append( "\n}" );
|
||||
|
||||
return sb.toString();
|
||||
|
@ -51,10 +51,10 @@ public class Mapper extends AbstractModelElement {
|
||||
importedTypes.add( Type.forClass( Generated.class ) );
|
||||
|
||||
for ( BeanMapping beanMapping : beanMappings ) {
|
||||
addWithDependents( importedTypes, beanMapping.getSourceType() );
|
||||
addWithDependents( importedTypes, beanMapping.getTargetType() );
|
||||
addWithDependents( importedTypes, beanMapping.getMappingMethod().getSourceType() );
|
||||
addWithDependents( importedTypes, beanMapping.getMappingMethod().getTargetType() );
|
||||
|
||||
for ( PropertyMapping propertyMapping : beanMapping.getPropertyMappings() ) {
|
||||
for ( PropertyMapping propertyMapping : beanMapping.getMappingMethod().getPropertyMappings() ) {
|
||||
addWithDependents( importedTypes, propertyMapping.getSourceType() );
|
||||
addWithDependents( importedTypes, propertyMapping.getTargetType() );
|
||||
}
|
||||
|
@ -18,25 +18,44 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
public class MappingMethod {
|
||||
import java.util.List;
|
||||
|
||||
public class MappingMethod extends AbstractModelElement {
|
||||
|
||||
private final Type declaringMapper;
|
||||
private final String name;
|
||||
private final String parameterName;
|
||||
private final Type sourceType;
|
||||
private final Type targetType;
|
||||
private final List<PropertyMapping> propertyMappings;
|
||||
private final MappingMethod elementMappingMethod;
|
||||
private final boolean isIterableMapping;
|
||||
private final String toConversion;
|
||||
|
||||
public MappingMethod(Type declaringMapper, String name, String parameterName) {
|
||||
public MappingMethod(Type declaringMapper, String name, String parameterName, Type sourceType, Type targetType) {
|
||||
this.declaringMapper = declaringMapper;
|
||||
this.name = name;
|
||||
this.parameterName = parameterName;
|
||||
this.sourceType = sourceType;
|
||||
this.targetType = targetType;
|
||||
this.propertyMappings = null;
|
||||
this.elementMappingMethod = null;
|
||||
this.isIterableMapping = false;
|
||||
this.toConversion = null;
|
||||
}
|
||||
|
||||
public MappingMethod(Type declaringMapper, String name, String parameterName, MappingMethod elementMappingMethod) {
|
||||
public MappingMethod(Type declaringMapper, String name, String parameterName, Type sourceType, Type targetType,
|
||||
List<PropertyMapping> propertyMappings, MappingMethod elementMappingMethod,
|
||||
String toConversion) {
|
||||
this.declaringMapper = declaringMapper;
|
||||
this.name = name;
|
||||
this.parameterName = parameterName;
|
||||
this.sourceType = sourceType;
|
||||
this.targetType = targetType;
|
||||
this.propertyMappings = propertyMappings;
|
||||
this.elementMappingMethod = elementMappingMethod;
|
||||
this.isIterableMapping = sourceType.isIterableType() && targetType.isIterableType();
|
||||
this.toConversion = toConversion;
|
||||
}
|
||||
|
||||
public Type getDeclaringMapper() {
|
||||
@ -51,10 +70,30 @@ public class MappingMethod {
|
||||
return parameterName;
|
||||
}
|
||||
|
||||
public Type getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
public Type getTargetType() {
|
||||
return targetType;
|
||||
}
|
||||
|
||||
public List<PropertyMapping> getPropertyMappings() {
|
||||
return propertyMappings;
|
||||
}
|
||||
|
||||
public MappingMethod getElementMappingMethod() {
|
||||
return elementMappingMethod;
|
||||
}
|
||||
|
||||
public boolean isIterableMapping() {
|
||||
return isIterableMapping;
|
||||
}
|
||||
|
||||
public String getToConversion() {
|
||||
return toConversion;
|
||||
}
|
||||
|
||||
public boolean isGenerationRequired() {
|
||||
return declaringMapper == null;
|
||||
}
|
||||
|
@ -29,49 +29,31 @@ package org.mapstruct.ap.model;
|
||||
public class PropertyMapping {
|
||||
|
||||
private final String sourceReadAccessorName;
|
||||
private final String sourceWriteAccessorName;
|
||||
private final Type sourceType;
|
||||
private final String targetReadAccessorName;
|
||||
private final String targetWriteAccessorName;
|
||||
private final Type targetType;
|
||||
|
||||
private final MappingMethod mappingMethod;
|
||||
private final MappingMethod reverseMappingMethod;
|
||||
private final String toConversion;
|
||||
private final String fromConversion;
|
||||
|
||||
public PropertyMapping(String sourceReadAccessorName, String sourceWriteAccessorName, Type sourceType,
|
||||
String targetReadAccessorName, String targetWriteAccessorName, Type targetType,
|
||||
MappingMethod mappingMethod, MappingMethod reverseMappingMethod, String toConversion,
|
||||
String fromConversion) {
|
||||
public PropertyMapping(String sourceReadAccessorName, Type sourceType, String targetWriteAccessorName,
|
||||
Type targetType, MappingMethod mappingMethod, String toConversion) {
|
||||
this.sourceReadAccessorName = sourceReadAccessorName;
|
||||
this.sourceWriteAccessorName = sourceWriteAccessorName;
|
||||
this.sourceType = sourceType;
|
||||
this.targetReadAccessorName = targetReadAccessorName;
|
||||
this.targetWriteAccessorName = targetWriteAccessorName;
|
||||
this.targetType = targetType;
|
||||
this.mappingMethod = mappingMethod;
|
||||
this.reverseMappingMethod = reverseMappingMethod;
|
||||
this.toConversion = toConversion;
|
||||
this.fromConversion = fromConversion;
|
||||
}
|
||||
|
||||
public String getSourceReadAccessorName() {
|
||||
return sourceReadAccessorName;
|
||||
}
|
||||
|
||||
public String getSourceWriteAccessorName() {
|
||||
return sourceWriteAccessorName;
|
||||
}
|
||||
|
||||
public Type getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
public String getTargetReadAccessorName() {
|
||||
return targetReadAccessorName;
|
||||
}
|
||||
|
||||
public String getTargetWriteAccessorName() {
|
||||
return targetWriteAccessorName;
|
||||
}
|
||||
@ -84,29 +66,19 @@ public class PropertyMapping {
|
||||
return mappingMethod;
|
||||
}
|
||||
|
||||
public MappingMethod getReverseMappingMethod() {
|
||||
return reverseMappingMethod;
|
||||
}
|
||||
|
||||
public String getToConversion() {
|
||||
return toConversion;
|
||||
}
|
||||
|
||||
public String getFromConversion() {
|
||||
return fromConversion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PropertyMapping {" +
|
||||
"\n sourceName='" + sourceReadAccessorName + "/" + sourceWriteAccessorName + "\'," +
|
||||
"\n sourceName='" + sourceReadAccessorName + "\'," +
|
||||
"\n sourceType=" + sourceType + "," +
|
||||
"\n targetName='" + targetReadAccessorName + "/" + targetWriteAccessorName + "\'," +
|
||||
"\n targetName='" + targetWriteAccessorName + "\'," +
|
||||
"\n targetType=" + targetType + "," +
|
||||
"\n mappingMethod=" + mappingMethod + "," +
|
||||
"\n reverseMappingMethod=" + reverseMappingMethod + "," +
|
||||
"\n toConversion='" + toConversion + "\'," +
|
||||
"\n fromConversion='" + fromConversion + "\'," +
|
||||
"\n}";
|
||||
}
|
||||
}
|
||||
|
@ -19,126 +19,9 @@
|
||||
|
||||
-->
|
||||
<#if mappingMethod.generationRequired == true>
|
||||
<#if iterableMapping == true>
|
||||
@Override
|
||||
public ${targetType.name}<${targetType.elementType.name}> ${mappingMethod.name}(${sourceType.name}<${sourceType.elementType.name}> ${mappingMethod.parameterName}) {
|
||||
if ( ${mappingMethod.parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
<#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side -->
|
||||
<#if targetType.name == "Iterable" && targetType.packageName == "java.lang">${targetType.iterableImplementationType.name}<#else>${targetType.name}</#if><${targetType.elementType.name}> ${targetType.name?uncap_first} = new <#if targetType.iterableImplementationType??>${targetType.iterableImplementationType.name}<#else>${targetType.name}</#if><${targetType.elementType.name}>();
|
||||
|
||||
for ( ${sourceType.elementType.name} ${sourceType.elementType.name?uncap_first} : ${mappingMethod.parameterName} ) {
|
||||
<#if toConversion??>
|
||||
${targetType.name?uncap_first}.add( ${toConversion} );
|
||||
<#else>
|
||||
${targetType.name?uncap_first}.add( ${mappingMethod.elementMappingMethod.name}( ${sourceType.elementType.name?uncap_first} ) );
|
||||
</#if>
|
||||
}
|
||||
|
||||
return ${targetType.name?uncap_first};
|
||||
}
|
||||
<#else>
|
||||
@Override
|
||||
public ${targetType.name} ${mappingMethod.name}(${sourceType.name} ${mappingMethod.parameterName}) {
|
||||
if ( ${mappingMethod.parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
${targetType.name} ${targetType.name?uncap_first} = new ${targetType.name}();
|
||||
|
||||
<#list propertyMappings as propertyMapping>
|
||||
<@simpleMap
|
||||
sourceBeanName=mappingMethod.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>
|
||||
|
||||
return ${targetType.name?uncap_first};
|
||||
}
|
||||
</#if>
|
||||
<@includeModel object=mappingMethod/>
|
||||
</#if>
|
||||
|
||||
<#if reverseMappingMethod??>
|
||||
<#if reverseMappingMethod.generationRequired == true>
|
||||
<#if iterableMapping == true>
|
||||
|
||||
@Override
|
||||
public ${sourceType.name}<${sourceType.elementType.name}> ${reverseMappingMethod.name}(${targetType.name}<${targetType.elementType.name}> ${reverseMappingMethod.parameterName}) {
|
||||
if ( ${reverseMappingMethod.parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
<#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side -->
|
||||
<#if sourceType.name == "Iterable" && sourceType.packageName == "java.lang">${sourceType.iterableImplementationType.name}<#else>${sourceType.name}</#if><${sourceType.elementType.name}> ${sourceType.name?uncap_first} = new <#if sourceType.iterableImplementationType??>${sourceType.iterableImplementationType.name}<#else>${sourceType.name}</#if><${sourceType.elementType.name}>();
|
||||
|
||||
for ( ${targetType.elementType.name} ${targetType.elementType.name?uncap_first} : ${reverseMappingMethod.parameterName} ) {
|
||||
<#if fromConversion??>
|
||||
${sourceType.name?uncap_first}.add( ${fromConversion} );
|
||||
<#else>
|
||||
${sourceType.name?uncap_first}.add( ${reverseMappingMethod.elementMappingMethod.name}( ${targetType.elementType.name?uncap_first} ) );
|
||||
</#if>
|
||||
}
|
||||
|
||||
return ${sourceType.name?uncap_first};
|
||||
}
|
||||
<#else>
|
||||
@Override
|
||||
public ${sourceType.name} ${reverseMappingMethod.name}(${targetType.name} ${reverseMappingMethod.parameterName}) {
|
||||
if ( ${reverseMappingMethod.parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
${sourceType.name} ${sourceType.name?uncap_first} = new ${sourceType.name}();
|
||||
|
||||
<#list propertyMappings as propertyMapping>
|
||||
<@simpleMap
|
||||
sourceBeanName=reverseMappingMethod.parameterName
|
||||
sourceType=propertyMapping.targetType
|
||||
sourceAccessorName=propertyMapping.targetReadAccessorName
|
||||
targetBeanName=sourceType.name?uncap_first
|
||||
targetType=propertyMapping.sourceType
|
||||
targetAccessorName=propertyMapping.sourceWriteAccessorName
|
||||
conversion=propertyMapping.fromConversion
|
||||
mappingMethod=propertyMapping.reverseMappingMethod
|
||||
/>
|
||||
</#list>
|
||||
|
||||
return ${sourceType.name?uncap_first};
|
||||
}
|
||||
</#if>
|
||||
</#if>
|
||||
<@includeModel object=reverseMappingMethod/>
|
||||
</#if>
|
||||
|
||||
<#-- 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>
|
||||
|
@ -0,0 +1,93 @@
|
||||
<#--
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
<#if generationRequired == true>
|
||||
<#if iterableMapping == true>
|
||||
@Override
|
||||
public ${targetType.name}<${targetType.elementType.name}> ${name}(${sourceType.name}<${sourceType.elementType.name}> ${parameterName}) {
|
||||
if ( ${parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
<#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side -->
|
||||
<#if targetType.name == "Iterable" && targetType.packageName == "java.lang">${targetType.iterableImplementationType.name}<#else>${targetType.name}</#if><${targetType.elementType.name}> ${targetType.name?uncap_first} = new <#if targetType.iterableImplementationType??>${targetType.iterableImplementationType.name}<#else>${targetType.name}</#if><${targetType.elementType.name}>();
|
||||
|
||||
for ( ${sourceType.elementType.name} ${sourceType.elementType.name?uncap_first} : ${parameterName} ) {
|
||||
<#if toConversion??>
|
||||
${targetType.name?uncap_first}.add( ${toConversion} );
|
||||
<#else>
|
||||
${targetType.name?uncap_first}.add( ${elementMappingMethod.name}( ${sourceType.elementType.name?uncap_first} ) );
|
||||
</#if>
|
||||
}
|
||||
|
||||
return ${targetType.name?uncap_first};
|
||||
}
|
||||
<#else>
|
||||
@Override
|
||||
public ${targetType.name} ${name}(${sourceType.name} ${parameterName}) {
|
||||
if ( ${parameterName} == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
${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>
|
||||
|
||||
return ${targetType.name?uncap_first};
|
||||
}
|
||||
</#if>
|
||||
</#if>
|
||||
|
||||
<#-- 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