#136 Some clean-up/documentation

This commit is contained in:
Andreas Gudian 2014-02-28 21:50:27 +01:00
parent 4372e726ee
commit 1db853137c
2 changed files with 20 additions and 25 deletions

View File

@ -18,12 +18,12 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
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.model.source.SourceMethod;
import org.mapstruct.ap.model.source.builtin.BuiltInMethod; import org.mapstruct.ap.model.source.builtin.BuiltInMethod;
@ -35,7 +35,7 @@ import org.mapstruct.ap.model.source.builtin.BuiltInMethod;
public class MethodReference extends MappingMethod { public class MethodReference extends MappingMethod {
private final MapperReference declaringMapper; private final MapperReference declaringMapper;
private final Type importType; private final Set<Type> importTypes;
/** /**
* A reference to another mapping method in case this is a two-step mapping, e.g. from {@code JAXBElement<Bar>} to * A reference to another mapping method in case this is a two-step mapping, e.g. from {@code JAXBElement<Bar>} to
@ -51,25 +51,27 @@ public class MethodReference extends MappingMethod {
*/ */
private final String contextParam; private final String contextParam;
public MethodReference(SourceMethod method, MapperReference declaringMapper) { /**
* Creates a new reference to the given method.
* @param method the target method of the reference
* @param declaringMapper the method declaring the mapper; {@code null} if the current mapper itself
* @param targetType in case the referenced method has a parameter for passing the target type, the given
* target type, otherwise {@code null}
*/
public MethodReference(SourceMethod method, MapperReference declaringMapper, Type targetType) {
super( method ); super( method );
this.declaringMapper = declaringMapper; this.declaringMapper = declaringMapper;
this.contextParam = null; this.contextParam = null;
this.importType = null; this.importTypes = targetType == null ?
Collections.<Type>emptySet() :
Collections.<Type>singleton( targetType );
} }
public MethodReference(BuiltInMethod method, ConversionContext contextParam) { public MethodReference(BuiltInMethod method, ConversionContext contextParam) {
super( method ); super( method );
this.declaringMapper = null; this.declaringMapper = null;
this.contextParam = method.getContextParameter( contextParam ); this.contextParam = method.getContextParameter( contextParam );
this.importType = null; this.importTypes = Collections.emptySet();
}
public MethodReference(Method method, MapperReference declaringMapper, Type importType) {
super( method );
this.declaringMapper = declaringMapper;
this.contextParam = null;
this.importType = importType;
} }
public MapperReference getDeclaringMapper() { public MapperReference getDeclaringMapper() {
@ -107,9 +109,7 @@ public class MethodReference extends MappingMethod {
@Override @Override
public Set<Type> getImportTypes() { public Set<Type> getImportTypes() {
Set<Type> imported = super.getImportTypes(); Set<Type> imported = super.getImportTypes();
if ( importType != null ) { imported.addAll( importTypes );
imported.add( importType );
}
if ( methodRefChild != null ) { if ( methodRefChild != null ) {
imported.addAll( methodRefChild.getImportTypes() ); imported.addAll( methodRefChild.getImportTypes() );
} }

View File

@ -259,19 +259,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping() if ( !method.requiresImplementation() && !method.isIterableMapping() && !method.isMapMapping()
&& method.getSourceParameters().size() == 0 ) { && method.getSourceParameters().size() == 0 ) {
List<Type> paramterTypes = List<Type> parameterTypes =
MethodSelectors.getParameterTypes( typeFactory, method.getParameters(), null, returnType ); MethodSelectors.getParameterTypes( typeFactory, method.getParameters(), null, returnType );
if ( method.matches( paramterTypes, returnType ) ) { if ( method.matches( parameterTypes, returnType ) ) {
if ( result == null ) { if ( result == null ) {
MapperReference mapperReference = findMapperReference( mapperReferences, method ); MapperReference mapperReference = findMapperReference( mapperReferences, method );
result = new MethodReference( method, mapperReference, null );
result =
new MethodReference(
method,
mapperReference,
SourceMethod.containsTargetTypeParameter( method.getParameters() )
? returnType : null );
} }
else { else {
messager.printMessage( messager.printMessage(
@ -1155,7 +1149,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
return new MethodReference( return new MethodReference(
method, method,
mapperReference, mapperReference,
SourceMethod.containsTargetTypeParameter( method.getParameters() ) ? targetType : null ); SourceMethod.containsTargetTypeParameter( method.getParameters() ) ? targetType : null
);
} }
private MapperReference findMapperReference(List<MapperReference> mapperReferences, SourceMethod method) { private MapperReference findMapperReference(List<MapperReference> mapperReferences, SourceMethod method) {