From 9b8e22462f2d61ee65ea6ab1aec46a1537db5dfb Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sat, 22 Feb 2014 22:45:25 +0100 Subject: [PATCH] #120 Consolidating method reference look-up in MapperCreationProcessor --- .../ap/processor/MapperCreationProcessor.java | 185 +++++++++--------- 1 file changed, 90 insertions(+), 95 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index 9d26b0021..2dbe851a8 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -578,22 +578,17 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences, List methods, SourceMethod method) { List sourceTypeParams = method.getSourceParameters().iterator().next().getType().getTypeParameters(); - Type sourceKeyType = sourceTypeParams.get( 0 ); - Type sourceValueType = sourceTypeParams.get( 1 ); - List resultTypeParams = method.getResultType().getTypeParameters(); - Type targetKeyType = resultTypeParams.get( 0 ); - Type targetValueType = resultTypeParams.get( 1 ); + // find mapping method or conversion for key + Type keySourceType = sourceTypeParams.get( 0 ); + Type keyTargetType = resultTypeParams.get( 0 ); String keyDateFormat = method.getMapMapping() != null ? method.getMapMapping().getKeyFormat() : null; - String valueDateFormat = method.getMapMapping() != null ? method.getMapMapping().getValueFormat() : null; - TypeConversion keyConversion = getConversion( sourceKeyType, targetKeyType, keyDateFormat, "entry.getKey()" ); - TypeConversion valueConversion = getConversion( - sourceValueType, - targetValueType, - valueDateFormat, - "entry.getValue()" - ); - - // first try the source methods MethodReference keyMappingMethod = getMappingMethodReference( - getBestMatch( method, "map key", methods, sourceKeyType, targetKeyType ), mapperReferences + method, + "map key", + mapperReferences, + methods, + keySourceType, + keyTargetType, + keyDateFormat ); + TypeConversion keyConversion = getConversion( keySourceType, keyTargetType, keyDateFormat, "entry.getKey()" ); - // then try built-in methods - if ( keyMappingMethod == null ) { - keyMappingMethod = getMappingMethodReference( - getBestMatch( method, "map key", builtInMethods.getBuiltInMethods(), sourceKeyType, targetKeyType ), - targetKeyType, - keyDateFormat - ); - } - - // first try the source methods - MethodReference valueMappingMethod = getMappingMethodReference( - getBestMatch( method, "map value", methods, sourceValueType, targetValueType ), - mapperReferences - ); - - // then try built-in methods - if ( valueMappingMethod == null ) { - valueMappingMethod = getMappingMethodReference( - getBestMatch( - method, - "map value", - builtInMethods.getBuiltInMethods(), - sourceValueType, - targetValueType - ), - targetValueType, - valueDateFormat - ); - } - - if ( !sourceKeyType.isAssignableTo( targetKeyType ) && keyConversion == null && keyMappingMethod == null ) { + if ( !keySourceType.isAssignableTo( keyTargetType ) && keyConversion == null && keyMappingMethod == null ) { messager.printMessage( Kind.ERROR, String.format( @@ -746,7 +698,28 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences, + List methods, Type sourceType, Type targetType, + String dateFormat) { + // first try to find a matching source method + SourceMethod matchingSourceMethod = getBestMatch( method, mappedElement, methods, sourceType, targetType ); + + if ( matchingSourceMethod != null ) { + return getMappingMethodReference( matchingSourceMethod, mapperReferences ); + } + + // then a matching built-in method + BuiltInMethod matchingBuiltInMethod = getBestMatch( + method, + mappedElement, + builtInMethods.getBuiltInMethods(), + sourceType, + targetType + ); + + return matchingBuiltInMethod != null ? + getMappingMethodReference( matchingBuiltInMethod, targetType, dateFormat ) : null; + } private T getBestMatch(SourceMethod mappingMethod, String mappedElement, Iterable methods, Type parameterType, @@ -850,26 +850,20 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences) { - if ( method != null ) { - MapperReference mapperReference = null; - for ( MapperReference ref : mapperReferences ) { - if ( ref.getMapperType().equals( method.getDeclaringMapper() ) ) { - mapperReference = ref; - break; - } + MapperReference mapperReference = null; + for ( MapperReference ref : mapperReferences ) { + if ( ref.getMapperType().equals( method.getDeclaringMapper() ) ) { + mapperReference = ref; + break; } - return new MethodReference( method, mapperReference ); } - return null; + return new MethodReference( method, mapperReference ); } private MethodReference getMappingMethodReference(BuiltInMethod method, Type returnType, String dateFormat) { - if ( method != null ) { - virtualMethods.add( new VirtualMappingMethod( method ) ); - ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat ); - return new MethodReference( method, ctx ); - } - return null; + virtualMethods.add( new VirtualMappingMethod( method ) ); + ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat ); + return new MethodReference( method, ctx ); } /** @@ -878,6 +872,7 @@ public class MapperCreationProcessor implements ModelElementProcessor *
  • the source type is assignable to the target type
  • *
  • a mapping method exists
  • + *
  • a built-in method exists/
  • *
  • a built-in conversion exists
  • *
  • the property is of a collection or map type and the constructor of the target type (either itself or its * implementation type) accepts the source type.