mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#157 refactoring, adding ParameterAssignment Freemarker template and using it.
This commit is contained in:
parent
6f68512406
commit
fbd06315a0
@ -33,15 +33,13 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
*/
|
*/
|
||||||
public class IterableMappingMethod extends MappingMethod {
|
public class IterableMappingMethod extends MappingMethod {
|
||||||
|
|
||||||
private final MethodReference elementMappingMethod;
|
private final ParameterAssignment elementAssignment;
|
||||||
private final TypeConversion conversion;
|
|
||||||
private final MethodReference factoryMethod;
|
private final MethodReference factoryMethod;
|
||||||
|
|
||||||
public IterableMappingMethod(SourceMethod method, MethodReference elementMappingMethod,
|
public IterableMappingMethod(SourceMethod method, ParameterAssignment parameterAssignment,
|
||||||
TypeConversion conversion, MethodReference factoryMethod) {
|
MethodReference factoryMethod) {
|
||||||
super( method );
|
super( method );
|
||||||
this.elementMappingMethod = elementMappingMethod;
|
this.elementAssignment = parameterAssignment;
|
||||||
this.conversion = conversion;
|
|
||||||
this.factoryMethod = factoryMethod;
|
this.factoryMethod = factoryMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,20 +53,16 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodReference getElementMappingMethod() {
|
public ParameterAssignment getElementAssignment() {
|
||||||
return elementMappingMethod;
|
return elementAssignment;
|
||||||
}
|
|
||||||
|
|
||||||
public TypeConversion getConversion() {
|
|
||||||
return conversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> types = super.getImportTypes();
|
Set<Type> types = super.getImportTypes();
|
||||||
|
|
||||||
if ( conversion != null ) {
|
if ( elementAssignment != null ) {
|
||||||
types.addAll( conversion.getImportTypes() );
|
types.addAll( elementAssignment.getImportTypes() );
|
||||||
}
|
}
|
||||||
|
|
||||||
return types;
|
return types;
|
||||||
|
@ -33,21 +33,16 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
*/
|
*/
|
||||||
public class MapMappingMethod extends MappingMethod {
|
public class MapMappingMethod extends MappingMethod {
|
||||||
|
|
||||||
private final MethodReference keyMappingMethod;
|
private final ParameterAssignment keyAssignment;
|
||||||
private final MethodReference valueMappingMethod;
|
private final ParameterAssignment valueAssignment;
|
||||||
private final TypeConversion keyConversion;
|
|
||||||
private final TypeConversion valueConversion;
|
|
||||||
private final MethodReference factoryMethod;
|
private final MethodReference factoryMethod;
|
||||||
|
|
||||||
public MapMappingMethod(SourceMethod method, MethodReference keyMappingMethod, TypeConversion keyConversion,
|
public MapMappingMethod(SourceMethod method, ParameterAssignment keyAssignment, ParameterAssignment valueAssignment,
|
||||||
MethodReference valueMappingMethod, TypeConversion valueConversion,
|
|
||||||
MethodReference factoryMethod) {
|
MethodReference factoryMethod) {
|
||||||
super( method );
|
super( method );
|
||||||
|
|
||||||
this.keyMappingMethod = keyMappingMethod;
|
this.keyAssignment = keyAssignment;
|
||||||
this.keyConversion = keyConversion;
|
this.valueAssignment = valueAssignment;
|
||||||
this.valueMappingMethod = valueMappingMethod;
|
|
||||||
this.valueConversion = valueConversion;
|
|
||||||
this.factoryMethod = factoryMethod;
|
this.factoryMethod = factoryMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,31 +56,23 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodReference getKeyMappingMethod() {
|
public ParameterAssignment getKeyAssignment() {
|
||||||
return keyMappingMethod;
|
return keyAssignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypeConversion getKeyConversion() {
|
public ParameterAssignment getValueAssignment() {
|
||||||
return keyConversion;
|
return valueAssignment;
|
||||||
}
|
|
||||||
|
|
||||||
public MethodReference getValueMappingMethod() {
|
|
||||||
return valueMappingMethod;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeConversion getValueConversion() {
|
|
||||||
return valueConversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> types = super.getImportTypes();
|
Set<Type> types = super.getImportTypes();
|
||||||
|
|
||||||
if ( valueConversion != null ) {
|
if ( keyAssignment != null ) {
|
||||||
types.addAll( valueConversion.getImportTypes() );
|
types.addAll( keyAssignment.getImportTypes() );
|
||||||
}
|
}
|
||||||
if ( keyConversion != null ) {
|
if ( valueAssignment != null ) {
|
||||||
types.addAll( keyConversion.getImportTypes() );
|
types.addAll( valueAssignment.getImportTypes() );
|
||||||
}
|
}
|
||||||
|
|
||||||
return types;
|
return types;
|
||||||
|
@ -18,6 +18,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class carries the possible ways to do an assignment to a parameter on a mapping target
|
* This class carries the possible ways to do an assignment to a parameter on a mapping target
|
||||||
*
|
*
|
||||||
@ -30,20 +37,28 @@ package org.mapstruct.ap.model;
|
|||||||
*
|
*
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
public class ParameterAssignment {
|
public class ParameterAssignment extends ModelElement {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static enum AssignmentType { TYPE_CONVERSION, METHOD_REFERENCE, ASSIGNMENT };
|
||||||
|
|
||||||
private MethodReference methodReference;
|
private MethodReference methodReference;
|
||||||
private TypeConversion typeConversion;
|
private TypeConversion typeConversion;
|
||||||
|
private final AssignmentType assignmentType;
|
||||||
|
|
||||||
|
|
||||||
public ParameterAssignment() {
|
public ParameterAssignment() {
|
||||||
|
assignmentType = AssignmentType.ASSIGNMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterAssignment( MethodReference methodReference ) {
|
public ParameterAssignment( MethodReference methodReference ) {
|
||||||
|
assignmentType = AssignmentType.METHOD_REFERENCE;
|
||||||
this.methodReference = methodReference;
|
this.methodReference = methodReference;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParameterAssignment( TypeConversion typeConversion ) {
|
public ParameterAssignment( TypeConversion typeConversion ) {
|
||||||
|
assignmentType = AssignmentType.TYPE_CONVERSION;
|
||||||
this.typeConversion = typeConversion;
|
this.typeConversion = typeConversion;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,4 +70,51 @@ public class ParameterAssignment {
|
|||||||
return typeConversion;
|
return typeConversion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AssignmentType getAssignmentType() {
|
||||||
|
return assignmentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Type> getExceptionTypes() {
|
||||||
|
List<Type> exceptionTypes = new ArrayList<Type>();
|
||||||
|
switch ( assignmentType ) {
|
||||||
|
case METHOD_REFERENCE:
|
||||||
|
// exceptionTypes.addAll( methodReference.getExceptionTypes() );
|
||||||
|
break;
|
||||||
|
case TYPE_CONVERSION:
|
||||||
|
exceptionTypes.addAll( typeConversion.getExceptionTypes() );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return exceptionTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
Set<Type> importedTypes = new HashSet<Type>();
|
||||||
|
switch ( assignmentType ) {
|
||||||
|
case METHOD_REFERENCE:
|
||||||
|
importedTypes.addAll( methodReference.getImportTypes() );
|
||||||
|
break;
|
||||||
|
case TYPE_CONVERSION:
|
||||||
|
importedTypes.addAll( typeConversion.getImportTypes() );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return importedTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String result = "";
|
||||||
|
switch ( assignmentType ) {
|
||||||
|
case METHOD_REFERENCE:
|
||||||
|
result = methodReference.toString();
|
||||||
|
break;
|
||||||
|
case TYPE_CONVERSION:
|
||||||
|
result = typeConversion.toString();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ import java.util.Set;
|
|||||||
|
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
import org.mapstruct.ap.model.ParameterAssignment.AssignmentType;
|
||||||
/**
|
/**
|
||||||
* Represents the mapping between a source and target property, e.g. from
|
* Represents the mapping between a source and target property, e.g. from
|
||||||
* {@code String Source#foo} to {@code int Target#bar}. Name and type of source
|
* {@code String Source#foo} to {@code int Target#bar}. Name and type of source
|
||||||
@ -45,12 +45,12 @@ public class PropertyMapping extends ModelElement {
|
|||||||
private final boolean isTargetAccessorSetter;
|
private final boolean isTargetAccessorSetter;
|
||||||
private final String targetReadAccessorName;
|
private final String targetReadAccessorName;
|
||||||
|
|
||||||
private final MethodReference mappingMethod;
|
private final ParameterAssignment parameterAssignment;
|
||||||
private final TypeConversion conversion;
|
|
||||||
|
|
||||||
public PropertyMapping(String sourceBeanName, String sourceName, String sourceAccessorName, Type sourceType,
|
public PropertyMapping(String sourceBeanName, String sourceName, String sourceAccessorName, Type sourceType,
|
||||||
String targetName, String targetAccessorName, Type targetType,
|
String targetName, String targetAccessorName, Type targetType,
|
||||||
MethodReference mappingMethod, TypeConversion conversion) {
|
ParameterAssignment parameterAssignment ) {
|
||||||
|
|
||||||
this.sourceBeanName = sourceBeanName;
|
this.sourceBeanName = sourceBeanName;
|
||||||
this.sourceName = sourceName;
|
this.sourceName = sourceName;
|
||||||
@ -64,8 +64,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
this.targetReadAccessorName =
|
this.targetReadAccessorName =
|
||||||
this.isTargetAccessorSetter ? "get" + targetAccessorName.substring( 3 ) : targetAccessorName;
|
this.isTargetAccessorSetter ? "get" + targetAccessorName.substring( 3 ) : targetAccessorName;
|
||||||
|
|
||||||
this.mappingMethod = mappingMethod;
|
this.parameterAssignment = parameterAssignment;
|
||||||
this.conversion = conversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSourceBeanName() {
|
public String getSourceBeanName() {
|
||||||
@ -96,12 +95,8 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return targetType;
|
return targetType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodReference getMappingMethod() {
|
public ParameterAssignment getParameterAssignment() {
|
||||||
return mappingMethod;
|
return parameterAssignment;
|
||||||
}
|
|
||||||
|
|
||||||
public TypeConversion getConversion() {
|
|
||||||
return conversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,16 +120,17 @@ public class PropertyMapping extends ModelElement {
|
|||||||
@Override
|
@Override
|
||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> importTypes = new HashSet<Type>();
|
Set<Type> importTypes = new HashSet<Type>();
|
||||||
|
if ( parameterAssignment != null ) {
|
||||||
if ( isTargetAccessorSetter() && getMappingMethod() == null
|
if ( isTargetAccessorSetter()
|
||||||
|
&& parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT )
|
||||||
&& ( targetType.isCollectionType() || targetType.isMapType() ) ) {
|
&& ( targetType.isCollectionType() || targetType.isMapType() ) ) {
|
||||||
importTypes.addAll( targetType.getImportTypes() );
|
importTypes.addAll( targetType.getImportTypes() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( conversion != null && mappingMethod == null ) {
|
if ( !parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) ) {
|
||||||
importTypes.addAll( conversion.getImportTypes() );
|
importTypes.addAll( parameterAssignment.getImportTypes() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return importTypes;
|
return importTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,8 +141,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
"\n sourceType=" + sourceType + "," +
|
"\n sourceType=" + sourceType + "," +
|
||||||
"\n targetName='" + targetAccessorName + "\'," +
|
"\n targetName='" + targetAccessorName + "\'," +
|
||||||
"\n targetType=" + targetType + "," +
|
"\n targetType=" + targetType + "," +
|
||||||
"\n mappingMethod=" + mappingMethod + "," +
|
"\n parameterAssignment=" + parameterAssignment +
|
||||||
"\n Conversion='" + conversion + "\'," +
|
|
||||||
"\n}";
|
"\n}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -662,8 +662,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
Executables.getPropertyName( targetAcessor ),
|
Executables.getPropertyName( targetAcessor ),
|
||||||
targetAcessor.getSimpleName().toString(),
|
targetAcessor.getSimpleName().toString(),
|
||||||
targetType,
|
targetType,
|
||||||
parameterAssignment != null ? parameterAssignment.getMethodReference() : null,
|
parameterAssignment
|
||||||
parameterAssignment != null ? parameterAssignment.getTypeConversion() : null
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( !isPropertyMappable( property ) ) {
|
if ( !isPropertyMappable( property ) ) {
|
||||||
@ -715,12 +714,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
}
|
}
|
||||||
|
|
||||||
MethodReference factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
|
MethodReference factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
|
||||||
return new IterableMappingMethod(
|
return new IterableMappingMethod( method, parameterAssignment, factoryMethod );
|
||||||
method,
|
|
||||||
parameterAssignment != null ? parameterAssignment.getMethodReference() : null,
|
|
||||||
parameterAssignment != null ? parameterAssignment.getTypeConversion() : null,
|
|
||||||
factoryMethod
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapMappingMethod getMapMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
|
private MapMappingMethod getMapMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
|
||||||
@ -787,15 +781,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
}
|
}
|
||||||
|
|
||||||
MethodReference factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
|
MethodReference factoryMethod = getFactoryMethod( mapperReferences, methods, method.getReturnType() );
|
||||||
|
return new MapMappingMethod( method, parameterAssignmentKey, parameterAssignmentValue, factoryMethod );
|
||||||
return new MapMappingMethod(
|
|
||||||
method,
|
|
||||||
parameterAssignmentKey != null ? parameterAssignmentKey.getMethodReference() : null,
|
|
||||||
parameterAssignmentKey != null ? parameterAssignmentKey.getTypeConversion() : null,
|
|
||||||
parameterAssignmentValue != null ? parameterAssignmentValue.getMethodReference() : null,
|
|
||||||
parameterAssignmentValue != null ? parameterAssignmentValue.getTypeConversion() : null,
|
|
||||||
factoryMethod
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private EnumMappingMethod getEnumMappingMethod(SourceMethod method) {
|
private EnumMappingMethod getEnumMappingMethod(SourceMethod method) {
|
||||||
@ -977,9 +963,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( property.getSourceType().isAssignableTo( property.getTargetType() ) ||
|
if ( property.getParameterAssignment() != null ||
|
||||||
property.getMappingMethod() != null ||
|
|
||||||
property.getConversion() != null ||
|
|
||||||
( ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) &&
|
( ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) &&
|
||||||
collectionOrMapTargetTypeHasCompatibleConstructor ) ) {
|
collectionOrMapTargetTypeHasCompatibleConstructor ) ) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,24 +32,7 @@
|
|||||||
</#if>
|
</#if>
|
||||||
|
|
||||||
for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) {
|
for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) {
|
||||||
<#if elementMappingMethod??>
|
<@includeModel object=elementAssignment target="${resultName}.add" source="${loopVariableName}" targetType="${resultType.typeParameters[0].name}"/>
|
||||||
${resultName}.add( <@includeModel object=elementMappingMethod input="${loopVariableName}" targetType="${resultType.typeParameters[0].name}"/> );
|
|
||||||
<#elseif conversion??>
|
|
||||||
<#if (conversion.exceptionTypes?size == 0) >
|
|
||||||
${resultName}.add( <@includeModel object=conversion/> );
|
|
||||||
<#else>
|
|
||||||
try {
|
|
||||||
${resultName}.add( <@includeModel object=conversion/> );
|
|
||||||
}
|
|
||||||
<#list conversion.exceptionTypes as exceptionType>
|
|
||||||
catch( <@includeModel object=exceptionType/> e ) {
|
|
||||||
throw new RuntimeException( e );
|
|
||||||
}
|
|
||||||
</#list>
|
|
||||||
</#if>
|
|
||||||
<#else>
|
|
||||||
${resultName}.add( ${loopVariableName} );
|
|
||||||
</#if>
|
|
||||||
}
|
}
|
||||||
<#if returnType.name != "void">
|
<#if returnType.name != "void">
|
||||||
|
|
||||||
|
@ -32,48 +32,18 @@
|
|||||||
|
|
||||||
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
||||||
for ( java.util.Map.Entry<<#list sourceParameter.type.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> ${entryVariableName} : ${sourceParameter.name}.entrySet() ) {
|
for ( java.util.Map.Entry<<#list sourceParameter.type.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> ${entryVariableName} : ${sourceParameter.name}.entrySet() ) {
|
||||||
|
|
||||||
<#-- key -->
|
<#-- key -->
|
||||||
<#if keyMappingMethod??>
|
<@includeModel object=keyAssignment
|
||||||
<@includeModel object=resultType.typeParameters[0]/> ${keyVariableName} = <@includeModel object=keyMappingMethod input="entry.getKey()" targetType="${resultType.typeParameters[0].name}"/>;
|
target=keyVariableName
|
||||||
<#elseif keyConversion??>
|
targetType=typeName(resultType.typeParameters[0])
|
||||||
<#if (keyConversion.exceptionTypes?size == 0) >
|
source="entry.getKey()"
|
||||||
<@includeModel object=resultType.typeParameters[0]/> ${keyVariableName} = <@includeModel object=keyConversion/>;
|
isLocalVar=true/>
|
||||||
<#else>
|
|
||||||
<@includeModel object=resultType.typeParameters[0]/> ${keyVariableName};
|
|
||||||
try {
|
|
||||||
${keyVariableName} = <@includeModel object=keyConversion/>;
|
|
||||||
}
|
|
||||||
<#list keyConversion.exceptionTypes as exceptionType>
|
|
||||||
catch( <@includeModel object=exceptionType/> e ) {
|
|
||||||
throw new RuntimeException( e );
|
|
||||||
}
|
|
||||||
</#list>
|
|
||||||
</#if>
|
|
||||||
<#else>
|
|
||||||
<@includeModel object=resultType.typeParameters[0]/> ${keyVariableName} = entry.getKey();
|
|
||||||
</#if>
|
|
||||||
<#-- value -->
|
<#-- value -->
|
||||||
<#if valueMappingMethod??>
|
<@includeModel object=valueAssignment
|
||||||
<@includeModel object=resultType.typeParameters[1]/> ${valueVariableName} = <@includeModel object=valueMappingMethod input="entry.getValue()" targetType="${resultType.typeParameters[1].name}"/>;
|
target=valueVariableName
|
||||||
<#elseif valueConversion??>
|
targetType=typeName(resultType.typeParameters[1])
|
||||||
<#if (valueConversion.exceptionTypes?size == 0) >
|
source="entry.getValue()"
|
||||||
<@includeModel object=resultType.typeParameters[1]/> ${valueVariableName} = <@includeModel object=valueConversion/>;
|
isLocalVar=true/>
|
||||||
<#else>
|
|
||||||
<@includeModel object=resultType.typeParameters[1]/> ${valueVariableName};
|
|
||||||
try {
|
|
||||||
${valueVariableName} = <@includeModel object=valueConversion/>;
|
|
||||||
}
|
|
||||||
<#list valueConversion.exceptionTypes as exceptionType>
|
|
||||||
catch( <@includeModel object=exceptionType/> e ) {
|
|
||||||
throw new RuntimeException( e );
|
|
||||||
}
|
|
||||||
</#list>
|
|
||||||
</#if>
|
|
||||||
<#else>
|
|
||||||
<@includeModel object=resultType.typeParameters[1]/> ${valueVariableName} = entry.getValue();
|
|
||||||
</#if>
|
|
||||||
|
|
||||||
${resultName}.put( ${keyVariableName}, ${valueVariableName} );
|
${resultName}.put( ${keyVariableName}, ${valueVariableName} );
|
||||||
}
|
}
|
||||||
<#if returnType.name != "void">
|
<#if returnType.name != "void">
|
||||||
@ -81,3 +51,7 @@
|
|||||||
return ${resultName};
|
return ${resultName};
|
||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
|
<#function typeName type>
|
||||||
|
<#local result><@includeModel object=type/></#local>
|
||||||
|
<#return result>
|
||||||
|
</#function>
|
||||||
|
@ -18,8 +18,26 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<#if methodRefChild??>
|
<@compress single_line=true>
|
||||||
<#if declaringMapper??>${mapperVariableName}.</#if>${name}(<#list parameters as param> <#if param.targetType>${ext.targetType}.class<#else><@includeModel object=methodRefChild input=ext.input targetType=singleSourceParameterType.name/></#if><#if param_has_next>,<#else> </#if></#list><#if contextParam??>, ${contextParam}</#if>)<#t>
|
<#-- method is either internal to the mapper class, or external (via uses) declaringMapper!=null -->
|
||||||
<#else>
|
<#if declaringMapper??>${mapperVariableName}.</#if>${name}( <@arguments/> )
|
||||||
<#if declaringMapper??>${mapperVariableName}.</#if>${name}(<#list parameters as param> <#if param.targetType>${ext.targetType}.class<#else>${ext.input}</#if><#if param_has_next>,<#else> </#if></#list><#if contextParam??>, ${contextParam}</#if>)<#t>
|
<#macro arguments>
|
||||||
</#if>
|
<#list parameters as param>
|
||||||
|
<#if param.targetType>
|
||||||
|
<#-- a class is passed on for casting, see @TargetType -->
|
||||||
|
${ext.targetType}.class
|
||||||
|
<#else>
|
||||||
|
<#if methodRefChild??>
|
||||||
|
<#-- the nested case -->
|
||||||
|
<@includeModel object=methodRefChild source=ext.source targetType=singleSourceParameterType.name/>
|
||||||
|
<#else>
|
||||||
|
<#-- the non nested case -->
|
||||||
|
${ext.source}
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
<#if param_has_next>, </#if>
|
||||||
|
</#list>
|
||||||
|
<#-- context parameter, e.g. for buildin methods concerning date conversion -->
|
||||||
|
<#if contextParam??>, ${contextParam}</#if>
|
||||||
|
</#macro>
|
||||||
|
</@compress>
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
<#--
|
||||||
|
|
||||||
|
Copyright 2012-2014 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 ( ext.isLocalVar?? && ext.isLocalVar==true )>
|
||||||
|
<#-- assignment is a local variable assignment -->
|
||||||
|
<#if (exceptionTypes?size == 0) >
|
||||||
|
${ext.targetType} ${ext.target} = <@assignment/>;
|
||||||
|
<#else>
|
||||||
|
${ext.targetType} ${ext.target};
|
||||||
|
try {
|
||||||
|
${ext.target} = <@assignment/>;
|
||||||
|
}
|
||||||
|
<#list exceptionTypes as exceptionType>
|
||||||
|
catch ( <@includeModel object=exceptionType/> e ) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
|
</#list>
|
||||||
|
</#if>
|
||||||
|
<#else>
|
||||||
|
<#-- assignment is a method call -->
|
||||||
|
<#if (exceptionTypes?size == 0) >
|
||||||
|
${ext.target}( <@assignment/> );
|
||||||
|
<#else>
|
||||||
|
try {
|
||||||
|
${ext.target}( <@assignment/> );
|
||||||
|
}
|
||||||
|
<#list exceptionTypes as exceptionType>
|
||||||
|
catch ( <@includeModel object=exceptionType/> e ) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
|
</#list>
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
<#macro assignment>
|
||||||
|
<#compress>
|
||||||
|
<#switch assignmentType>
|
||||||
|
<#case "TYPE_CONVERSION">
|
||||||
|
<@includeModel object=typeConversion/>
|
||||||
|
<#break>
|
||||||
|
<#case "METHOD_REFERENCE">
|
||||||
|
<@includeModel object=methodReference source="${ext.source}" targetType=ext.targetType raw=ext.raw/>
|
||||||
|
<#break>
|
||||||
|
<#case "ASSIGNMENT">
|
||||||
|
${ext.source}
|
||||||
|
<#break>
|
||||||
|
</#switch>
|
||||||
|
</#compress>
|
||||||
|
</#macro>
|
@ -18,87 +18,55 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<#-- a) invoke mapping method -->
|
<#if !( targetType.collectionType || targetType.mapType ) >
|
||||||
<#if mappingMethod??>
|
<#-- non collections or maps -->
|
||||||
<@assignResult
|
<#if ( !sourceType.primitive && parameterAssignment.assignmentType!="ASSIGNMENT" ) >
|
||||||
existingInstanceMapping=ext.existingInstanceMapping
|
|
||||||
targetAccessorSetter=targetAccessorSetter
|
|
||||||
targetType=targetType
|
|
||||||
targetBeanName=ext.targetBeanName
|
|
||||||
targetReadAccessorName=targetReadAccessorName
|
|
||||||
targetAccessorName=targetAccessorName
|
|
||||||
sourceBeanName=sourceBeanName
|
|
||||||
sourceAccessorName=sourceAccessorName><#compress>
|
|
||||||
<@includeModel object=mappingMethod input="${sourceBeanName}.${sourceAccessorName}()" targetType=targetType raw=true/>
|
|
||||||
</#compress></@assignResult>
|
|
||||||
<#-- b) simple conversion -->
|
|
||||||
<#elseif conversion??>
|
|
||||||
<#if sourceType.primitive == false>
|
|
||||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||||
<@applyConversion targetBeanName=ext.targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
<@assignmentLine/>
|
||||||
}
|
}
|
||||||
<#else>
|
<#else>
|
||||||
<@applyConversion targetBeanName=ext.targetBeanName targetAccessorName=targetAccessorName conversion=conversion/>
|
<@assignmentLine/>
|
||||||
</#if>
|
</#if>
|
||||||
<#-- c) simply set -->
|
|
||||||
<#else>
|
<#else>
|
||||||
<@assignResult
|
<#-- collections or maps -->
|
||||||
existingInstanceMapping=ext.existingInstanceMapping
|
<#if ( ext.existingInstanceMapping || !targetAccessorSetter ) >
|
||||||
targetAccessorSetter=targetAccessorSetter
|
if ( ${ext.targetBeanName}.${targetReadAccessorName}() != null ) {
|
||||||
targetType=targetType
|
<#if ext.existingInstanceMapping>
|
||||||
targetBeanName=ext.targetBeanName
|
${ext.targetBeanName}.${targetReadAccessorName}().clear();
|
||||||
targetReadAccessorName=targetReadAccessorName
|
|
||||||
targetAccessorName=targetAccessorName
|
|
||||||
sourceBeanName=sourceBeanName
|
|
||||||
sourceAccessorName=sourceAccessorName
|
|
||||||
; use_plain><#compress>
|
|
||||||
<#if use_plain>
|
|
||||||
${sourceBeanName}.${sourceAccessorName}()
|
|
||||||
<#else>
|
|
||||||
new <#if targetType.implementationType??><@includeModel object=targetType.implementationType/><#else><@includeModel object=targetType/></#if>( ${sourceBeanName}.${sourceAccessorName}() )
|
|
||||||
</#if>
|
|
||||||
</#compress></@assignResult>
|
|
||||||
</#if>
|
|
||||||
<#macro assignResult existingInstanceMapping targetAccessorSetter targetType targetBeanName targetReadAccessorName targetAccessorName sourceBeanName sourceAccessorName>
|
|
||||||
<#if ( existingInstanceMapping || !targetAccessorSetter ) && ( targetType.collectionType || targetType.mapType ) >
|
|
||||||
if ( ${targetBeanName}.${targetReadAccessorName}() != null ) {
|
|
||||||
<#if existingInstanceMapping>
|
|
||||||
${targetBeanName}.${targetReadAccessorName}().clear();
|
|
||||||
</#if><#t>
|
</#if><#t>
|
||||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||||
<#if targetType.collectionType>
|
<#if targetType.collectionType>
|
||||||
${targetBeanName}.${targetReadAccessorName}().addAll( <#nested true> );
|
<@collectionOrMapAssignmentLine target="${ext.targetBeanName}.${targetReadAccessorName}().addAll"/>
|
||||||
<#else>
|
<#else>
|
||||||
${targetBeanName}.${targetReadAccessorName}().putAll( <#nested true> );
|
<@collectionOrMapAssignmentLine target="${ext.targetBeanName}.${targetReadAccessorName}().putAll"/>
|
||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<#if targetAccessorSetter>
|
<#if targetAccessorSetter>
|
||||||
else if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
else if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||||
${targetBeanName}.${targetAccessorName}( <#nested false> );
|
<@collectionOrMapAssignmentLine/>
|
||||||
}
|
}
|
||||||
</#if>
|
</#if>
|
||||||
<#elseif targetAccessorSetter>
|
<#elseif targetAccessorSetter>
|
||||||
<#if targetType.collectionType || targetType.mapType>
|
|
||||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||||
${targetBeanName}.${targetAccessorName}( <#nested false> );
|
<@collectionOrMapAssignmentLine/>
|
||||||
}
|
}
|
||||||
|
</#if>
|
||||||
|
</#if>
|
||||||
|
<#macro collectionOrMapAssignmentLine
|
||||||
|
target="${ext.targetBeanName}.${targetAccessorName}"
|
||||||
|
source="${sourceBeanName}.${sourceAccessorName}">
|
||||||
|
<#compress>
|
||||||
|
<#if parameterAssignment?? && parameterAssignment.assignmentType!="ASSIGNMENT">
|
||||||
|
<@assignmentLine target source/>
|
||||||
<#else>
|
<#else>
|
||||||
${targetBeanName}.${targetAccessorName}( <#nested true> );
|
${target}( new <#if targetType.implementationType??><@includeModel object=targetType.implementationType/><#else><@includeModel object=targetType/></#if>( ${source}() ) );
|
||||||
</#if>
|
|
||||||
</#if>
|
</#if>
|
||||||
|
</#compress>
|
||||||
|
|
||||||
</#macro>
|
</#macro>
|
||||||
<#macro applyConversion targetBeanName targetAccessorName conversion>
|
<#macro assignmentLine
|
||||||
<#if (conversion.exceptionTypes?size == 0) >
|
target="${ext.targetBeanName}.${targetAccessorName}"
|
||||||
${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> );
|
source="${sourceBeanName}.${sourceAccessorName}">
|
||||||
<#else>
|
<@includeModel object=parameterAssignment target=target source="${source}()" targetType=targetType raw=true/>
|
||||||
try {
|
|
||||||
${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> );
|
|
||||||
}
|
|
||||||
<#list conversion.exceptionTypes as exceptionType>
|
|
||||||
catch( <@includeModel object=exceptionType/> e ) {
|
|
||||||
throw new RuntimeException( e );
|
|
||||||
}
|
|
||||||
</#list>
|
|
||||||
</#if>
|
|
||||||
</#macro>
|
</#macro>
|
Loading…
x
Reference in New Issue
Block a user