diff --git a/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java b/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java index 303f52e02..4b642376d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java @@ -18,12 +18,12 @@ */ package org.mapstruct.ap.model; -import org.mapstruct.ap.model.source.BuiltInMethod; import java.util.HashSet; 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.source.BuiltInMethod; import org.mapstruct.ap.model.source.SourceMethod; /** @@ -34,19 +34,24 @@ import org.mapstruct.ap.model.source.SourceMethod; public class MethodReference extends MappingMethod { private final MapperReference declaringMapper; + + /** + * In case this reference targets a built-in method, allows to pass specific context information to the invoked + * method. Currently this is only used to pass in the configured date format string when invoking a built-in method + * which requires that. + */ private final String contextParam; - - public MethodReference(SourceMethod method, MapperReference declaringMapper ) { + public MethodReference(SourceMethod method, MapperReference declaringMapper) { super( method ); this.declaringMapper = declaringMapper; - this.contextParam = null; + this.contextParam = null; } - public MethodReference(BuiltInMethod method, ConversionContext contextParam ) { + public MethodReference(BuiltInMethod method, ConversionContext contextParam) { super( method ); this.declaringMapper = null; - this.contextParam = method.getContextParameter( contextParam ); + this.contextParam = method.getContextParameter( contextParam ); } public MapperReference getDeclaringMapper() { @@ -64,11 +69,4 @@ public class MethodReference extends MappingMethod { public String getContextParam() { return contextParam; } - - private String quoteParamWhenNotNull(String param) { - if (param != null ) { - return param != null ? "\"" + param + "\"" : "null"; - } - return null; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/ConversionContext.java b/processor/src/main/java/org/mapstruct/ap/model/common/ConversionContext.java index 5d919fb4e..434873959 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/ConversionContext.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/ConversionContext.java @@ -19,7 +19,7 @@ package org.mapstruct.ap.model.common; /** - * ConversionContext object passed to conversion providers. + * Context object passed to conversion providers and built-in methods. * * @author Gunnar Morling */ @@ -33,11 +33,11 @@ public interface ConversionContext { Type getTargetType(); /** - * Returns the date format if this conversion is from String to - * {@link Date} or vice versa. Returns {@code null} for other types or - * if not given. + * Returns the date format if this conversion or built-in method is from String to a date type (e.g. {@link Date}) + * or vice versa. * - * @return The date format if this conversion. + * @return The date format if this conversion or built-in method is from String to a date type. {@code null} is + * returned for other types or if not given. */ String getDateFormat(); diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/BuiltInMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/BuiltInMethod.java index 18c7ec2b1..e43feb52e 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/BuiltInMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/BuiltInMethod.java @@ -70,9 +70,9 @@ public abstract class BuiltInMethod extends ModelElement implements Method { * excluding generic type variables. When the implementor sees a need for this, this method can be overridden. */ @Override - public boolean matches( Type sourceType, Type targetType ) { + public boolean matches(Type sourceType, Type targetType) { if ( targetType.erasure().isAssignableTo( getReturnType().erasure() ) - && sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) { + && sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) { return doTypeVarsMatch( sourceType, targetType ); } return false; @@ -93,6 +93,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method { * * declaring mapper is always null, being the MapperImpl itself. This method should not be overridden by * implementors + * * @return null */ @Override @@ -110,6 +111,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method { /** * target parameter mechanism not supported for build-in-method + * * @return null */ @Override @@ -122,6 +124,7 @@ public abstract class BuiltInMethod extends ModelElement implements Method { * with context specific information such as a date format. * * @param conversionContext + * * @return null if no context parameter should be included * "null" if there should be an explicit null call * "'dateFormat'" for instance, to indicate how the build-in method should format the date @@ -144,10 +147,11 @@ public abstract class BuiltInMethod extends ModelElement implements Method { * equals based on class * * @param obj other class + * * @return true when classes are the same */ @Override - public boolean equals( Object obj ) { + public boolean equals(Object obj) { if ( obj == null ) { return false; } @@ -160,9 +164,10 @@ public abstract class BuiltInMethod extends ModelElement implements Method { * * @param parameter source * @param returnType target + * * @return */ - public boolean doTypeVarsMatch( Type parameter, Type returnType ) { + public boolean doTypeVarsMatch(Type parameter, Type returnType) { return true; } 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 6e5060eb1..d041627d0 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -578,18 +578,20 @@ public class MapperCreationProcessor implements ModelElementProcessor T getBestMatch(SourceMethod mappingMethod, String mappedElement, - Iterable methods, Type parameterType, - Type returnType) { + Iterable methods, Type parameterType, + Type returnType) { List candidatesWithMathingTargetType = new ArrayList(); for ( T method : methods ) { @@ -820,7 +835,8 @@ public class MapperCreationProcessor implements ModelElementProcessor int addToCandidateListIfMinimal(List candidatesWithBestMathingType, - int bestMatchingTypeDistance, T method, int currentTypeDistance) { + int bestMatchingTypeDistance, T method, + int currentTypeDistance) { if ( currentTypeDistance == bestMatchingTypeDistance ) { candidatesWithBestMathingType.add( method ); } @@ -833,7 +849,7 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences ) { + private MethodReference getMappingMethodReference(SourceMethod method, List mapperReferences) { if ( method != null ) { MapperReference mapperReference = null; for ( MapperReference ref : mapperReferences ) { @@ -847,7 +863,7 @@ public class MapperCreationProcessor implements ModelElementProcessor