mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#157 renaming ParameterAssignment in the more suitable TargetAssignment, creating package creation in processor for creation helpers, refactor MappingResolver
This commit is contained in:
parent
fbd06315a0
commit
9c17909fd6
@ -33,10 +33,10 @@ import org.mapstruct.ap.util.Strings;
|
||||
*/
|
||||
public class IterableMappingMethod extends MappingMethod {
|
||||
|
||||
private final ParameterAssignment elementAssignment;
|
||||
private final TargetAssignment elementAssignment;
|
||||
private final MethodReference factoryMethod;
|
||||
|
||||
public IterableMappingMethod(SourceMethod method, ParameterAssignment parameterAssignment,
|
||||
public IterableMappingMethod(SourceMethod method, TargetAssignment parameterAssignment,
|
||||
MethodReference factoryMethod) {
|
||||
super( method );
|
||||
this.elementAssignment = parameterAssignment;
|
||||
@ -53,7 +53,7 @@ public class IterableMappingMethod extends MappingMethod {
|
||||
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
||||
}
|
||||
|
||||
public ParameterAssignment getElementAssignment() {
|
||||
public TargetAssignment getElementAssignment() {
|
||||
return elementAssignment;
|
||||
}
|
||||
|
||||
|
@ -33,11 +33,11 @@ import org.mapstruct.ap.util.Strings;
|
||||
*/
|
||||
public class MapMappingMethod extends MappingMethod {
|
||||
|
||||
private final ParameterAssignment keyAssignment;
|
||||
private final ParameterAssignment valueAssignment;
|
||||
private final TargetAssignment keyAssignment;
|
||||
private final TargetAssignment valueAssignment;
|
||||
private final MethodReference factoryMethod;
|
||||
|
||||
public MapMappingMethod(SourceMethod method, ParameterAssignment keyAssignment, ParameterAssignment valueAssignment,
|
||||
public MapMappingMethod(SourceMethod method, TargetAssignment keyAssignment, TargetAssignment valueAssignment,
|
||||
MethodReference factoryMethod) {
|
||||
super( method );
|
||||
|
||||
@ -56,11 +56,11 @@ public class MapMappingMethod extends MappingMethod {
|
||||
throw new IllegalStateException( "Method " + this + " has no source parameter." );
|
||||
}
|
||||
|
||||
public ParameterAssignment getKeyAssignment() {
|
||||
public TargetAssignment getKeyAssignment() {
|
||||
return keyAssignment;
|
||||
}
|
||||
|
||||
public ParameterAssignment getValueAssignment() {
|
||||
public TargetAssignment getValueAssignment() {
|
||||
return valueAssignment;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.model.common.ModelElement;
|
||||
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
|
||||
* {@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 String targetReadAccessorName;
|
||||
|
||||
private final ParameterAssignment parameterAssignment;
|
||||
private final TargetAssignment propertyAssignment;
|
||||
|
||||
|
||||
public PropertyMapping(String sourceBeanName, String sourceName, String sourceAccessorName, Type sourceType,
|
||||
String targetName, String targetAccessorName, Type targetType,
|
||||
ParameterAssignment parameterAssignment ) {
|
||||
TargetAssignment propertyAssignment ) {
|
||||
|
||||
this.sourceBeanName = sourceBeanName;
|
||||
this.sourceName = sourceName;
|
||||
@ -64,7 +64,7 @@ public class PropertyMapping extends ModelElement {
|
||||
this.targetReadAccessorName =
|
||||
this.isTargetAccessorSetter ? "get" + targetAccessorName.substring( 3 ) : targetAccessorName;
|
||||
|
||||
this.parameterAssignment = parameterAssignment;
|
||||
this.propertyAssignment = propertyAssignment;
|
||||
}
|
||||
|
||||
public String getSourceBeanName() {
|
||||
@ -95,8 +95,8 @@ public class PropertyMapping extends ModelElement {
|
||||
return targetType;
|
||||
}
|
||||
|
||||
public ParameterAssignment getParameterAssignment() {
|
||||
return parameterAssignment;
|
||||
public TargetAssignment getPropertyAssignment() {
|
||||
return propertyAssignment;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -120,15 +120,15 @@ public class PropertyMapping extends ModelElement {
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> importTypes = new HashSet<Type>();
|
||||
if ( parameterAssignment != null ) {
|
||||
if ( propertyAssignment != null ) {
|
||||
if ( isTargetAccessorSetter()
|
||||
&& parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT )
|
||||
&& propertyAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT )
|
||||
&& ( targetType.isCollectionType() || targetType.isMapType() ) ) {
|
||||
importTypes.addAll( targetType.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( !parameterAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) ) {
|
||||
importTypes.addAll( parameterAssignment.getImportTypes() );
|
||||
if ( !propertyAssignment.getAssignmentType().equals( AssignmentType.ASSIGNMENT ) ) {
|
||||
importTypes.addAll( propertyAssignment.getImportTypes() );
|
||||
}
|
||||
}
|
||||
return importTypes;
|
||||
@ -141,7 +141,7 @@ public class PropertyMapping extends ModelElement {
|
||||
"\n sourceType=" + sourceType + "," +
|
||||
"\n targetName='" + targetAccessorName + "\'," +
|
||||
"\n targetType=" + targetType + "," +
|
||||
"\n parameterAssignment=" + parameterAssignment +
|
||||
"\n propertyAssignment=" + propertyAssignment +
|
||||
"\n}";
|
||||
}
|
||||
}
|
||||
|
@ -32,12 +32,12 @@ import org.mapstruct.ap.model.common.Type;
|
||||
* <ol>
|
||||
* <li>MethodReference</li>
|
||||
* <li>TypeConversion</li>
|
||||
* <li>Simple Assignment (empty ParameterAssignment)</li>
|
||||
* <li>Simple Assignment (empty TargetAssignment)</li>
|
||||
* </ol>
|
||||
*
|
||||
* @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;
|
||||
|
||||
|
||||
public ParameterAssignment() {
|
||||
public TargetAssignment() {
|
||||
assignmentType = AssignmentType.ASSIGNMENT;
|
||||
}
|
||||
|
||||
public ParameterAssignment( MethodReference methodReference ) {
|
||||
public TargetAssignment( MethodReference methodReference ) {
|
||||
assignmentType = AssignmentType.METHOD_REFERENCE;
|
||||
this.methodReference = methodReference;
|
||||
}
|
||||
|
||||
public ParameterAssignment( TypeConversion typeConversion ) {
|
||||
public TargetAssignment( TypeConversion typeConversion ) {
|
||||
assignmentType = AssignmentType.TYPE_CONVERSION;
|
||||
this.typeConversion = typeConversion;
|
||||
}
|
@ -18,7 +18,8 @@
|
||||
*/
|
||||
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.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -642,7 +643,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
|
||||
String mappedElement = "property '" + Executables.getPropertyName( sourceAccessor ) + "'";
|
||||
|
||||
ParameterAssignment parameterAssignment = mappingResolver.getParameterAssignment(
|
||||
TargetAssignment parameterAssignment = mappingResolver.getTargetAssignment(
|
||||
method,
|
||||
mappedElement,
|
||||
mapperReferences,
|
||||
@ -689,7 +690,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
String dateFormat = method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null;
|
||||
String conversionStr = Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() );
|
||||
|
||||
ParameterAssignment parameterAssignment = mappingResolver.getParameterAssignment(
|
||||
TargetAssignment parameterAssignment = mappingResolver.getTargetAssignment(
|
||||
method,
|
||||
"collection element",
|
||||
mapperReferences,
|
||||
@ -727,7 +728,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
Type keyTargetType = resultTypeParams.get( 0 );
|
||||
String keyDateFormat = method.getMapMapping() != null ? method.getMapMapping().getKeyFormat() : null;
|
||||
|
||||
ParameterAssignment parameterAssignmentKey = mappingResolver.getParameterAssignment(
|
||||
TargetAssignment parameterAssignmentKey = mappingResolver.getTargetAssignment(
|
||||
method,
|
||||
"map key",
|
||||
mapperReferences,
|
||||
@ -756,7 +757,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
Type valueTargetType = resultTypeParams.get( 1 );
|
||||
String valueDateFormat = method.getMapMapping() != null ? method.getMapMapping().getValueFormat() : null;
|
||||
|
||||
ParameterAssignment parameterAssignmentValue = mappingResolver.getParameterAssignment(
|
||||
TargetAssignment parameterAssignmentValue = mappingResolver.getTargetAssignment(
|
||||
method,
|
||||
"map value",
|
||||
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() ) &&
|
||||
collectionOrMapTargetTypeHasCompatibleConstructor ) ) {
|
||||
return true;
|
||||
|
@ -16,9 +16,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* 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.HashSet;
|
||||
import java.util.List;
|
||||
@ -107,11 +107,11 @@ public class MappingResolver {
|
||||
* <ol>
|
||||
* <li>MethodReference</li>
|
||||
* <li>TypeConversion</li>
|
||||
* <li>Simple Assignment (empty ParameterAssignment)</li>
|
||||
* <li>Simple Assignment (empty TargetAssignment)</li>
|
||||
* <li>null, no assignment found</li>
|
||||
* </ol>
|
||||
*/
|
||||
public ParameterAssignment getParameterAssignment( SourceMethod mappingMethod,
|
||||
public TargetAssignment getTargetAssignment( SourceMethod mappingMethod,
|
||||
String mappedElement,
|
||||
List<MapperReference> mapperReferences,
|
||||
List<SourceMethod> methods,
|
||||
@ -121,7 +121,7 @@ public class MappingResolver {
|
||||
String dateFormat,
|
||||
String sourceReference ) {
|
||||
|
||||
MethodReference mappingMethodReference = getMappingMethodReferenceBasedOnMethod(
|
||||
MethodReference mappingMethodReference = resolveViaMethod(
|
||||
mappingMethod,
|
||||
mappedElement,
|
||||
mapperReferences,
|
||||
@ -132,21 +132,21 @@ public class MappingResolver {
|
||||
dateFormat
|
||||
);
|
||||
|
||||
ParameterAssignment parameterAssignment = null;
|
||||
TargetAssignment assignment = null;
|
||||
|
||||
if (mappingMethodReference != null ) {
|
||||
parameterAssignment = new ParameterAssignment(mappingMethodReference );
|
||||
assignment = new TargetAssignment(mappingMethodReference );
|
||||
}
|
||||
else if (sourceType.isAssignableTo( targetType ) ) {
|
||||
parameterAssignment = new ParameterAssignment();
|
||||
assignment = new TargetAssignment();
|
||||
}
|
||||
else {
|
||||
TypeConversion conversion = getConversion( sourceType, targetType, dateFormat, sourceReference );
|
||||
TypeConversion conversion = resolveViaConversion( sourceType, targetType, dateFormat, sourceReference );
|
||||
if ( conversion != null ) {
|
||||
parameterAssignment = new ParameterAssignment(conversion );
|
||||
assignment = new TargetAssignment(conversion );
|
||||
}
|
||||
else {
|
||||
mappingMethodReference = getMappingMethodReferenceBasedOnParameter(
|
||||
mappingMethodReference = resolveViaMethodAndMethod(
|
||||
mappingMethod,
|
||||
mappedElement,
|
||||
mapperReferences,
|
||||
@ -157,15 +157,18 @@ public class MappingResolver {
|
||||
dateFormat
|
||||
);
|
||||
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 );
|
||||
|
||||
if ( conversionProvider == null ) {
|
||||
@ -192,7 +195,7 @@ public class MappingResolver {
|
||||
*
|
||||
* @return a method reference.
|
||||
*/
|
||||
private MethodReference getMappingMethodReferenceBasedOnMethod(SourceMethod mappingMethod,
|
||||
private MethodReference resolveViaMethod( SourceMethod mappingMethod,
|
||||
String mappedElement,
|
||||
List<MapperReference> mapperReferences,
|
||||
List<SourceMethod> methods,
|
||||
@ -200,6 +203,7 @@ public class MappingResolver {
|
||||
Type targetType,
|
||||
String targetPropertyName,
|
||||
String dateFormat ) {
|
||||
|
||||
// first try to find a matching source method
|
||||
SourceMethod matchingSourceMethod = getBestMatch(
|
||||
mappingMethod,
|
||||
@ -249,7 +253,7 @@ public class MappingResolver {
|
||||
*
|
||||
* @return a method reference.
|
||||
*/
|
||||
private MethodReference getMappingMethodReferenceBasedOnParameter(SourceMethod mappingMethod,
|
||||
private MethodReference resolveViaMethodAndMethod( SourceMethod mappingMethod,
|
||||
String mappedElement,
|
||||
List<MapperReference> mapperReferences,
|
||||
List<SourceMethod> methods,
|
||||
@ -272,7 +276,7 @@ public class MappingResolver {
|
||||
// a nested method call can be called. so C = methodY( methodX (A) )
|
||||
for ( Method methodYCandidate : methodYCandidates ) {
|
||||
if ( methodYCandidate.getSourceParameters().size() == 1 ) {
|
||||
methodRefY = getMappingMethodReferenceBasedOnMethod(
|
||||
methodRefY = resolveViaMethod(
|
||||
mappingMethod,
|
||||
mappedElement,
|
||||
mapperReferences,
|
||||
@ -283,7 +287,7 @@ public class MappingResolver {
|
||||
dateFormat
|
||||
);
|
||||
if ( methodRefY != null ) {
|
||||
MethodReference methodRefX = getMappingMethodReferenceBasedOnMethod(
|
||||
MethodReference methodRefX = resolveViaMethod(
|
||||
mappingMethod,
|
||||
mappedElement,
|
||||
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 ) {
|
||||
MapperReference mapperReference = findMapperReference( mapperReferences, method );
|
||||
|
@ -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;
|
@ -20,7 +20,7 @@
|
||||
-->
|
||||
<#if !( targetType.collectionType || targetType.mapType ) >
|
||||
<#-- non collections or maps -->
|
||||
<#if ( !sourceType.primitive && parameterAssignment.assignmentType!="ASSIGNMENT" ) >
|
||||
<#if ( !sourceType.primitive && propertyAssignment.assignmentType!="ASSIGNMENT" ) >
|
||||
if ( ${sourceBeanName}.${sourceAccessorName}() != null ) {
|
||||
<@assignmentLine/>
|
||||
}
|
||||
@ -57,7 +57,7 @@
|
||||
target="${ext.targetBeanName}.${targetAccessorName}"
|
||||
source="${sourceBeanName}.${sourceAccessorName}">
|
||||
<#compress>
|
||||
<#if parameterAssignment?? && parameterAssignment.assignmentType!="ASSIGNMENT">
|
||||
<#if propertyAssignment?? && propertyAssignment.assignmentType!="ASSIGNMENT">
|
||||
<@assignmentLine target source/>
|
||||
<#else>
|
||||
${target}( new <#if targetType.implementationType??><@includeModel object=targetType.implementationType/><#else><@includeModel object=targetType/></#if>( ${source}() ) );
|
||||
@ -68,5 +68,5 @@
|
||||
<#macro assignmentLine
|
||||
target="${ext.targetBeanName}.${targetAccessorName}"
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user