#157 renaming ParameterAssignment in the more suitable TargetAssignment, creating package creation in processor for creation helpers, refactor MappingResolver

This commit is contained in:
sjaakd 2014-03-22 13:26:29 +01:00
parent fbd06315a0
commit 9c17909fd6
9 changed files with 103 additions and 73 deletions

View File

@ -33,10 +33,10 @@ import org.mapstruct.ap.util.Strings;
*/ */
public class IterableMappingMethod extends MappingMethod { public class IterableMappingMethod extends MappingMethod {
private final ParameterAssignment elementAssignment; private final TargetAssignment elementAssignment;
private final MethodReference factoryMethod; private final MethodReference factoryMethod;
public IterableMappingMethod(SourceMethod method, ParameterAssignment parameterAssignment, public IterableMappingMethod(SourceMethod method, TargetAssignment parameterAssignment,
MethodReference factoryMethod) { MethodReference factoryMethod) {
super( method ); super( method );
this.elementAssignment = parameterAssignment; this.elementAssignment = parameterAssignment;
@ -53,7 +53,7 @@ public class IterableMappingMethod extends MappingMethod {
throw new IllegalStateException( "Method " + this + " has no source parameter." ); throw new IllegalStateException( "Method " + this + " has no source parameter." );
} }
public ParameterAssignment getElementAssignment() { public TargetAssignment getElementAssignment() {
return elementAssignment; return elementAssignment;
} }

View File

@ -33,11 +33,11 @@ import org.mapstruct.ap.util.Strings;
*/ */
public class MapMappingMethod extends MappingMethod { public class MapMappingMethod extends MappingMethod {
private final ParameterAssignment keyAssignment; private final TargetAssignment keyAssignment;
private final ParameterAssignment valueAssignment; private final TargetAssignment valueAssignment;
private final MethodReference factoryMethod; private final MethodReference factoryMethod;
public MapMappingMethod(SourceMethod method, ParameterAssignment keyAssignment, ParameterAssignment valueAssignment, public MapMappingMethod(SourceMethod method, TargetAssignment keyAssignment, TargetAssignment valueAssignment,
MethodReference factoryMethod) { MethodReference factoryMethod) {
super( method ); super( method );
@ -56,11 +56,11 @@ public class MapMappingMethod extends MappingMethod {
throw new IllegalStateException( "Method " + this + " has no source parameter." ); throw new IllegalStateException( "Method " + this + " has no source parameter." );
} }
public ParameterAssignment getKeyAssignment() { public TargetAssignment getKeyAssignment() {
return keyAssignment; return keyAssignment;
} }
public ParameterAssignment getValueAssignment() { public TargetAssignment getValueAssignment() {
return valueAssignment; return valueAssignment;
} }

View File

@ -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; import org.mapstruct.ap.model.TargetAssignment.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 ParameterAssignment parameterAssignment; private final TargetAssignment propertyAssignment;
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,
ParameterAssignment parameterAssignment ) { TargetAssignment propertyAssignment ) {
this.sourceBeanName = sourceBeanName; this.sourceBeanName = sourceBeanName;
this.sourceName = sourceName; this.sourceName = sourceName;
@ -64,7 +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.parameterAssignment = parameterAssignment; this.propertyAssignment = propertyAssignment;
} }
public String getSourceBeanName() { public String getSourceBeanName() {
@ -95,8 +95,8 @@ public class PropertyMapping extends ModelElement {
return targetType; return targetType;
} }
public ParameterAssignment getParameterAssignment() { public TargetAssignment getPropertyAssignment() {
return parameterAssignment; return propertyAssignment;
} }
/** /**
@ -120,15 +120,15 @@ 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 ( propertyAssignment != null ) {
if ( isTargetAccessorSetter() if ( isTargetAccessorSetter()
&& parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) && propertyAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT )
&& ( targetType.isCollectionType() || targetType.isMapType() ) ) { && ( targetType.isCollectionType() || targetType.isMapType() ) ) {
importTypes.addAll( targetType.getImportTypes() ); importTypes.addAll( targetType.getImportTypes() );
} }
if ( !parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) ) { if ( !propertyAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) ) {
importTypes.addAll( parameterAssignment.getImportTypes() ); importTypes.addAll( propertyAssignment.getImportTypes() );
} }
} }
return importTypes; return importTypes;
@ -141,7 +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 parameterAssignment=" + parameterAssignment + "\n propertyAssignment=" + propertyAssignment +
"\n}"; "\n}";
} }
} }

View File

@ -32,12 +32,12 @@ import org.mapstruct.ap.model.common.Type;
* <ol> * <ol>
* <li>MethodReference</li> * <li>MethodReference</li>
* <li>TypeConversion</li> * <li>TypeConversion</li>
* <li>Simple Assignment (empty ParameterAssignment)</li> * <li>Simple Assignment (empty TargetAssignment)</li>
* </ol> * </ol>
* *
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class ParameterAssignment extends ModelElement { public class TargetAssignment extends ModelElement {
@ -48,16 +48,16 @@ public class ParameterAssignment extends ModelElement {
private final AssignmentType assignmentType; private final AssignmentType assignmentType;
public ParameterAssignment() { public TargetAssignment() {
assignmentType = AssignmentType.ASSIGNMENT; assignmentType = AssignmentType.ASSIGNMENT;
} }
public ParameterAssignment( MethodReference methodReference ) { public TargetAssignment( MethodReference methodReference ) {
assignmentType = AssignmentType.METHOD_REFERENCE; assignmentType = AssignmentType.METHOD_REFERENCE;
this.methodReference = methodReference; this.methodReference = methodReference;
} }
public ParameterAssignment( TypeConversion typeConversion ) { public TargetAssignment( TypeConversion typeConversion ) {
assignmentType = AssignmentType.TYPE_CONVERSION; assignmentType = AssignmentType.TYPE_CONVERSION;
this.typeConversion = typeConversion; this.typeConversion = typeConversion;
} }

View File

@ -18,7 +18,8 @@
*/ */
package org.mapstruct.ap.processor; package org.mapstruct.ap.processor;
import org.mapstruct.ap.model.ParameterAssignment; import org.mapstruct.ap.processor.creation.MappingResolver;
import org.mapstruct.ap.model.TargetAssignment;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -642,7 +643,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'"; String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'";
ParameterAssignment parameterAssignment = mappingResolver.getParameterAssignment( TargetAssignment parameterAssignment = mappingResolver.getTargetAssignment(
method, method,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
@ -689,7 +690,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
String dateFormat = method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null; String dateFormat = method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null;
String conversionStr = Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() ); String conversionStr = Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() );
ParameterAssignment parameterAssignment = mappingResolver.getParameterAssignment( TargetAssignment parameterAssignment = mappingResolver.getTargetAssignment(
method, method,
"collection element", "collection element",
mapperReferences, mapperReferences,
@ -727,7 +728,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
Type keyTargetType = resultTypeParams.get( 0 ); Type keyTargetType = resultTypeParams.get( 0 );
String keyDateFormat = method.getMapMapping() != null ? method.getMapMapping().getKeyFormat() : null; String keyDateFormat = method.getMapMapping() != null ? method.getMapMapping().getKeyFormat() : null;
ParameterAssignment parameterAssignmentKey = mappingResolver.getParameterAssignment( TargetAssignment parameterAssignmentKey = mappingResolver.getTargetAssignment(
method, method,
"map key", "map key",
mapperReferences, mapperReferences,
@ -756,7 +757,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
Type valueTargetType = resultTypeParams.get( 1 ); Type valueTargetType = resultTypeParams.get( 1 );
String valueDateFormat = method.getMapMapping() != null ? method.getMapMapping().getValueFormat() : null; String valueDateFormat = method.getMapMapping() != null ? method.getMapMapping().getValueFormat() : null;
ParameterAssignment parameterAssignmentValue = mappingResolver.getParameterAssignment( TargetAssignment parameterAssignmentValue = mappingResolver.getTargetAssignment(
method, method,
"map value", "map value",
mapperReferences, mapperReferences,
@ -963,7 +964,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
); );
} }
if ( property.getParameterAssignment() != null || if ( property.getPropertyAssignment() != null ||
( ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) && ( ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) &&
collectionOrMapTargetTypeHasCompatibleConstructor ) ) { collectionOrMapTargetTypeHasCompatibleConstructor ) ) {
return true; return true;

View File

@ -16,9 +16,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.mapstruct.ap.processor; package org.mapstruct.ap.processor.creation;
import org.mapstruct.ap.model.ParameterAssignment; import org.mapstruct.ap.model.TargetAssignment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -107,11 +107,11 @@ public class MappingResolver {
* <ol> * <ol>
* <li>MethodReference</li> * <li>MethodReference</li>
* <li>TypeConversion</li> * <li>TypeConversion</li>
* <li>Simple Assignment (empty ParameterAssignment)</li> * <li>Simple Assignment (empty TargetAssignment)</li>
* <li>null, no assignment found</li> * <li>null, no assignment found</li>
* </ol> * </ol>
*/ */
public ParameterAssignment getParameterAssignment( SourceMethod mappingMethod, public TargetAssignment getTargetAssignment( SourceMethod mappingMethod,
String mappedElement, String mappedElement,
List<MapperReference> mapperReferences, List<MapperReference> mapperReferences,
List<SourceMethod> methods, List<SourceMethod> methods,
@ -121,7 +121,7 @@ public class MappingResolver {
String dateFormat, String dateFormat,
String sourceReference ) { String sourceReference ) {
MethodReference mappingMethodReference = getMappingMethodReferenceBasedOnMethod( MethodReference mappingMethodReference = resolveViaMethod(
mappingMethod, mappingMethod,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
@ -132,21 +132,21 @@ public class MappingResolver {
dateFormat dateFormat
); );
ParameterAssignment parameterAssignment = null; TargetAssignment assignment = null;
if (mappingMethodReference != null ) { if (mappingMethodReference != null ) {
parameterAssignment = new ParameterAssignment(mappingMethodReference ); assignment = new TargetAssignment(mappingMethodReference );
} }
else if (sourceType.isAssignableTo( targetType ) ) { else if (sourceType.isAssignableTo( targetType ) ) {
parameterAssignment = new ParameterAssignment(); assignment = new TargetAssignment();
} }
else { else {
TypeConversion conversion = getConversion( sourceType, targetType, dateFormat, sourceReference ); TypeConversion conversion = resolveViaConversion( sourceType, targetType, dateFormat, sourceReference );
if ( conversion != null ) { if ( conversion != null ) {
parameterAssignment = new ParameterAssignment(conversion ); assignment = new TargetAssignment(conversion );
} }
else { else {
mappingMethodReference = getMappingMethodReferenceBasedOnParameter( mappingMethodReference = resolveViaMethodAndMethod(
mappingMethod, mappingMethod,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
@ -157,15 +157,18 @@ public class MappingResolver {
dateFormat dateFormat
); );
if ( mappingMethodReference != null ) { if ( mappingMethodReference != null ) {
parameterAssignment = new ParameterAssignment( mappingMethodReference ); assignment = new TargetAssignment( mappingMethodReference );
} }
} }
} }
return parameterAssignment; return assignment;
} }
private TypeConversion getConversion(Type sourceType, Type targetType, String dateFormat, String sourceReference) { private TypeConversion resolveViaConversion( Type sourceType,
Type targetType,
String dateFormat,
String sourceReference ) {
ConversionProvider conversionProvider = conversions.getConversion( sourceType, targetType ); ConversionProvider conversionProvider = conversions.getConversion( sourceType, targetType );
if ( conversionProvider == null ) { if ( conversionProvider == null ) {
@ -192,7 +195,7 @@ public class MappingResolver {
* *
* @return a method reference. * @return a method reference.
*/ */
private MethodReference getMappingMethodReferenceBasedOnMethod(SourceMethod mappingMethod, private MethodReference resolveViaMethod( SourceMethod mappingMethod,
String mappedElement, String mappedElement,
List<MapperReference> mapperReferences, List<MapperReference> mapperReferences,
List<SourceMethod> methods, List<SourceMethod> methods,
@ -200,6 +203,7 @@ public class MappingResolver {
Type targetType, Type targetType,
String targetPropertyName, String targetPropertyName,
String dateFormat ) { String dateFormat ) {
// first try to find a matching source method // first try to find a matching source method
SourceMethod matchingSourceMethod = getBestMatch( SourceMethod matchingSourceMethod = getBestMatch(
mappingMethod, mappingMethod,
@ -249,7 +253,7 @@ public class MappingResolver {
* *
* @return a method reference. * @return a method reference.
*/ */
private MethodReference getMappingMethodReferenceBasedOnParameter(SourceMethod mappingMethod, private MethodReference resolveViaMethodAndMethod( SourceMethod mappingMethod,
String mappedElement, String mappedElement,
List<MapperReference> mapperReferences, List<MapperReference> mapperReferences,
List<SourceMethod> methods, List<SourceMethod> methods,
@ -272,7 +276,7 @@ public class MappingResolver {
// a nested method call can be called. so C = methodY( methodX (A) ) // a nested method call can be called. so C = methodY( methodX (A) )
for ( Method methodYCandidate : methodYCandidates ) { for ( Method methodYCandidate : methodYCandidates ) {
if ( methodYCandidate.getSourceParameters().size() == 1 ) { if ( methodYCandidate.getSourceParameters().size() == 1 ) {
methodRefY = getMappingMethodReferenceBasedOnMethod( methodRefY = resolveViaMethod(
mappingMethod, mappingMethod,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
@ -283,7 +287,7 @@ public class MappingResolver {
dateFormat dateFormat
); );
if ( methodRefY != null ) { if ( methodRefY != null ) {
MethodReference methodRefX = getMappingMethodReferenceBasedOnMethod( MethodReference methodRefX = resolveViaMethod(
mappingMethod, mappingMethod,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
@ -349,7 +353,8 @@ public class MappingResolver {
} }
private MethodReference getMappingMethodReference(SourceMethod method, List<MapperReference> mapperReferences, private MethodReference getMappingMethodReference( SourceMethod method,
List<MapperReference> mapperReferences,
Type targetType ) { Type targetType ) {
MapperReference mapperReference = findMapperReference( mapperReferences, method ); MapperReference mapperReference = findMapperReference( mapperReferences, method );

View File

@ -0,0 +1,24 @@
/**
* 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.
*/
/**
* <p>
* Contains all helper classes for the {@link org.mapstruct.ap.processor.MapperCreationProcessor}
* </p>
*/
package org.mapstruct.ap.processor.creation;

View File

@ -20,7 +20,7 @@
--> -->
<#if !( targetType.collectionType || targetType.mapType ) > <#if !( targetType.collectionType || targetType.mapType ) >
<#-- non collections or maps --> <#-- non collections or maps -->
<#if ( !sourceType.primitive && parameterAssignment.assignmentType!="ASSIGNMENT" ) > <#if ( !sourceType.primitive && propertyAssignment.assignmentType!="ASSIGNMENT" ) >
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
<@assignmentLine/> <@assignmentLine/>
} }
@ -57,7 +57,7 @@
target="${ext.targetBeanName}.${targetAccessorName}" target="${ext.targetBeanName}.${targetAccessorName}"
source="${sourceBeanName}.${sourceAccessorName}"> source="${sourceBeanName}.${sourceAccessorName}">
<#compress> <#compress>
<#if parameterAssignment?? && parameterAssignment.assignmentType!="ASSIGNMENT"> <#if propertyAssignment?? && propertyAssignment.assignmentType!="ASSIGNMENT">
<@assignmentLine target source/> <@assignmentLine target source/>
<#else> <#else>
${target}( new <#if targetType.implementationType??><@includeModel object=targetType.implementationType/><#else><@includeModel object=targetType/></#if>( ${source}() ) ); ${target}( new <#if targetType.implementationType??><@includeModel object=targetType.implementationType/><#else><@includeModel object=targetType/></#if>( ${source}() ) );
@ -68,5 +68,5 @@
<#macro assignmentLine <#macro assignmentLine
target="${ext.targetBeanName}.${targetAccessorName}" target="${ext.targetBeanName}.${targetAccessorName}"
source="${sourceBeanName}.${sourceAccessorName}"> source="${sourceBeanName}.${sourceAccessorName}">
<@includeModel object=parameterAssignment target=target source="${source}()" targetType=targetType raw=true/> <@includeModel object=propertyAssignment target=target source="${source}()" targetType=targetType raw=true/>
</#macro> </#macro>