#19 Simplifying creation of MappingMethod objects

This commit is contained in:
Gunnar Morling 2013-07-21 10:45:53 +02:00
parent 466a640ced
commit 476f147078
11 changed files with 107 additions and 145 deletions

View File

@ -21,7 +21,7 @@ package org.mapstruct.ap.model;
import java.util.List;
import java.util.Set;
import org.mapstruct.ap.model.source.Parameter;
import org.mapstruct.ap.model.source.Method;
/**
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one
@ -34,10 +34,8 @@ public class BeanMappingMethod extends MappingMethod {
private final List<PropertyMapping> propertyMappings;
public BeanMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters,
Type resultType, String resultName, Type returnType,
List<PropertyMapping> propertyMappings) {
super( name, parameters, sourceParameters, resultType, resultName, returnType );
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings) {
super( method );
this.propertyMappings = propertyMappings;
}

View File

@ -18,10 +18,9 @@
*/
package org.mapstruct.ap.model;
import java.util.List;
import java.util.Set;
import org.mapstruct.ap.model.source.Parameter;
import org.mapstruct.ap.model.source.Method;
/**
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one iterable type to another. The collection
@ -34,10 +33,9 @@ public class IterableMappingMethod extends MappingMethod {
private final MappingMethodReference elementMappingMethod;
private final TypeConversion conversion;
public IterableMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters,
Type resultType, String resultName, Type returnType,
MappingMethodReference elementMappingMethod, TypeConversion conversion) {
super( name, parameters, sourceParameters, resultType, resultName, returnType );
public IterableMappingMethod(Method method, MappingMethodReference elementMappingMethod,
TypeConversion conversion) {
super( method );
this.elementMappingMethod = elementMappingMethod;
this.conversion = conversion;
}

View File

@ -18,10 +18,9 @@
*/
package org.mapstruct.ap.model;
import java.util.List;
import java.util.Set;
import org.mapstruct.ap.model.source.Parameter;
import org.mapstruct.ap.model.source.Method;
/**
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one {@code Map} type to another. Keys and
@ -36,11 +35,9 @@ public class MapMappingMethod extends MappingMethod {
private final MappingMethodReference valueMappingMethod;
private final TypeConversion valueConversion;
public MapMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters, Type resultType,
String resultName, Type returnType,
MappingMethodReference keyMappingMethod, TypeConversion keyConversion,
public MapMappingMethod(Method method, MappingMethodReference keyMappingMethod, TypeConversion keyConversion,
MappingMethodReference valueMappingMethod, TypeConversion valueConversion) {
super( name, parameters, sourceParameters, resultType, resultName, returnType );
super( method );
this.keyMappingMethod = keyMappingMethod;
this.keyConversion = keyConversion;

View File

@ -18,10 +18,12 @@
*/
package org.mapstruct.ap.model;
import java.beans.Introspector;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.model.source.Parameter;
/**
@ -33,22 +35,16 @@ public abstract class MappingMethod extends AbstractModelElement {
private final String name;
private final List<Parameter> parameters;
private final List<Parameter> sourceParameters;
private final Type resultType;
private final String resultName;
private final Type returnType;
private final boolean existingInstanceMapping;
private final Parameter singleSourceParameter;
private final Parameter targetParameter;
protected MappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters, Type resultType,
String resultName, Type returnType) {
this.name = name;
this.parameters = parameters;
this.sourceParameters = sourceParameters;
this.resultType = resultType;
this.resultName = resultName;
this.returnType = returnType;
this.existingInstanceMapping =
( null != parameters && null != sourceParameters && parameters.size() > sourceParameters.size() );
public MappingMethod(Method method) {
this.name = method.getName();
this.parameters = method.getParameters();
this.returnType = method.getReturnType();
this.singleSourceParameter = method.getSingleSourceParameter();
this.targetParameter = method.getTargetParameter();
}
public String getName() {
@ -59,16 +55,16 @@ public abstract class MappingMethod extends AbstractModelElement {
return parameters;
}
public List<Parameter> getSourceParameters() {
return sourceParameters;
public Parameter getSingleSourceParameter() {
return singleSourceParameter;
}
public Type getResultType() {
return resultType;
return targetParameter != null ? targetParameter.getType() : returnType;
}
public String getResultName() {
return resultName;
return targetParameter != null ? targetParameter.getName() : Introspector.decapitalize( returnType.getName() );
}
public Type getReturnType() {
@ -76,14 +72,14 @@ public abstract class MappingMethod extends AbstractModelElement {
}
public boolean isExistingInstanceMapping() {
return existingInstanceMapping;
return targetParameter != null;
}
@Override
public Set<Type> getImportTypes() {
Set<Type> types = new HashSet<Type>();
for ( Parameter param : getParameters() ) {
for ( Parameter param : parameters ) {
types.add( param.getType() );
}

View File

@ -21,6 +21,8 @@ package org.mapstruct.ap.model;
import java.util.HashSet;
import java.util.Set;
import org.mapstruct.ap.model.source.Method;
/**
* Represents a reference to {@link MappingMethod}.
*
@ -30,9 +32,9 @@ public class MappingMethodReference extends MappingMethod {
private final Type declaringMapper;
public MappingMethodReference(Type declaringMapper, String name) {
super( name, null, null, null, null, null );
this.declaringMapper = declaringMapper;
public MappingMethodReference(Method method) {
super( method );
this.declaringMapper = method.getDeclaringMapper();
}
public Type getDeclaringMapper() {

View File

@ -18,18 +18,18 @@
*/
package org.mapstruct.ap.model.source;
import java.util.Arrays;
import java.beans.Introspector;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.ExecutableElement;
import org.mapstruct.ap.model.Type;
import org.mapstruct.ap.util.Strings;
/**
* Represents a mapping method with source and target type and the mappings
* between the properties of source and target type.
* Represents a mapping method with source and target type and the mappings between the properties of source and target
* type.
*
* @author Gunnar Morling
*/
@ -38,66 +38,77 @@ public class Method {
private final Type declaringMapper;
private final ExecutableElement executable;
private final List<Parameter> parameters;
private final List<Parameter> sourceParameters;
private final String resultName;
private final Type resultType;
private final Type returnType;
private Map<String, Mapping> mappings;
private IterableMapping iterableMapping;
private MapMapping mapMapping;
private final Parameter singleSourceParameter;
private final Parameter targetParameter;
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters,
List<Parameter> sourceParameters, Type resultType,
String resultName, Type targetType,
Map<String, Mapping> mappings,
Type returnType, Map<String, Mapping> mappings,
IterableMapping iterableMapping, MapMapping mapMapping) {
return new Method(
null,
executable,
parameters,
sourceParameters,
resultType,
resultName,
targetType,
returnType,
mappings,
iterableMapping,
mapMapping
);
}
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, String parameterName,
Type sourceType, Type targetType) {
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable,
List<Parameter> parameters, Type returnType) {
return new Method(
declaringMapper,
executable,
Arrays.asList( new Parameter( parameterName, sourceType ) ),
Arrays.asList( new Parameter( parameterName, sourceType ) ),
targetType,
null,
targetType,
parameters,
returnType,
Collections.<String, Mapping>emptyMap(),
null,
null
);
}
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters,
List<Parameter> sourceParameters, Type resultType, String resultName,
Type returnType,
Map<String, Mapping> mappings, IterableMapping iterableMapping,
MapMapping mapMapping) {
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType,
Map<String, Mapping> mappings, IterableMapping iterableMapping, MapMapping mapMapping) {
this.declaringMapper = declaringMapper;
this.executable = executable;
this.parameters = parameters;
this.sourceParameters = sourceParameters;
this.resultType = resultType;
this.resultName = resultName;
this.returnType = returnType;
this.mappings = mappings;
this.iterableMapping = iterableMapping;
this.mapMapping = mapMapping;
this.singleSourceParameter = determineSingleSourceParameter();
this.targetParameter = determineTargetParameter( parameters );
}
private Parameter determineTargetParameter(Iterable<Parameter> parameters) {
for ( Parameter parameter : parameters ) {
if ( parameter.isMappingTarget() ) {
return parameter;
}
}
return null;
}
private Parameter determineSingleSourceParameter() {
for ( Parameter parameter : parameters ) {
if ( !parameter.isMappingTarget() ) {
return parameter;
}
}
throw new IllegalStateException( "Method " + this + " has no source parameter." );
}
/**
@ -124,15 +135,11 @@ public class Method {
}
public String getResultName() {
return resultName;
}
public List<Parameter> getSourceParameters() {
return sourceParameters;
return targetParameter != null ? targetParameter.getName() : Introspector.decapitalize( returnType.getName() );
}
public Type getResultType() {
return resultType;
return targetParameter != null ? targetParameter.getType() : returnType;
}
public Type getReturnType() {
@ -165,20 +172,24 @@ public class Method {
public boolean reverses(Method method) {
return
equals( getSingleSourceType(), method.getReturnType() )
&& equals( returnType, method.getSingleSourceType() );
equals( getSingleSourceParameter().getType(), method.getResultType() )
&& equals( getResultType(), method.getSingleSourceParameter().getType() );
}
public Type getSingleSourceType() {
return sourceParameters.size() == 1 ? sourceParameters.get( 0 ).getType() : null;
public Parameter getSingleSourceParameter() {
return singleSourceParameter;
}
public Parameter getTargetParameter() {
return targetParameter;
}
public boolean isIterableMapping() {
return getSingleSourceType().isIterableType() && resultType.isIterableType();
return getSingleSourceParameter().getType().isIterableType() && getResultType().isIterableType();
}
public boolean isMapMapping() {
return getSingleSourceType().isMapType() && resultType.isMapType();
return getSingleSourceParameter().getType().isMapType() && getResultType().isMapType();
}
private boolean equals(Object o1, Object o2) {
@ -187,19 +198,6 @@ public class Method {
@Override
public String toString() {
return returnType + " " + getName() + "(" + getParamsList() + ")";
}
private String getParamsList() {
StringBuilder sb = new StringBuilder();
for ( Iterator<Parameter> it = parameters.iterator(); it.hasNext(); ) {
Parameter param = it.next();
sb.append( param.getType() ).append( " " ).append( param.getName() );
if ( it.hasNext() ) {
sb.append( ", " );
}
}
return sb.toString();
return returnType + " " + getName() + "(" + Strings.join( parameters, ", " ) + ")";
}
}

View File

@ -202,7 +202,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
Map<String, Mapping> mappings = method.getMappings();
TypeElement resultTypeElement = elementUtils.getTypeElement( method.getResultType().getCanonicalName() );
TypeElement parameterElement = elementUtils.getTypeElement( method.getSingleSourceType().getCanonicalName() );
TypeElement parameterElement = elementUtils.getTypeElement(
method.getSingleSourceParameter()
.getType()
.getCanonicalName()
);
List<ExecutableElement> sourceGetters = Filters.getterMethodsIn(
elementUtils.getAllMembers( parameterElement )
@ -251,15 +255,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
mappedTargetProperties
);
return new BeanMappingMethod(
method.getName(),
method.getParameters(),
method.getSourceParameters(),
method.getResultType(),
method.getResultName(),
method.getReturnType(),
propertyMappings
);
return new BeanMappingMethod( method, propertyMappings );
}
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
@ -300,7 +296,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
String.format(
"Unknown property \"%s\" in parameter type %s.",
mappedProperty.getSourceName(),
method.getSingleSourceType()
method.getSingleSourceParameter().getType()
),
method.getExecutable(),
mappedProperty.getMirror(),
@ -333,12 +329,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
sourceType,
targetType,
dateFormat,
method.getSourceParameters().get( 0 ).getName() + "."
method.getSingleSourceParameter().getName() + "."
+ getterMethod.getSimpleName().toString() + "()"
);
PropertyMapping property = new PropertyMapping(
method.getSourceParameters().get( 0 ).getName(),
method.getSingleSourceParameter().getName(),
method.getResultName(),
executables.getPropertyName( getterMethod ),
getterMethod.getSimpleName().toString(),
@ -359,7 +355,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
}
private MappingMethod getIterableMappingMethod(List<Method> methods, Method method) {
Type sourceElementType = method.getSourceParameters().get( 0 ).getType().getTypeParameters().get( 0 );
Type sourceElementType = method.getSingleSourceParameter().getType().getTypeParameters().get( 0 );
Type targetElementType = method.getResultType().getTypeParameters().get( 0 );
TypeConversion conversion = getConversion(
@ -370,19 +366,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
);
return new IterableMappingMethod(
method.getName(),
method.getParameters(),
method.getSourceParameters(),
method.getResultType(),
method.getResultName(),
method.getReturnType(),
method,
getMappingMethodReference( methods, sourceElementType, targetElementType ),
conversion
);
}
private MappingMethod getMapMappingMethod(List<Method> methods, Method method) {
List<Type> sourceTypeParams = method.getSourceParameters().get( 0 ).getType().getTypeParameters();
List<Type> sourceTypeParams = method.getSingleSourceParameter().getType().getTypeParameters();
Type sourceKeyType = sourceTypeParams.get( 0 );
Type sourceValueType = sourceTypeParams.get( 1 );
@ -408,18 +399,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
targetValueType
);
return new MapMappingMethod(
method.getName(),
method.getParameters(),
method.getSourceParameters(),
method.getResultType(),
method.getResultName(),
method.getReturnType(),
keyMappingMethod,
keyConversion,
valueMappingMethod,
valueConversion
);
return new MapMappingMethod( method, keyMappingMethod, keyConversion, valueMappingMethod, valueConversion );
}
private TypeConversion getConversion(Type sourceType, Type targetType, String dateFormat, String sourceReference) {
@ -435,14 +415,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
private MappingMethodReference getMappingMethodReference(Iterable<Method> methods, Type parameterType,
Type returnType) {
for ( Method oneMethod : methods ) {
Parameter singleSourceParam = oneMethod.getSourceParameters().get( 0 );
Parameter singleSourceParam = oneMethod.getSingleSourceParameter();
if ( singleSourceParam.getType().equals( parameterType ) &&
oneMethod.getReturnType().equals( returnType ) ) {
return new MappingMethodReference(
oneMethod.getDeclaringMapper(),
oneMethod.getName()
);
oneMethod.getResultType().equals( returnType ) ) {
return new MappingMethodReference( oneMethod );
}
}

View File

@ -133,9 +133,6 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
Method.forMethodRequiringImplementation(
method,
parameters,
sourceParameters,
resultType,
selectResultName( targetParameter, resultType ),
returnType,
getMappings( method ),
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ) ),
@ -152,8 +149,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
Method.forReferencedMethod(
typeUtil.getType( typeUtils.getDeclaredType( element ) ),
method,
parameters.get( 0 ).getName(),
parameters.get( 0 ).getType(),
parameters,
returnType
);
}

View File

@ -20,7 +20,7 @@
-->
@Override
public ${returnType.name} ${name}(<#list parameters as param>${param.type.name} ${param.name}<#if param_has_next>, </#if></#list>) {
if ( ${sourceParameters[0].name} == null ) {
if ( ${singleSourceParameter.name} == null ) {
return<#if returnType.name != "void"> null</#if>;
}
<#if !existingInstanceMapping>

View File

@ -20,7 +20,7 @@
-->
@Override
public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
if ( ${sourceParameters[0].name} == null ) {
if ( ${singleSourceParameter.name} == null ) {
return<#if returnType.name != "void"> null</#if>;
}
@ -31,9 +31,9 @@
<#if resultType.name == "Iterable" && resultType.packageName == "java.lang">${resultType.iterableImplementationType.name}<#else>${resultType.name}</#if><<@includeModel object=resultType.typeParameters[0]/>> ${resultName} = new <#if resultType.iterableImplementationType??>${resultType.iterableImplementationType.name}<#else>${resultType.name}</#if><<@includeModel object=resultType.typeParameters[0]/>>();
</#if>
for ( <@includeModel object=sourceParameters[0].type.typeParameters[0]/> ${sourceParameters[0].type.typeParameters[0].name?uncap_first} : ${sourceParameters[0].name} ) {
for ( <@includeModel object=singleSourceParameter.type.typeParameters[0]/> ${singleSourceParameter.type.typeParameters[0].name?uncap_first} : ${singleSourceParameter.name} ) {
<#if elementMappingMethod??>
${resultName}.add( <@includeModel object=elementMappingMethod input="${sourceParameters[0].type.typeParameters[0].name?uncap_first}"/> );
${resultName}.add( <@includeModel object=elementMappingMethod input="${singleSourceParameter.type.typeParameters[0].name?uncap_first}"/> );
<#else>
<#if (conversion.exceptionTypes?size == 0) >
${resultName}.add( <@includeModel object=conversion/> );

View File

@ -20,7 +20,7 @@
-->
@Override
public <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
if ( ${sourceParameters[0].name} == null ) {
if ( ${singleSourceParameter.name} == null ) {
return<#if returnType.name != "void"> null</#if>;
}
@ -30,7 +30,7 @@
<@includeModel object=resultType /> ${resultName} = new <#if resultType.mapImplementationType??><@includeModel object=resultType.mapImplementationType /><#else><@includeModel object=resultType /></#if>();
</#if>
for ( Map.Entry<<#list sourceParameters[0].type.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> entry : ${sourceParameters[0].name}.entrySet() ) {
for ( Map.Entry<<#list singleSourceParameter.type.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> entry : ${singleSourceParameter.name}.entrySet() ) {
<#-- key -->
<#if keyMappingMethod??>