#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.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
@ -40,7 +40,9 @@ public class BeanMappingMethod extends MappingMethod {
private final List<PropertyMapping> propertyMappings;
private final MethodReference factoryMethod;
public BeanMappingMethod(Method method, List<PropertyMapping> propertyMappings, MethodReference factoryMethod) {
public BeanMappingMethod(SourceMethod method,
List<PropertyMapping> propertyMappings,
MethodReference factoryMethod) {
super( method );
this.propertyMappings = propertyMappings;
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.Type;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.util.Strings;
/**
@ -37,7 +37,7 @@ public class IterableMappingMethod extends MappingMethod {
private final TypeConversion conversion;
private final MethodReference factoryMethod;
public IterableMappingMethod(Method method, MethodReference elementMappingMethod,
public IterableMappingMethod(SourceMethod method, MethodReference elementMappingMethod,
TypeConversion conversion, MethodReference factoryMethod) {
super( method );
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.Type;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.util.Strings;
/**
@ -39,7 +39,7 @@ public class MapMappingMethod extends MappingMethod {
private final TypeConversion valueConversion;
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 factoryMethod) {
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.Type;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.model.source.SourceMethod;
/**
* Represents a reference to {@link MappingMethod}.
@ -37,7 +37,7 @@ public class MethodReference extends MappingMethod {
private final String contextParam;
public MethodReference(Method method, MapperReference declaringMapper ) {
public MethodReference(SourceMethod method, MapperReference declaringMapper ) {
super( method );
this.declaringMapper = declaringMapper;
this.contextParam = null;

View File

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

View File

@ -42,7 +42,7 @@ import org.mapstruct.ap.util.Strings;
*
* @author Gunnar Morling
*/
public class Method implements BasicMethod {
public class SourceMethod implements BasicMethod {
private final Type declaringMapper;
private final ExecutableElement executable;
@ -58,12 +58,15 @@ public class Method implements BasicMethod {
private boolean configuredByReverseMappingMethod = false;
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters,
Type returnType, Map<String, List<Mapping>> mappings,
IterableMapping iterableMapping, MapMapping mapMapping,
Types typeUtils ) {
public static SourceMethod forMethodRequiringImplementation(ExecutableElement executable,
List<Parameter> parameters,
Type returnType, Map<String,
List<Mapping>> mappings,
IterableMapping iterableMapping,
MapMapping mapMapping,
Types typeUtils ) {
return new Method(
return new SourceMethod(
null,
executable,
parameters,
@ -74,10 +77,13 @@ public class Method implements BasicMethod {
typeUtils );
}
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable,
List<Parameter> parameters, Type returnType, Types typeUtils ) {
public static SourceMethod forReferencedMethod(Type declaringMapper,
ExecutableElement executable,
List<Parameter> parameters,
Type returnType,
Types typeUtils ) {
return new Method(
return new SourceMethod(
declaringMapper,
executable,
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) {
return new Method(
return new SourceMethod(
declaringMapper,
executable,
Collections.<Parameter>emptyList(),
@ -104,9 +110,14 @@ public class Method implements BasicMethod {
);
}
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType,
Map<String, List<Mapping>> mappings, IterableMapping iterableMapping, MapMapping mapMapping,
Types typeUtils ) {
private SourceMethod(Type declaringMapper,
ExecutableElement executable,
List<Parameter> parameters,
Type returnType,
Map<String, List<Mapping>> mappings,
IterableMapping iterableMapping,
MapMapping mapMapping,
Types typeUtils ) {
this.declaringMapper = declaringMapper;
this.executable = executable;
this.parameters = parameters;
@ -229,7 +240,7 @@ public class Method implements BasicMethod {
this.configuredByReverseMappingMethod = true;
}
public boolean reverses(Method method) {
public boolean reverses(SourceMethod method) {
return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
&& equals( getSourceParameters().iterator().next().getType(), method.getResultType() )
&& 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.source.BasicMethod;
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.ReportingPolicy;
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
* list of {@link Method}s.
* list of {@link SourceMethod}s.
*
* @author Gunnar Morling
*/
public class MapperCreationProcessor implements ModelElementProcessor<List<Method>, Mapper> {
public class MapperCreationProcessor implements ModelElementProcessor<List<SourceMethod>, Mapper> {
private Elements elementUtils;
private Types typeUtils;
@ -85,7 +85,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
private Set<BuiltInMethod> usedBuiltInMethods;
@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.typeUtils = context.getTypeUtils();
this.messager = context.getMessager();
@ -105,7 +105,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return 1000;
}
private Mapper getMapper(TypeElement element, List<Method> methods) {
private Mapper getMapper(TypeElement element, List<SourceMethod> methods) {
ReportingPolicy unmappedTargetPolicy = getEffectiveUnmappedTargetPolicy( element );
List<MapperReference> mapperReferences = getReferencedMappers( element );
List<MappingMethod> mappingMethods = getMappingMethods( mapperReferences, methods, unmappedTargetPolicy );
@ -170,17 +170,17 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return mapperReferences;
}
private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<Method> methods,
private List<MappingMethod> getMappingMethods(List<MapperReference> mapperReferences, List<SourceMethod> methods,
ReportingPolicy unmappedTargetPolicy) {
List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();
for ( Method method : methods ) {
for ( SourceMethod method : methods ) {
if ( !method.requiresImplementation() ) {
continue;
}
Method reverseMappingMethod = getReverseMappingMethod( methods, method );
SourceMethod reverseMappingMethod = getReverseMappingMethod( methods, method );
boolean hasFactoryMethod = false;
if ( method.isIterableMapping() ) {
@ -230,10 +230,10 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return mappingMethods;
}
private MethodReference getFactoryMethod(List<MapperReference> mapperReferences, List<Method> methods,
private MethodReference getFactoryMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
Type returnType) {
MethodReference result = null;
for ( Method method : methods ) {
for ( SourceMethod method : methods ) {
if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping()
&& method.getMappings().isEmpty() && method.getParameters().isEmpty() ) {
if ( method.getReturnType().equals( returnType ) ) {
@ -265,7 +265,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return result;
}
private void reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(Method method) {
private void reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(SourceMethod method) {
if ( method.getReturnType().getTypeMirror().getKind() != TypeKind.VOID &&
method.getReturnType().isInterface() &&
method.getReturnType().getImplementationType() == null ) {
@ -294,8 +294,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return reversed;
}
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences, List<Method> methods,
Method method, ExecutableElement targetAcessor, Parameter parameter) {
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences,
List<SourceMethod> methods,
SourceMethod method,
ExecutableElement targetAcessor,
Parameter parameter) {
String targetPropertyName = Executables.getPropertyName( targetAcessor );
Mapping mapping = method.getMapping( targetPropertyName );
String dateFormat = mapping != null ? mapping.getDateFormat() : null;
@ -341,8 +344,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return null;
}
private BeanMappingMethod getBeanMappingMethod(List<MapperReference> mapperReferences, List<Method> methods,
Method method, ReportingPolicy unmappedTargetPolicy) {
private BeanMappingMethod getBeanMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
SourceMethod method, ReportingPolicy unmappedTargetPolicy) {
List<PropertyMapping> propertyMappings = new ArrayList<PropertyMapping>();
Set<String> mappedTargetProperties = new HashSet<String>();
@ -413,7 +416,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return new BeanMappingMethod( method, propertyMappings, factoryMethod );
}
private void reportErrorForUnmappedTargetPropertiesIfRequired(Method method,
private void reportErrorForUnmappedTargetPropertiesIfRequired(SourceMethod method,
ReportingPolicy unmappedTargetPolicy,
Set<String> targetProperties,
Set<String> mappedTargetProperties) {
@ -433,8 +436,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
}
}
private Method getReverseMappingMethod(List<Method> rawMethods, Method method) {
for ( Method oneMethod : rawMethods ) {
private SourceMethod getReverseMappingMethod(List<SourceMethod> rawMethods, SourceMethod method) {
for ( SourceMethod oneMethod : rawMethods ) {
if ( oneMethod.reverses( method ) ) {
return oneMethod;
}
@ -442,7 +445,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return null;
}
private boolean hasSourceProperty(Method method, String propertyName) {
private boolean hasSourceProperty(SourceMethod method, String propertyName) {
for ( Parameter parameter : method.getSourceParameters() ) {
if ( hasSourceProperty( parameter, propertyName ) ) {
return true;
@ -461,7 +464,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
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
if ( method.isConfiguredByReverseMappingMethod() ) {
return true;
@ -551,9 +554,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return !foundUnmappedProperty;
}
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences, List<Method> methods,
Method method, Parameter parameter, ExecutableElement sourceAccessor,
ExecutableElement targetAcessor, String dateFormat) {
private PropertyMapping getPropertyMapping(List<MapperReference> mapperReferences,
List<SourceMethod> methods,
SourceMethod method,
Parameter parameter,
ExecutableElement sourceAccessor,
ExecutableElement targetAcessor,
String dateFormat) {
Type sourceType = typeFactory.getReturnType( sourceAccessor );
Type targetType = null;
String conversionString = null;
@ -607,8 +614,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return property;
}
private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences, List<Method> methods,
Method method) {
private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences,
List<SourceMethod> methods,
SourceMethod method) {
Type sourceElementType = method.getSourceParameters().iterator().next().getType().getTypeParameters().get( 0 );
Type targetElementType = method.getResultType().getTypeParameters().get( 0 );
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,
Method method) {
private MapMappingMethod getMapMappingMethod(List<MapperReference> mapperReferences, List<SourceMethod> methods,
SourceMethod method) {
List<Type> sourceTypeParams = method.getSourceParameters().iterator().next().getType().getTypeParameters();
Type sourceKeyType = sourceTypeParams.get( 0 );
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,
Type returnType) {
List<T> candidatesWithMathingTargetType = new ArrayList<T>();
@ -820,7 +828,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
return bestMatchingTypeDistance;
}
private MethodReference getMappingMethodReference( Method method, List<MapperReference> mapperReferences ) {
private MethodReference getMappingMethodReference( SourceMethod method, List<MapperReference> mapperReferences ) {
if ( method != null ) {
MapperReference mapperReference = null;
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 property The property mapping to check.
*/
private void reportErrorIfPropertyCanNotBeMapped(Method method, PropertyMapping property) {
private void reportErrorIfPropertyCanNotBeMapped(SourceMethod method, PropertyMapping property) {
boolean collectionOrMapTargetTypeHasCompatibleConstructor = false;
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.MapMapping;
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.MapMappingPrism;
import org.mapstruct.ap.prism.MapperPrism;
@ -49,21 +49,21 @@ import static javax.lang.model.util.ElementFilter.methodsIn;
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
* all referenced mapper methods declared by other mappers referenced by the
* current mapper.
*
* @author Gunnar Morling
*/
public class MethodRetrievalProcessor implements ModelElementProcessor<Void, List<Method>> {
public class MethodRetrievalProcessor implements ModelElementProcessor<Void, List<SourceMethod>> {
private Messager messager;
private TypeFactory typeFactory;
private Types typeUtils;
@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.typeFactory = context.getTypeFactory();
this.typeUtils = context.getTypeUtils();
@ -85,11 +85,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
*
* @return All mapping methods declared by the given type
*/
private List<Method> retrieveMethods(TypeElement element, boolean mapperRequiresImplementation) {
List<Method> methods = new ArrayList<Method>();
private List<SourceMethod> retrieveMethods(TypeElement element, boolean mapperRequiresImplementation) {
List<SourceMethod> methods = new ArrayList<SourceMethod>();
for ( ExecutableElement executable : methodsIn( element.getEnclosedElements() ) ) {
Method method = getMethod( element, executable, mapperRequiresImplementation );
SourceMethod method = getMethod( element, executable, mapperRequiresImplementation );
if ( method != null ) {
methods.add( method );
}
@ -117,7 +117,9 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
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 );
Type returnType = typeFactory.getReturnType( method );
@ -134,7 +136,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
if ( isValid ) {
return
Method.forMethodRequiringImplementation(
SourceMethod.forMethodRequiringImplementation(
method,
parameters,
returnType,
@ -151,7 +153,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
//otherwise add reference to existing mapper method
else if ( parameters.size() == 1 ) {
return
Method.forReferencedMethod(
SourceMethod.forReferencedMethod(
mapperRequiresImplementation ? null : typeFactory.getType( element ),
method,
parameters,
@ -162,7 +164,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
//create factory method
else if ( parameters.isEmpty() ) {
return
Method.forFactoryMethod(
SourceMethod.forFactoryMethod(
mapperRequiresImplementation ? null : typeFactory.getType( element ),
method,
returnType,