mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#19 Simplifying creation of MappingMethod objects
This commit is contained in:
parent
466a640ced
commit
476f147078
@ -21,7 +21,7 @@ package org.mapstruct.ap.model;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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
|
* 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;
|
private final List<PropertyMapping> propertyMappings;
|
||||||
|
|
||||||
public BeanMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters,
|
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings) {
|
||||||
Type resultType, String resultName, Type returnType,
|
super( method );
|
||||||
List<PropertyMapping> propertyMappings) {
|
|
||||||
super( name, parameters, sourceParameters, resultType, resultName, returnType );
|
|
||||||
this.propertyMappings = propertyMappings;
|
this.propertyMappings = propertyMappings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,10 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
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
|
* 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 MappingMethodReference elementMappingMethod;
|
||||||
private final TypeConversion conversion;
|
private final TypeConversion conversion;
|
||||||
|
|
||||||
public IterableMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters,
|
public IterableMappingMethod(Method method, MappingMethodReference elementMappingMethod,
|
||||||
Type resultType, String resultName, Type returnType,
|
TypeConversion conversion) {
|
||||||
MappingMethodReference elementMappingMethod, TypeConversion conversion) {
|
super( method );
|
||||||
super( name, parameters, sourceParameters, resultType, resultName, returnType );
|
|
||||||
this.elementMappingMethod = elementMappingMethod;
|
this.elementMappingMethod = elementMappingMethod;
|
||||||
this.conversion = conversion;
|
this.conversion = conversion;
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
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
|
* 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 MappingMethodReference valueMappingMethod;
|
||||||
private final TypeConversion valueConversion;
|
private final TypeConversion valueConversion;
|
||||||
|
|
||||||
public MapMappingMethod(String name, List<Parameter> parameters, List<Parameter> sourceParameters, Type resultType,
|
public MapMappingMethod(Method method, MappingMethodReference keyMappingMethod, TypeConversion keyConversion,
|
||||||
String resultName, Type returnType,
|
|
||||||
MappingMethodReference keyMappingMethod, TypeConversion keyConversion,
|
|
||||||
MappingMethodReference valueMappingMethod, TypeConversion valueConversion) {
|
MappingMethodReference valueMappingMethod, TypeConversion valueConversion) {
|
||||||
super( name, parameters, sourceParameters, resultType, resultName, returnType );
|
super( method );
|
||||||
|
|
||||||
this.keyMappingMethod = keyMappingMethod;
|
this.keyMappingMethod = keyMappingMethod;
|
||||||
this.keyConversion = keyConversion;
|
this.keyConversion = keyConversion;
|
||||||
|
@ -18,10 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
|
import java.beans.Introspector;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.model.source.Method;
|
||||||
import org.mapstruct.ap.model.source.Parameter;
|
import org.mapstruct.ap.model.source.Parameter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,22 +35,16 @@ public abstract class MappingMethod extends AbstractModelElement {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final List<Parameter> parameters;
|
private final List<Parameter> parameters;
|
||||||
private final List<Parameter> sourceParameters;
|
|
||||||
private final Type resultType;
|
|
||||||
private final String resultName;
|
|
||||||
private final Type returnType;
|
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,
|
public MappingMethod(Method method) {
|
||||||
String resultName, Type returnType) {
|
this.name = method.getName();
|
||||||
this.name = name;
|
this.parameters = method.getParameters();
|
||||||
this.parameters = parameters;
|
this.returnType = method.getReturnType();
|
||||||
this.sourceParameters = sourceParameters;
|
this.singleSourceParameter = method.getSingleSourceParameter();
|
||||||
this.resultType = resultType;
|
this.targetParameter = method.getTargetParameter();
|
||||||
this.resultName = resultName;
|
|
||||||
this.returnType = returnType;
|
|
||||||
this.existingInstanceMapping =
|
|
||||||
( null != parameters && null != sourceParameters && parameters.size() > sourceParameters.size() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -59,16 +55,16 @@ public abstract class MappingMethod extends AbstractModelElement {
|
|||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Parameter> getSourceParameters() {
|
public Parameter getSingleSourceParameter() {
|
||||||
return sourceParameters;
|
return singleSourceParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getResultType() {
|
public Type getResultType() {
|
||||||
return resultType;
|
return targetParameter != null ? targetParameter.getType() : returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getResultName() {
|
public String getResultName() {
|
||||||
return resultName;
|
return targetParameter != null ? targetParameter.getName() : Introspector.decapitalize( returnType.getName() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getReturnType() {
|
public Type getReturnType() {
|
||||||
@ -76,14 +72,14 @@ public abstract class MappingMethod extends AbstractModelElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isExistingInstanceMapping() {
|
public boolean isExistingInstanceMapping() {
|
||||||
return existingInstanceMapping;
|
return targetParameter != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> types = new HashSet<Type>();
|
Set<Type> types = new HashSet<Type>();
|
||||||
|
|
||||||
for ( Parameter param : getParameters() ) {
|
for ( Parameter param : parameters ) {
|
||||||
types.add( param.getType() );
|
types.add( param.getType() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,8 @@ package org.mapstruct.ap.model;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.model.source.Method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a reference to {@link MappingMethod}.
|
* Represents a reference to {@link MappingMethod}.
|
||||||
*
|
*
|
||||||
@ -30,9 +32,9 @@ public class MappingMethodReference extends MappingMethod {
|
|||||||
|
|
||||||
private final Type declaringMapper;
|
private final Type declaringMapper;
|
||||||
|
|
||||||
public MappingMethodReference(Type declaringMapper, String name) {
|
public MappingMethodReference(Method method) {
|
||||||
super( name, null, null, null, null, null );
|
super( method );
|
||||||
this.declaringMapper = declaringMapper;
|
this.declaringMapper = method.getDeclaringMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getDeclaringMapper() {
|
public Type getDeclaringMapper() {
|
||||||
|
@ -18,18 +18,18 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model.source;
|
package org.mapstruct.ap.model.source;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.beans.Introspector;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.Type;
|
import org.mapstruct.ap.model.Type;
|
||||||
|
import org.mapstruct.ap.util.Strings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a mapping method with source and target type and the mappings
|
* Represents a mapping method with source and target type and the mappings between the properties of source and target
|
||||||
* between the properties of source and target type.
|
* type.
|
||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
@ -38,66 +38,77 @@ public class Method {
|
|||||||
private final Type declaringMapper;
|
private final Type declaringMapper;
|
||||||
private final ExecutableElement executable;
|
private final ExecutableElement executable;
|
||||||
private final List<Parameter> parameters;
|
private final List<Parameter> parameters;
|
||||||
private final List<Parameter> sourceParameters;
|
|
||||||
private final String resultName;
|
|
||||||
private final Type resultType;
|
|
||||||
private final Type returnType;
|
private final Type returnType;
|
||||||
|
|
||||||
private Map<String, Mapping> mappings;
|
private Map<String, Mapping> mappings;
|
||||||
private IterableMapping iterableMapping;
|
private IterableMapping iterableMapping;
|
||||||
private MapMapping mapMapping;
|
private MapMapping mapMapping;
|
||||||
|
|
||||||
|
private final Parameter singleSourceParameter;
|
||||||
|
private final Parameter targetParameter;
|
||||||
|
|
||||||
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters,
|
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters,
|
||||||
List<Parameter> sourceParameters, Type resultType,
|
Type returnType, Map<String, Mapping> mappings,
|
||||||
String resultName, Type targetType,
|
|
||||||
Map<String, Mapping> mappings,
|
|
||||||
IterableMapping iterableMapping, MapMapping mapMapping) {
|
IterableMapping iterableMapping, MapMapping mapMapping) {
|
||||||
|
|
||||||
return new Method(
|
return new Method(
|
||||||
null,
|
null,
|
||||||
executable,
|
executable,
|
||||||
parameters,
|
parameters,
|
||||||
sourceParameters,
|
returnType,
|
||||||
resultType,
|
|
||||||
resultName,
|
|
||||||
targetType,
|
|
||||||
mappings,
|
mappings,
|
||||||
iterableMapping,
|
iterableMapping,
|
||||||
mapMapping
|
mapMapping
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, String parameterName,
|
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable,
|
||||||
Type sourceType, Type targetType) {
|
List<Parameter> parameters, Type returnType) {
|
||||||
|
|
||||||
return new Method(
|
return new Method(
|
||||||
declaringMapper,
|
declaringMapper,
|
||||||
executable,
|
executable,
|
||||||
Arrays.asList( new Parameter( parameterName, sourceType ) ),
|
parameters,
|
||||||
Arrays.asList( new Parameter( parameterName, sourceType ) ),
|
returnType,
|
||||||
targetType,
|
|
||||||
null,
|
|
||||||
targetType,
|
|
||||||
Collections.<String, Mapping>emptyMap(),
|
Collections.<String, Mapping>emptyMap(),
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters,
|
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType,
|
||||||
List<Parameter> sourceParameters, Type resultType, String resultName,
|
Map<String, Mapping> mappings, IterableMapping iterableMapping, MapMapping mapMapping) {
|
||||||
Type returnType,
|
|
||||||
Map<String, Mapping> mappings, IterableMapping iterableMapping,
|
|
||||||
MapMapping mapMapping) {
|
|
||||||
this.declaringMapper = declaringMapper;
|
this.declaringMapper = declaringMapper;
|
||||||
this.executable = executable;
|
this.executable = executable;
|
||||||
this.parameters = parameters;
|
this.parameters = parameters;
|
||||||
this.sourceParameters = sourceParameters;
|
|
||||||
this.resultType = resultType;
|
|
||||||
this.resultName = resultName;
|
|
||||||
this.returnType = returnType;
|
this.returnType = returnType;
|
||||||
|
|
||||||
this.mappings = mappings;
|
this.mappings = mappings;
|
||||||
this.iterableMapping = iterableMapping;
|
this.iterableMapping = iterableMapping;
|
||||||
this.mapMapping = mapMapping;
|
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() {
|
public String getResultName() {
|
||||||
return resultName;
|
return targetParameter != null ? targetParameter.getName() : Introspector.decapitalize( returnType.getName() );
|
||||||
}
|
|
||||||
|
|
||||||
public List<Parameter> getSourceParameters() {
|
|
||||||
return sourceParameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getResultType() {
|
public Type getResultType() {
|
||||||
return resultType;
|
return targetParameter != null ? targetParameter.getType() : returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getReturnType() {
|
public Type getReturnType() {
|
||||||
@ -165,20 +172,24 @@ public class Method {
|
|||||||
|
|
||||||
public boolean reverses(Method method) {
|
public boolean reverses(Method method) {
|
||||||
return
|
return
|
||||||
equals( getSingleSourceType(), method.getReturnType() )
|
equals( getSingleSourceParameter().getType(), method.getResultType() )
|
||||||
&& equals( returnType, method.getSingleSourceType() );
|
&& equals( getResultType(), method.getSingleSourceParameter().getType() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getSingleSourceType() {
|
public Parameter getSingleSourceParameter() {
|
||||||
return sourceParameters.size() == 1 ? sourceParameters.get( 0 ).getType() : null;
|
return singleSourceParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Parameter getTargetParameter() {
|
||||||
|
return targetParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isIterableMapping() {
|
public boolean isIterableMapping() {
|
||||||
return getSingleSourceType().isIterableType() && resultType.isIterableType();
|
return getSingleSourceParameter().getType().isIterableType() && getResultType().isIterableType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMapMapping() {
|
public boolean isMapMapping() {
|
||||||
return getSingleSourceType().isMapType() && resultType.isMapType();
|
return getSingleSourceParameter().getType().isMapType() && getResultType().isMapType();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean equals(Object o1, Object o2) {
|
private boolean equals(Object o1, Object o2) {
|
||||||
@ -187,19 +198,6 @@ public class Method {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return returnType + " " + getName() + "(" + getParamsList() + ")";
|
return returnType + " " + getName() + "(" + Strings.join( parameters, ", " ) + ")";
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
Map<String, Mapping> mappings = method.getMappings();
|
Map<String, Mapping> mappings = method.getMappings();
|
||||||
|
|
||||||
TypeElement resultTypeElement = elementUtils.getTypeElement( method.getResultType().getCanonicalName() );
|
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(
|
List<ExecutableElement> sourceGetters = Filters.getterMethodsIn(
|
||||||
elementUtils.getAllMembers( parameterElement )
|
elementUtils.getAllMembers( parameterElement )
|
||||||
@ -251,15 +255,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
mappedTargetProperties
|
mappedTargetProperties
|
||||||
);
|
);
|
||||||
|
|
||||||
return new BeanMappingMethod(
|
return new BeanMappingMethod( method, propertyMappings );
|
||||||
method.getName(),
|
|
||||||
method.getParameters(),
|
|
||||||
method.getSourceParameters(),
|
|
||||||
method.getResultType(),
|
|
||||||
method.getResultName(),
|
|
||||||
method.getReturnType(),
|
|
||||||
propertyMappings
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
|
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
|
||||||
@ -300,7 +296,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
String.format(
|
String.format(
|
||||||
"Unknown property \"%s\" in parameter type %s.",
|
"Unknown property \"%s\" in parameter type %s.",
|
||||||
mappedProperty.getSourceName(),
|
mappedProperty.getSourceName(),
|
||||||
method.getSingleSourceType()
|
method.getSingleSourceParameter().getType()
|
||||||
),
|
),
|
||||||
method.getExecutable(),
|
method.getExecutable(),
|
||||||
mappedProperty.getMirror(),
|
mappedProperty.getMirror(),
|
||||||
@ -333,12 +329,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
sourceType,
|
sourceType,
|
||||||
targetType,
|
targetType,
|
||||||
dateFormat,
|
dateFormat,
|
||||||
method.getSourceParameters().get( 0 ).getName() + "."
|
method.getSingleSourceParameter().getName() + "."
|
||||||
+ getterMethod.getSimpleName().toString() + "()"
|
+ getterMethod.getSimpleName().toString() + "()"
|
||||||
);
|
);
|
||||||
|
|
||||||
PropertyMapping property = new PropertyMapping(
|
PropertyMapping property = new PropertyMapping(
|
||||||
method.getSourceParameters().get( 0 ).getName(),
|
method.getSingleSourceParameter().getName(),
|
||||||
method.getResultName(),
|
method.getResultName(),
|
||||||
executables.getPropertyName( getterMethod ),
|
executables.getPropertyName( getterMethod ),
|
||||||
getterMethod.getSimpleName().toString(),
|
getterMethod.getSimpleName().toString(),
|
||||||
@ -359,7 +355,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
}
|
}
|
||||||
|
|
||||||
private MappingMethod getIterableMappingMethod(List<Method> methods, Method method) {
|
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 );
|
Type targetElementType = method.getResultType().getTypeParameters().get( 0 );
|
||||||
|
|
||||||
TypeConversion conversion = getConversion(
|
TypeConversion conversion = getConversion(
|
||||||
@ -370,19 +366,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
);
|
);
|
||||||
|
|
||||||
return new IterableMappingMethod(
|
return new IterableMappingMethod(
|
||||||
method.getName(),
|
method,
|
||||||
method.getParameters(),
|
|
||||||
method.getSourceParameters(),
|
|
||||||
method.getResultType(),
|
|
||||||
method.getResultName(),
|
|
||||||
method.getReturnType(),
|
|
||||||
getMappingMethodReference( methods, sourceElementType, targetElementType ),
|
getMappingMethodReference( methods, sourceElementType, targetElementType ),
|
||||||
conversion
|
conversion
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MappingMethod getMapMappingMethod(List<Method> methods, Method method) {
|
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 sourceKeyType = sourceTypeParams.get( 0 );
|
||||||
Type sourceValueType = sourceTypeParams.get( 1 );
|
Type sourceValueType = sourceTypeParams.get( 1 );
|
||||||
|
|
||||||
@ -408,18 +399,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
|||||||
targetValueType
|
targetValueType
|
||||||
);
|
);
|
||||||
|
|
||||||
return new MapMappingMethod(
|
return new MapMappingMethod( method, keyMappingMethod, keyConversion, valueMappingMethod, valueConversion );
|
||||||
method.getName(),
|
|
||||||
method.getParameters(),
|
|
||||||
method.getSourceParameters(),
|
|
||||||
method.getResultType(),
|
|
||||||
method.getResultName(),
|
|
||||||
method.getReturnType(),
|
|
||||||
keyMappingMethod,
|
|
||||||
keyConversion,
|
|
||||||
valueMappingMethod,
|
|
||||||
valueConversion
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeConversion getConversion(Type sourceType, Type targetType, String dateFormat, String sourceReference) {
|
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,
|
private MappingMethodReference getMappingMethodReference(Iterable<Method> methods, Type parameterType,
|
||||||
Type returnType) {
|
Type returnType) {
|
||||||
for ( Method oneMethod : methods ) {
|
for ( Method oneMethod : methods ) {
|
||||||
Parameter singleSourceParam = oneMethod.getSourceParameters().get( 0 );
|
Parameter singleSourceParam = oneMethod.getSingleSourceParameter();
|
||||||
|
|
||||||
if ( singleSourceParam.getType().equals( parameterType ) &&
|
if ( singleSourceParam.getType().equals( parameterType ) &&
|
||||||
oneMethod.getReturnType().equals( returnType ) ) {
|
oneMethod.getResultType().equals( returnType ) ) {
|
||||||
return new MappingMethodReference(
|
return new MappingMethodReference( oneMethod );
|
||||||
oneMethod.getDeclaringMapper(),
|
|
||||||
oneMethod.getName()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,9 +133,6 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
Method.forMethodRequiringImplementation(
|
Method.forMethodRequiringImplementation(
|
||||||
method,
|
method,
|
||||||
parameters,
|
parameters,
|
||||||
sourceParameters,
|
|
||||||
resultType,
|
|
||||||
selectResultName( targetParameter, resultType ),
|
|
||||||
returnType,
|
returnType,
|
||||||
getMappings( method ),
|
getMappings( method ),
|
||||||
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ) ),
|
IterableMapping.fromPrism( IterableMappingPrism.getInstanceOn( method ) ),
|
||||||
@ -152,8 +149,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
|||||||
Method.forReferencedMethod(
|
Method.forReferencedMethod(
|
||||||
typeUtil.getType( typeUtils.getDeclaredType( element ) ),
|
typeUtil.getType( typeUtils.getDeclaredType( element ) ),
|
||||||
method,
|
method,
|
||||||
parameters.get( 0 ).getName(),
|
parameters,
|
||||||
parameters.get( 0 ).getType(),
|
|
||||||
returnType
|
returnType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
-->
|
-->
|
||||||
@Override
|
@Override
|
||||||
public ${returnType.name} ${name}(<#list parameters as param>${param.type.name} ${param.name}<#if param_has_next>, </#if></#list>) {
|
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>;
|
return<#if returnType.name != "void"> null</#if>;
|
||||||
}
|
}
|
||||||
<#if !existingInstanceMapping>
|
<#if !existingInstanceMapping>
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
-->
|
-->
|
||||||
@Override
|
@Override
|
||||||
public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
|
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>;
|
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 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>
|
</#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??>
|
<#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>
|
<#else>
|
||||||
<#if (conversion.exceptionTypes?size == 0) >
|
<#if (conversion.exceptionTypes?size == 0) >
|
||||||
${resultName}.add( <@includeModel object=conversion/> );
|
${resultName}.add( <@includeModel object=conversion/> );
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
-->
|
-->
|
||||||
@Override
|
@Override
|
||||||
public <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param.type/> ${param.name}<#if param_has_next>, </#if></#list>) {
|
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>;
|
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>();
|
<@includeModel object=resultType /> ${resultName} = new <#if resultType.mapImplementationType??><@includeModel object=resultType.mapImplementationType /><#else><@includeModel object=resultType /></#if>();
|
||||||
</#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 -->
|
<#-- key -->
|
||||||
<#if keyMappingMethod??>
|
<#if keyMappingMethod??>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user