diff --git a/processor/src/main/java/org/mapstruct/ap/model/ParameterAssignment.java b/processor/src/main/java/org/mapstruct/ap/model/ParameterAssignment.java new file mode 100644 index 000000000..2d2eccdd4 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/model/ParameterAssignment.java @@ -0,0 +1,58 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.model; + +/** + * This class carries the possible ways to do an assignment to a parameter on a mapping target + * + * The following options exist: + *
    + *
  1. MethodReference
  2. + *
  3. TypeConversion
  4. + *
  5. Simple Assignment (empty ParameterAssignment)
  6. + *
+ * + * @author Sjaak Derksen + */ +public class ParameterAssignment { + + + private MethodReference methodReference; + private TypeConversion typeConversion; + + public ParameterAssignment() { + } + + public ParameterAssignment( MethodReference methodReference ) { + this.methodReference = methodReference; + } + + public ParameterAssignment( TypeConversion typeConversion ) { + this.typeConversion = typeConversion; + } + + public MethodReference getMethodReference() { + return methodReference; + } + + public TypeConversion getTypeConversion() { + return typeConversion; + } + +} 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 1d163b0fc..3e7729ec5 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.processor; +import org.mapstruct.ap.model.ParameterAssignment; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; @@ -48,7 +49,6 @@ import org.mapstruct.ap.model.MapperReference; import org.mapstruct.ap.model.MappingMethod; import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.PropertyMapping; -import org.mapstruct.ap.model.TypeConversion; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -79,7 +79,7 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences = getReferencedMappers( element ); List mappingMethods = getMappingMethods( mapperReferences, methods, unmappedTargetPolicy ); - mappingMethods.addAll( mappingMethodResolver.getVirtualMethodsToCreate() ); + mappingMethods.addAll( mappingResolver.getVirtualMethodsToGenerate() ); Mapper mapper = new Mapper.Builder() .element( element ) @@ -641,7 +641,8 @@ public class MapperCreationProcessor implements ModelElementProcessor virtualMethods; - public MappingMethodResolver(Messager messager, TypeFactory typeFactory, Elements elementUtils, Types typeUtils) { + + public MappingResolver(Messager messager, TypeFactory typeFactory, Elements elementUtils, Types typeUtils) { this.messager = messager; this.typeFactory = typeFactory; this.conversions = new Conversions( elementUtils, typeFactory ); @@ -88,7 +90,82 @@ public class MappingMethodResolver { } - public TypeConversion getConversion(Type sourceType, Type targetType, String dateFormat, String sourceReference) { + /** + * returns a parameter assignment + * + * @param mappingMethod target mapping method + * @param mappedElement used for error messages + * @param mapperReferences list of references to mapper + * @param methods list of candidate methods + * @param sourceType parameter to match + * @param targetType return type to match + * @param targetPropertyName name of the target property + * @param dateFormat used for formatting dates in build in methods that need context information + * @param sourceReference call to source type as string + * + * @return an assignment to a method parameter, which can either be: + *
    + *
  1. MethodReference
  2. + *
  3. TypeConversion
  4. + *
  5. Simple Assignment (empty ParameterAssignment)
  6. + *
  7. null, no assignment found
  8. + *
+ */ + public ParameterAssignment getParameterAssignment( SourceMethod mappingMethod, + String mappedElement, + List mapperReferences, + List methods, + Type sourceType, + Type targetType, + String targetPropertyName, + String dateFormat, + String sourceReference ) { + + MethodReference mappingMethodReference = getMappingMethodReferenceBasedOnMethod( + mappingMethod, + mappedElement, + mapperReferences, + methods, + sourceType, + targetType, + targetPropertyName, + dateFormat + ); + + ParameterAssignment parameterAssignment = null; + + if (mappingMethodReference != null ) { + parameterAssignment = new ParameterAssignment(mappingMethodReference ); + } + else if (sourceType.isAssignableTo( targetType ) ) { + parameterAssignment = new ParameterAssignment(); + } + else { + TypeConversion conversion = getConversion( sourceType, targetType, dateFormat, sourceReference ); + if ( conversion != null ) { + parameterAssignment = new ParameterAssignment(conversion ); + } + else { + mappingMethodReference = getMappingMethodReferenceBasedOnParameter( + mappingMethod, + mappedElement, + mapperReferences, + methods, + sourceType, + targetType, + targetPropertyName, + dateFormat + ); + if ( mappingMethodReference != null ) { + parameterAssignment = new ParameterAssignment( mappingMethodReference ); + } + } + } + return parameterAssignment; + } + + + private TypeConversion getConversion(Type sourceType, Type targetType, String dateFormat, String sourceReference) { ConversionProvider conversionProvider = conversions.getConversion( sourceType, targetType ); if ( conversionProvider == null ) { @@ -115,7 +192,7 @@ public class MappingMethodResolver { * * @return a method reference. */ - public MethodReference getMappingMethodReferenceBasedOnMethod(SourceMethod mappingMethod, + private MethodReference getMappingMethodReferenceBasedOnMethod(SourceMethod mappingMethod, String mappedElement, List mapperReferences, List methods, @@ -172,7 +249,7 @@ public class MappingMethodResolver { * * @return a method reference. */ - public MethodReference getMappingMethodReferenceBasedOnParameter(SourceMethod mappingMethod, + private MethodReference getMappingMethodReferenceBasedOnParameter(SourceMethod mappingMethod, String mappedElement, List mapperReferences, List methods, @@ -267,7 +344,7 @@ public class MappingMethodResolver { return null; } - public Set getVirtualMethodsToCreate() { + public Set getVirtualMethodsToGenerate() { return virtualMethods; }