#120 Renaming Method to SourceMethod

This commit is contained in:
sjaakd 2014-02-18 21:38:57 +01:00 committed by Gunnar Morling
parent bab8c2bb9b
commit 06b4220af8
8 changed files with 95 additions and 72 deletions

View File

@ -26,7 +26,7 @@ import java.util.Set;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
/** /**
* A {@link MappingMethod} implemented by a {@link Mapper} class which maps one * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one
@ -40,7 +40,9 @@ public class BeanMappingMethod extends MappingMethod {
private final List<PropertyMapping> propertyMappings; private final List<PropertyMapping> propertyMappings;
private final MethodReference factoryMethod; private final MethodReference factoryMethod;
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings, MethodReference factoryMethod) { public BeanMappingMethod(SourceMethod method,
List<PropertyMapping> propertyMappings,
MethodReference factoryMethod) {
super( method ); super( method );
this.propertyMappings = propertyMappings; this.propertyMappings = propertyMappings;
this.factoryMethod = factoryMethod; this.factoryMethod = factoryMethod;

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.util.Strings;
/** /**
@ -37,7 +37,7 @@ public class IterableMappingMethod extends MappingMethod {
private final TypeConversion conversion; private final TypeConversion conversion;
private final MethodReference factoryMethod; private final MethodReference factoryMethod;
public IterableMappingMethod(Method method, MethodReference elementMappingMethod, public IterableMappingMethod(SourceMethod method, MethodReference elementMappingMethod,
TypeConversion conversion, MethodReference factoryMethod) { TypeConversion conversion, MethodReference factoryMethod) {
super( method ); super( method );
this.elementMappingMethod = elementMappingMethod; this.elementMappingMethod = elementMappingMethod;

View File

@ -22,7 +22,7 @@ import java.util.Set;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.util.Strings;
/** /**
@ -39,7 +39,7 @@ public class MapMappingMethod extends MappingMethod {
private final TypeConversion valueConversion; private final TypeConversion valueConversion;
private final MethodReference factoryMethod; private final MethodReference factoryMethod;
public MapMappingMethod(Method method, MethodReference keyMappingMethod, TypeConversion keyConversion, public MapMappingMethod(SourceMethod method, MethodReference keyMappingMethod, TypeConversion keyConversion,
MethodReference valueMappingMethod, TypeConversion valueConversion, MethodReference valueMappingMethod, TypeConversion valueConversion,
MethodReference factoryMethod) { MethodReference factoryMethod) {
super( method ); super( method );

View File

@ -24,7 +24,7 @@ import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
/** /**
* Represents a reference to {@link MappingMethod}. * Represents a reference to {@link MappingMethod}.
@ -37,7 +37,7 @@ public class MethodReference extends MappingMethod {
private final String contextParam; private final String contextParam;
public MethodReference(Method method, MapperReference declaringMapper ) { public MethodReference(SourceMethod method, MapperReference declaringMapper ) {
super( method ); super( method );
this.declaringMapper = declaringMapper; this.declaringMapper = declaringMapper;
this.contextParam = null; this.contextParam = null;

View File

@ -37,14 +37,14 @@ import javax.lang.model.util.Types;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
/** /**
* MethodMatcher $8.4 of the JavaLanguage specification describes a method body as such: * SourceMethodMatcher $8.4 of the JavaLanguage specification describes a method body as such:
* *
* <pre> * <pre>
* MethodDeclaration: MethodHeader MethodBody SourceMethodDeclaration: SourceMethodHeader SourceMethodBody
* MethodHeader: MethodModifiers TypeParameters Result MethodDeclarator Throws SourceMethodHeader: SourceMethodModifiers TypeParameters Result SourceMethodDeclarator Throws
* MethodDeclarator: Identifier ( FormalParameterList ) SourceMethodDeclarator: Identifier ( FormalParameterList )
*
* example &lt;T extends String & Serializable&gt; T getResult(? extends T) throws Exception example &lt;T extends String & Serializable&gt; T getResult(? extends T) throws Exception
* \-------------------------------/ \-/ \---------/ * \-------------------------------/ \-/ \---------/
* TypeParameters Result ParameterList * TypeParameters Result ParameterList
* </pre> * </pre>
@ -62,7 +62,7 @@ import org.mapstruct.ap.model.common.Type;
public class MethodMatcher { public class MethodMatcher {
private final Method candidateMethod; private final SourceMethod candidateMethod;
private final Types typeUtils; private final Types typeUtils;
private final Map<TypeVariable, TypeMirror> genericTypesMap = new HashMap<TypeVariable, TypeMirror>(); private final Map<TypeVariable, TypeMirror> genericTypesMap = new HashMap<TypeVariable, TypeMirror>();
@ -72,7 +72,7 @@ public class MethodMatcher {
* @param typeUtils * @param typeUtils
* @param candidateMethod * @param candidateMethod
*/ */
MethodMatcher(Types typeUtils, Method candidateMethod) { MethodMatcher(Types typeUtils, SourceMethod candidateMethod) {
this.typeUtils = typeUtils; this.typeUtils = typeUtils;
this.candidateMethod = candidateMethod; this.candidateMethod = candidateMethod;
} }

View File

@ -42,7 +42,7 @@ import org.mapstruct.ap.util.Strings;
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
public class Method implements BasicMethod { public class SourceMethod implements BasicMethod {
private final Type declaringMapper; private final Type declaringMapper;
private final ExecutableElement executable; private final ExecutableElement executable;
@ -58,12 +58,15 @@ public class Method implements BasicMethod {
private boolean configuredByReverseMappingMethod = false; private boolean configuredByReverseMappingMethod = false;
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters, public static SourceMethod forMethodRequiringImplementation(ExecutableElement executable,
Type returnType, Map<String, List<Mapping>> mappings, List<Parameter> parameters,
IterableMapping iterableMapping, MapMapping mapMapping, Type returnType, Map<String,
Types typeUtils ) { List<Mapping>> mappings,
IterableMapping iterableMapping,
MapMapping mapMapping,
Types typeUtils ) {
return new Method( return new SourceMethod(
null, null,
executable, executable,
parameters, parameters,
@ -74,10 +77,13 @@ public class Method implements BasicMethod {
typeUtils ); typeUtils );
} }
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, public static SourceMethod forReferencedMethod(Type declaringMapper,
List<Parameter> parameters, Type returnType, Types typeUtils ) { ExecutableElement executable,
List<Parameter> parameters,
Type returnType,
Types typeUtils ) {
return new Method( return new SourceMethod(
declaringMapper, declaringMapper,
executable, executable,
parameters, parameters,
@ -89,10 +95,10 @@ public class Method implements BasicMethod {
); );
} }
public static Method forFactoryMethod(Type declaringMapper, ExecutableElement executable, public static SourceMethod forFactoryMethod(Type declaringMapper, ExecutableElement executable,
Type returnType, Types typeUtils) { Type returnType, Types typeUtils) {
return new Method( return new SourceMethod(
declaringMapper, declaringMapper,
executable, executable,
Collections.<Parameter>emptyList(), Collections.<Parameter>emptyList(),
@ -104,9 +110,14 @@ public class Method implements BasicMethod {
); );
} }
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType, private SourceMethod(Type declaringMapper,
Map<String, List<Mapping>> mappings, IterableMapping iterableMapping, MapMapping mapMapping, ExecutableElement executable,
Types typeUtils ) { List<Parameter> parameters,
Type returnType,
Map<String, List<Mapping>> mappings,
IterableMapping iterableMapping,
MapMapping mapMapping,
Types typeUtils ) {
this.declaringMapper = declaringMapper; this.declaringMapper = declaringMapper;
this.executable = executable; this.executable = executable;
this.parameters = parameters; this.parameters = parameters;
@ -229,7 +240,7 @@ public class Method implements BasicMethod {
this.configuredByReverseMappingMethod = true; this.configuredByReverseMappingMethod = true;
} }
public boolean reverses(Method method) { public boolean reverses(SourceMethod method) {
return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1 return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
&& equals( getSourceParameters().iterator().next().getType(), method.getResultType() ) && equals( getSourceParameters().iterator().next().getType(), method.getResultType() )
&& equals( getResultType(), method.getSourceParameters().iterator().next().getType() ); && equals( getResultType(), method.getSourceParameters().iterator().next().getType() );

View File

@ -58,7 +58,7 @@ import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.model.common.TypeFactory;
import org.mapstruct.ap.model.source.BasicMethod; import org.mapstruct.ap.model.source.BasicMethod;
import org.mapstruct.ap.model.source.Mapping; import org.mapstruct.ap.model.source.Mapping;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.option.Options; import org.mapstruct.ap.option.Options;
import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.option.ReportingPolicy;
import org.mapstruct.ap.prism.MapperPrism; import org.mapstruct.ap.prism.MapperPrism;
@ -68,11 +68,11 @@ import org.mapstruct.ap.util.Strings;
/** /**
* A {@link ModelElementProcessor} which creates a {@link Mapper} from the given * A {@link ModelElementProcessor} which creates a {@link Mapper} from the given
* list of {@link Method}s. * list of {@link SourceMethod}s.
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
public class MapperCreationProcessor implements ModelElementProcessor<List<Method>, Mapper> { public class MapperCreationProcessor implements ModelElementProcessor<List<SourceMethod>, Mapper> {
private Elements elementUtils; private Elements elementUtils;
private Types typeUtils; private Types typeUtils;
@ -85,7 +85,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
private Set<BuiltInMethod> usedBuiltInMethods; private Set<BuiltInMethod> usedBuiltInMethods;
@Override @Override
public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, List<Method> sourceModel) { public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, List<SourceMethod> sourceModel) {
this.elementUtils = context.getElementUtils(); this.elementUtils = context.getElementUtils();
this.typeUtils = context.getTypeUtils(); this.typeUtils = context.getTypeUtils();
this.messager = context.getMessager(); this.messager = context.getMessager();
@ -105,7 +105,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return 1000; return 1000;
} }
private Mapper getMapper(TypeElement element, List<Method> methods) { private Mapper getMapper(TypeElement element, List<SourceMethod> methods) {
ReportingPolicy unmappedTargetPolicy = getEffectiveUnmappedTargetPolicy( element ); ReportingPolicy unmappedTargetPolicy = getEffectiveUnmappedTargetPolicy( element );
List<MapperReference> mapperReferences = getReferencedMappers( element ); List<MapperReference> mapperReferences = getReferencedMappers( element );
List<MappingMethod> mappingMethods = getMappingMethods( mapperReferences, methods, unmappedTargetPolicy ); List<MappingMethod> mappingMethods = getMappingMethods( mapperReferences, methods, unmappedTargetPolicy );
@ -170,17 +170,17 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return mapperReferences; return mapperReferences;
} }
private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<Method> methods, private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<SourceMethod> methods,
ReportingPolicy unmappedTargetPolicy) { ReportingPolicy unmappedTargetPolicy) {
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>(); List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
for ( Method method : methods ) { for ( SourceMethod method : methods ) {
if ( !method.requiresImplementation() ) { if ( !method.requiresImplementation() ) {
continue; continue;
} }
Method reverseMappingMethod = getReverseMappingMethod( methods, method ); SourceMethod reverseMappingMethod = getReverseMappingMethod( methods, method );
boolean hasFactoryMethod = false; boolean hasFactoryMethod = false;
if ( method.isIterableMapping() ) { if ( method.isIterableMapping() ) {
@ -230,10 +230,10 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return mappingMethods; return mappingMethods;
} }
private MethodReference getFactoryMethod(List<MapperReference> mapperReferences, List<Method> methods, private MethodReference getFactoryMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
Type returnType) { Type returnType) {
MethodReference result = null; MethodReference result = null;
for ( Method method : methods ) { for ( SourceMethod method : methods ) {
if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping() if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping()
&& method.getMappings().isEmpty() && method.getParameters().isEmpty() ) { && method.getMappings().isEmpty() && method.getParameters().isEmpty() ) {
if ( method.getReturnType().equals( returnType ) ) { if ( method.getReturnType().equals( returnType ) ) {
@ -265,7 +265,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return result; return result;
} }
private void reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(Method method) { private void reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(SourceMethod method) {
if ( method.getReturnType().getTypeMirror().getKind() != TypeKind.VOID && if ( method.getReturnType().getTypeMirror().getKind() != TypeKind.VOID &&
method.getReturnType().isInterface() && method.getReturnType().isInterface() &&
method.getReturnType().getImplementationType() == null ) { method.getReturnType().getImplementationType() == null ) {
@ -294,8 +294,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return reversed; return reversed;
} }
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences, List<Method> methods, private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences,
Method method, ExecutableElement targetAcessor, Parameter parameter) { List<SourceMethod> methods,
SourceMethod method,
ExecutableElement targetAcessor,
Parameter parameter) {
String targetPropertyName = Executables.getPropertyName( targetAcessor ); String targetPropertyName = Executables.getPropertyName( targetAcessor );
Mapping mapping = method.getMapping( targetPropertyName ); Mapping mapping = method.getMapping( targetPropertyName );
String dateFormat = mapping != null ? mapping.getDateFormat() : null; String dateFormat = mapping != null ? mapping.getDateFormat() : null;
@ -341,8 +344,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return null; return null;
} }
private BeanMappingMethod getBeanMappingMethod(List<MapperReference> mapperReferences, List<Method> methods, private BeanMappingMethod getBeanMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
Method method, ReportingPolicy unmappedTargetPolicy) { SourceMethod method, ReportingPolicy unmappedTargetPolicy) {
List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>(); List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>();
Set<String> mappedTargetProperties = new HashSet<String>(); Set<String> mappedTargetProperties = new HashSet<String>();
@ -413,7 +416,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return new BeanMappingMethod( method, propertyMappings, factoryMethod ); return new BeanMappingMethod( method, propertyMappings, factoryMethod );
} }
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method, private void reportErrorForUnmappedTargetPropertiesIfRequired(SourceMethod method,
ReportingPolicy unmappedTargetPolicy, ReportingPolicy unmappedTargetPolicy,
Set<String> targetProperties, Set<String> targetProperties,
Set<String> mappedTargetProperties) { Set<String> mappedTargetProperties) {
@ -433,8 +436,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
} }
} }
private Method getReverseMappingMethod(List<Method> rawMethods, Method method) { private SourceMethod getReverseMappingMethod(List<SourceMethod> rawMethods, SourceMethod method) {
for ( Method oneMethod : rawMethods ) { for ( SourceMethod oneMethod : rawMethods ) {
if ( oneMethod.reverses( method ) ) { if ( oneMethod.reverses( method ) ) {
return oneMethod; return oneMethod;
} }
@ -442,7 +445,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return null; return null;
} }
private boolean hasSourceProperty(Method method, String propertyName) { private boolean hasSourceProperty(SourceMethod method, String propertyName) {
for ( Parameter parameter : method.getSourceParameters() ) { for ( Parameter parameter : method.getSourceParameters() ) {
if ( hasSourceProperty( parameter, propertyName ) ) { if ( hasSourceProperty( parameter, propertyName ) ) {
return true; return true;
@ -461,7 +464,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return Executables.getPropertyNames( getters ).contains( propertyName ); return Executables.getPropertyNames( getters ).contains( propertyName );
} }
private boolean reportErrorIfMappedPropertiesDontExist(Method method) { private boolean reportErrorIfMappedPropertiesDontExist(SourceMethod method) {
// only report errors if this method itself is configured // only report errors if this method itself is configured
if ( method.isConfiguredByReverseMappingMethod() ) { if ( method.isConfiguredByReverseMappingMethod() ) {
return true; return true;
@ -551,9 +554,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return !foundUnmappedProperty; return !foundUnmappedProperty;
} }
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences, List<Method> methods, private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences,
Method method, Parameter parameter, ExecutableElement sourceAccessor, List<SourceMethod> methods,
ExecutableElement targetAcessor, String dateFormat) { SourceMethod method,
Parameter parameter,
ExecutableElement sourceAccessor,
ExecutableElement targetAcessor,
String dateFormat) {
Type sourceType = typeFactory.getReturnType( sourceAccessor ); Type sourceType = typeFactory.getReturnType( sourceAccessor );
Type targetType = null; Type targetType = null;
String conversionString = null; String conversionString = null;
@ -607,8 +614,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return property; return property;
} }
private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences, List<Method> methods, private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences,
Method method) { List<SourceMethod> methods,
SourceMethod method) {
Type sourceElementType = method.getSourceParameters().iterator().next().getType().getTypeParameters().get( 0 ); Type sourceElementType = method.getSourceParameters().iterator().next().getType().getTypeParameters().get( 0 );
Type targetElementType = method.getResultType().getTypeParameters().get( 0 ); Type targetElementType = method.getResultType().getTypeParameters().get( 0 );
String dateFormat = method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null; String dateFormat = method.getIterableMapping() != null ? method.getIterableMapping().getDateFormat() : null;
@ -659,8 +667,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
); );
} }
private MapMappingMethod getMapMappingMethod(List<MapperReference> mapperReferences, List<Method> methods, private MapMappingMethod getMapMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
Method method) { SourceMethod method) {
List<Type> sourceTypeParams = method.getSourceParameters().iterator().next().getType().getTypeParameters(); List<Type> sourceTypeParams = method.getSourceParameters().iterator().next().getType().getTypeParameters();
Type sourceKeyType = sourceTypeParams.get( 0 ); Type sourceKeyType = sourceTypeParams.get( 0 );
Type sourceValueType = sourceTypeParams.get( 1 ); Type sourceValueType = sourceTypeParams.get( 1 );
@ -752,7 +760,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
private <T extends BasicMethod> T getBestMatch(Method mappingMethod, String mappedElement, private <T extends BasicMethod> T getBestMatch(SourceMethod mappingMethod, String mappedElement,
Iterable<T> methods, Type parameterType, Iterable<T> methods, Type parameterType,
Type returnType) { Type returnType) {
List<T> candidatesWithMathingTargetType = new ArrayList<T>(); List<T> candidatesWithMathingTargetType = new ArrayList<T>();
@ -820,7 +828,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return bestMatchingTypeDistance; return bestMatchingTypeDistance;
} }
private MethodReference getMappingMethodReference( Method method, List<MapperReference> mapperReferences ) { private MethodReference getMappingMethodReference( SourceMethod method, List<MapperReference> mapperReferences ) {
if ( method != null ) { if ( method != null ) {
MapperReference mapperReference = null; MapperReference mapperReference = null;
for ( MapperReference ref : mapperReferences ) { for ( MapperReference ref : mapperReferences ) {
@ -857,7 +865,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
* @param method The mapping method owning the property mapping. * @param method The mapping method owning the property mapping.
* @param property The property mapping to check. * @param property The property mapping to check.
*/ */
private void reportErrorIfPropertyCanNotBeMapped(Method method, PropertyMapping property) { private void reportErrorIfPropertyCanNotBeMapped(SourceMethod method, PropertyMapping property) {
boolean collectionOrMapTargetTypeHasCompatibleConstructor = false; boolean collectionOrMapTargetTypeHasCompatibleConstructor = false;
if ( property.getSourceType().isCollectionType() && property.getTargetType().isCollectionType() ) { if ( property.getSourceType().isCollectionType() && property.getTargetType().isCollectionType() ) {

View File

@ -37,7 +37,7 @@ import org.mapstruct.ap.model.common.TypeFactory;
import org.mapstruct.ap.model.source.IterableMapping; import org.mapstruct.ap.model.source.IterableMapping;
import org.mapstruct.ap.model.source.MapMapping; import org.mapstruct.ap.model.source.MapMapping;
import org.mapstruct.ap.model.source.Mapping; import org.mapstruct.ap.model.source.Mapping;
import org.mapstruct.ap.model.source.Method; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.prism.IterableMappingPrism; import org.mapstruct.ap.prism.IterableMappingPrism;
import org.mapstruct.ap.prism.MapMappingPrism; import org.mapstruct.ap.prism.MapMappingPrism;
import org.mapstruct.ap.prism.MapperPrism; import org.mapstruct.ap.prism.MapperPrism;
@ -49,21 +49,21 @@ import static javax.lang.model.util.ElementFilter.methodsIn;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
/** /**
* A {@link ModelElementProcessor} which retrieves a list of {@link Method}s * A {@link ModelElementProcessor} which retrieves a list of {@link SourceMethod}s
* representing all the mapping methods of the given bean mapper type as well as * representing all the mapping methods of the given bean mapper type as well as
* all referenced mapper methods declared by other mappers referenced by the * all referenced mapper methods declared by other mappers referenced by the
* current mapper. * current mapper.
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
public class MethodRetrievalProcessor implements ModelElementProcessor<Void, List<Method>> { public class MethodRetrievalProcessor implements ModelElementProcessor<Void, List<SourceMethod>> {
private Messager messager; private Messager messager;
private TypeFactory typeFactory; private TypeFactory typeFactory;
private Types typeUtils; private Types typeUtils;
@Override @Override
public List<Method> process(ProcessorContext context, TypeElement mapperTypeElement, Void sourceModel) { public List<SourceMethod> process(ProcessorContext context, TypeElement mapperTypeElement, Void sourceModel) {
this.messager = context.getMessager(); this.messager = context.getMessager();
this.typeFactory = context.getTypeFactory(); this.typeFactory = context.getTypeFactory();
this.typeUtils = context.getTypeUtils(); this.typeUtils = context.getTypeUtils();
@ -85,11 +85,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
* *
* @return All mapping methods declared by the given type * @return All mapping methods declared by the given type
*/ */
private List<Method> retrieveMethods(TypeElement element, boolean mapperRequiresImplementation) { private List<SourceMethod> retrieveMethods(TypeElement element, boolean mapperRequiresImplementation) {
List<Method> methods = new ArrayList<Method>(); List<SourceMethod> methods = new ArrayList<SourceMethod>();
for ( ExecutableElement executable : methodsIn( element.getEnclosedElements() ) ) { for ( ExecutableElement executable : methodsIn( element.getEnclosedElements() ) ) {
Method method = getMethod( element, executable, mapperRequiresImplementation ); SourceMethod method = getMethod( element, executable, mapperRequiresImplementation );
if ( method != null ) { if ( method != null ) {
methods.add( method ); methods.add( method );
} }
@ -117,7 +117,9 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return methods; return methods;
} }
private Method getMethod(TypeElement element, ExecutableElement method, boolean mapperRequiresImplementation) { private SourceMethod getMethod(TypeElement element,
ExecutableElement method,
boolean mapperRequiresImplementation) {
List<Parameter> parameters = typeFactory.getParameters( method ); List<Parameter> parameters = typeFactory.getParameters( method );
Type returnType = typeFactory.getReturnType( method ); Type returnType = typeFactory.getReturnType( method );
@ -134,7 +136,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
if ( isValid ) { if ( isValid ) {
return return
Method.forMethodRequiringImplementation( SourceMethod.forMethodRequiringImplementation(
method, method,
parameters, parameters,
returnType, returnType,
@ -151,7 +153,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
//otherwise add reference to existing mapper method //otherwise add reference to existing mapper method
else if ( parameters.size() == 1 ) { else if ( parameters.size() == 1 ) {
return return
Method.forReferencedMethod( SourceMethod.forReferencedMethod(
mapperRequiresImplementation ? null : typeFactory.getType( element ), mapperRequiresImplementation ? null : typeFactory.getType( element ),
method, method,
parameters, parameters,
@ -162,7 +164,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
//create factory method //create factory method
else if ( parameters.isEmpty() ) { else if ( parameters.isEmpty() ) {
return return
Method.forFactoryMethod( SourceMethod.forFactoryMethod(
mapperRequiresImplementation ? null : typeFactory.getType( element ), mapperRequiresImplementation ? null : typeFactory.getType( element ),
method, method,
returnType, returnType,