diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/BuiltInMappingMethods.java b/processor/src/main/java/org/mapstruct/ap/builtin/BuiltInMappingMethods.java index 01df8b10d..cb60a829e 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/BuiltInMappingMethods.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/BuiltInMappingMethods.java @@ -18,103 +18,27 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import java.util.ArrayList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.annotation.processing.Messager; -import javax.tools.Diagnostic; -import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; -import org.mapstruct.ap.util.Strings; + /** + * Registry for all build in methods. * * @author Sjaak Derksen */ -public class BuiltInMappingMethods { +public class BuiltInMappingMethods extends HashSet { - private final Set builtInMethods = new HashSet(); - private final Messager messager; + public BuiltInMappingMethods( TypeFactory typeFactory ) { - public BuiltInMappingMethods( Messager messager, TypeFactory typeFactory ) { - this.messager = messager; - - builtInMethods.add( new JaxbElemToValue( typeFactory ) ); - builtInMethods.add( new ListOfJaxbElemToListOfValue( typeFactory ) ); - builtInMethods.add( new DateToXmlGregorianCalendar( typeFactory ) ); - builtInMethods.add( new XmlGregorianCalendarToDate( typeFactory ) ); - builtInMethods.add( new StringToXmlGregorianCalendar( typeFactory ) ); - builtInMethods.add( new XmlGregorianCalendarToString( typeFactory ) ); - builtInMethods.add( new CalendarToXmlGregorianCalendar( typeFactory ) ); - builtInMethods.add( new XmlGregorianCalendarToCalendar( typeFactory ) ); - } - - /** - * The method looks for a match on equal source type and best matching target type (minimum distance) TODO: - * investigate whether also the best matching source type should be investigating iso equal. - * - * @param sourceType - * @param targetType - * @return - */ - public BuiltInMappingMethod getConversion( Type sourceType, Type targetType ) { - - List candidates = new ArrayList(); - int bestMatchingTargetTypeDistance = Integer.MAX_VALUE; - for ( BuiltInMappingMethod entry : builtInMethods ) { - - if ( targetType.erasure().isAssignableTo( entry.target() ) - && sourceType.erasure().isAssignableTo( entry.source() ) ) { - - if ( entry.doGenericsMatch( sourceType, targetType ) ) { - int sourceTypeDistance = targetType.distanceTo( entry.target() ); - bestMatchingTargetTypeDistance - = addToCandidateListIfMinimal( - candidates, - bestMatchingTargetTypeDistance, - entry, - sourceTypeDistance ); - } - } - } - - if ( candidates.isEmpty() ) { - return null; - } - - if ( candidates.size() > 1 ) { - // print a warning if we find more than one method with minimum source type distance - List builtInMethodNames = new ArrayList(); - for ( BuiltInMappingMethod candidate : candidates ) { - builtInMethodNames.add( candidate.getName() ); - } - - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( - "MapStruct error. Conflicting build-in methods %s for sourceType: %s, targetTypes %s.", - Strings.join( builtInMethodNames, ", " ), - sourceType, - targetType ) - ); - } - - return candidates.get( 0 ); - } - - private int addToCandidateListIfMinimal( List candidatesWithBestMathingType, - int bestMatchingTypeDistance, BuiltInMappingMethod builtInMethod, int currentTypeDistance ) { - if ( currentTypeDistance == bestMatchingTypeDistance ) { - candidatesWithBestMathingType.add( builtInMethod ); - } - else if ( currentTypeDistance < bestMatchingTypeDistance ) { - bestMatchingTypeDistance = currentTypeDistance; - - candidatesWithBestMathingType.clear(); - candidatesWithBestMathingType.add( builtInMethod ); - } - return bestMatchingTypeDistance; + add( new JaxbElemToValue( typeFactory ) ); + add( new ListOfJaxbElemToListOfValue( typeFactory ) ); + add( new DateToXmlGregorianCalendar( typeFactory ) ); + add( new XmlGregorianCalendarToDate( typeFactory ) ); + add( new StringToXmlGregorianCalendar( typeFactory ) ); + add( new XmlGregorianCalendarToString( typeFactory ) ); + add( new CalendarToXmlGregorianCalendar( typeFactory ) ); + add( new XmlGregorianCalendarToCalendar( typeFactory ) ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/CalendarToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/builtin/CalendarToXmlGregorianCalendar.java index e86e7eeda..9c5e4a150 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/CalendarToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/CalendarToXmlGregorianCalendar.java @@ -18,15 +18,13 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Set; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -36,25 +34,17 @@ import static org.mapstruct.ap.util.Collections.asSet; * * @author Sjaak Derksen */ -public class CalendarToXmlGregorianCalendar extends BuiltInMappingMethod { +public class CalendarToXmlGregorianCalendar extends BuiltInMethod { - private static final Class SOURCE = Calendar.class; - private static final Class TARGET = XMLGregorianCalendar.class; + private final Parameter parameter; + private final Type returnType; private final TypeFactory typeFactory; public CalendarToXmlGregorianCalendar( TypeFactory typeFactory ) { this.typeFactory = typeFactory; - } - - @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "cal", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); + this.parameter = typeFactory.createParameter( "cal ", Calendar.class ); + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); } @Override @@ -65,12 +55,12 @@ public class CalendarToXmlGregorianCalendar extends BuiltInMappingMethod { } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Parameter getParameter() { + return parameter; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/DateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/builtin/DateToXmlGregorianCalendar.java index bc51293ee..e4151a576 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/DateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/DateToXmlGregorianCalendar.java @@ -18,15 +18,13 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.Date; import java.util.GregorianCalendar; import java.util.Set; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -36,25 +34,17 @@ import static org.mapstruct.ap.util.Collections.asSet; * * @author Sjaak Derksen */ -public class DateToXmlGregorianCalendar extends BuiltInMappingMethod { +public class DateToXmlGregorianCalendar extends BuiltInMethod { - private static final Class SOURCE = Date.class; - private static final Class TARGET = XMLGregorianCalendar.class; + private final Parameter parameter; + private final Type returnType; private final TypeFactory typeFactory; public DateToXmlGregorianCalendar( TypeFactory typeFactory ) { this.typeFactory = typeFactory; - } - - @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "date", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); + this.parameter = typeFactory.createParameter( "date", Date.class ); + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); } @Override @@ -65,12 +55,12 @@ public class DateToXmlGregorianCalendar extends BuiltInMappingMethod { } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Parameter getParameter() { + return parameter; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/JaxbElemToValue.java b/processor/src/main/java/org/mapstruct/ap/builtin/JaxbElemToValue.java index b9131e8e6..2b86586ce 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/JaxbElemToValue.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/JaxbElemToValue.java @@ -18,10 +18,8 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import javax.xml.bind.JAXBElement; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -30,29 +28,18 @@ import org.mapstruct.ap.model.common.TypeFactory; * * @author Sjaak Derksen */ -public class JaxbElemToValue extends BuiltInMappingMethod { +public class JaxbElemToValue extends BuiltInMethod { - private static final Class SOURCE = JAXBElement.class; - private static final Class TARGET = Object.class; - - private final TypeFactory typeFactory; + private final Parameter parameter; + private final Type returnType; public JaxbElemToValue( TypeFactory typeFactory ) { - this.typeFactory = typeFactory; + this.parameter = typeFactory.createParameter( "element", JAXBElement.class ); + this.returnType = typeFactory.getType( Object.class ); } @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "element", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); - } - - @Override - public boolean doGenericsMatch(Type sourceType, Type targetType) { + public boolean doTypeVarsMatch(Type sourceType, Type targetType) { boolean match = false; if (sourceType.getTypeParameters().size() == 1) { match = sourceType.getTypeParameters().get( 0 ).equals( targetType ); @@ -61,12 +48,12 @@ public class JaxbElemToValue extends BuiltInMappingMethod { } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Parameter getParameter() { + return parameter; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/ListOfJaxbElemToListOfValue.java b/processor/src/main/java/org/mapstruct/ap/builtin/ListOfJaxbElemToListOfValue.java index 562abb7e5..eb20bf722 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/ListOfJaxbElemToListOfValue.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/ListOfJaxbElemToListOfValue.java @@ -18,11 +18,9 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.List; import javax.xml.bind.JAXBElement; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -31,36 +29,24 @@ import org.mapstruct.ap.model.common.TypeFactory; * * @author Sjaak Derksen */ -public class ListOfJaxbElemToListOfValue extends BuiltInMappingMethod { +public class ListOfJaxbElemToListOfValue extends BuiltInMethod { - private static final Class SOURCE = List.class; - private static final Class TARGET = List.class; - private static final Class TARGET_PARAM = JAXBElement.class; - - private final TypeFactory typeFactory; + private final Parameter parameter; + private final Type returnType; + private final Type genericParam; public ListOfJaxbElemToListOfValue( TypeFactory typeFactory ) { - this.typeFactory = typeFactory; + this.parameter = typeFactory.createParameter( "elementList", List.class ); + this.returnType = typeFactory.getType( List.class ); + this.genericParam = typeFactory.getType( JAXBElement.class ).erasure(); } @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "elementList", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); - } - - @Override - public boolean doGenericsMatch(Type sourceType, Type targetType) { + public boolean doTypeVarsMatch(Type sourceType, Type targetType) { boolean match = false; - if ( ( sourceType.getTypeParameters().size() == 1 ) - && ( targetType.getTypeParameters().size() == 1 ) ) { + if ( ( sourceType.getTypeParameters().size() == 1 ) && ( targetType.getTypeParameters().size() == 1 ) ) { Type typeParam = sourceType.getTypeParameters().get( 0 ); - if ( typeParam.erasure().equals( typeFactory.getType( TARGET_PARAM ).erasure() ) && - ( typeParam.getTypeParameters().size() == 1 ) ) { + if ( typeParam.erasure().equals( genericParam ) && ( typeParam.getTypeParameters().size() == 1 ) ) { match = typeParam.getTypeParameters().get( 0 ).equals( targetType.getTypeParameters().get( 0 ) ); } } @@ -68,12 +54,12 @@ public class ListOfJaxbElemToListOfValue extends BuiltInMappingMethod { } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Parameter getParameter() { + return parameter; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/StringToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/builtin/StringToXmlGregorianCalendar.java index a53fefe0e..f7993c2a3 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/StringToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/StringToXmlGregorianCalendar.java @@ -21,14 +21,12 @@ package org.mapstruct.ap.builtin; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.GregorianCalendar; import java.util.Set; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; @@ -39,26 +37,18 @@ import static org.mapstruct.ap.util.Collections.asSet; * * @author Sjaak Derksen */ -public class StringToXmlGregorianCalendar extends BuiltInMappingMethod { +public class StringToXmlGregorianCalendar extends BuiltInMethod { - private static final Class SOURCE = String.class; - private static final Class TARGET = XMLGregorianCalendar.class; + private final Parameter parameter; + private final Type returnType; private final TypeFactory typeFactory; - private ConversionContext conversionContext; public StringToXmlGregorianCalendar( TypeFactory typeFactory ) { this.typeFactory = typeFactory; - } + this.parameter = typeFactory.createParameter( "date" , String.class ); + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "date" , SOURCE ) } ), - typeFactory.getType( TARGET ), - getContextParm() - ); } @Override @@ -72,21 +62,17 @@ public class StringToXmlGregorianCalendar extends BuiltInMappingMethod { } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Parameter getParameter() { + return parameter; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } @Override - public void setConversionContext(ConversionContext conversionContext) { - this.conversionContext = conversionContext; - } - - private String getContextParm() { + public String getContextParameter(ConversionContext conversionContext) { return conversionContext.getDateFormat() != null ? "\"" + conversionContext.getDateFormat() + "\"" : "null"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToCalendar.java b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToCalendar.java index 6128cc59b..2adbc808c 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToCalendar.java @@ -18,11 +18,9 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.Calendar; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -31,34 +29,23 @@ import org.mapstruct.ap.model.common.TypeFactory; * * @author Sjaak Derksen */ -public class XmlGregorianCalendarToCalendar extends BuiltInMappingMethod { +public class XmlGregorianCalendarToCalendar extends BuiltInMethod { - private static final Class SOURCE = XMLGregorianCalendar.class; - private static final Class TARGET = Calendar.class; - - private final TypeFactory typeFactory; + private final Parameter parameter; + private final Type returnType; public XmlGregorianCalendarToCalendar( TypeFactory typeFactory ) { - this.typeFactory = typeFactory; + this.parameter = typeFactory.createParameter( "xcal", XMLGregorianCalendar.class ); + this.returnType = typeFactory.getType( Calendar.class ); } @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "xcal", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); + public Parameter getParameter() { + return parameter; } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); - } - - @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToDate.java b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToDate.java index 77004e98b..9fd03e4b3 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToDate.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToDate.java @@ -18,11 +18,9 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import java.util.Date; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; @@ -31,34 +29,23 @@ import org.mapstruct.ap.model.common.TypeFactory; * * @author Sjaak Derksen */ -public class XmlGregorianCalendarToDate extends BuiltInMappingMethod { +public class XmlGregorianCalendarToDate extends BuiltInMethod { - private static final Class SOURCE = XMLGregorianCalendar.class; - private static final Class TARGET = Date.class; - - private final TypeFactory typeFactory; + private final Parameter parameter; + private final Type returnType; public XmlGregorianCalendarToDate( TypeFactory typeFactory ) { - this.typeFactory = typeFactory; + this.parameter = typeFactory.createParameter( "xcal", XMLGregorianCalendar.class ); + this.returnType = typeFactory.getType( Date.class ); } @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "xcal", SOURCE ) } ), - typeFactory.getType( TARGET ), - null - ); + public Parameter getParameter() { + return parameter; } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); - } - - @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); + public Type getReturnType() { + return returnType; } } diff --git a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToString.java b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToString.java index 065ed2932..a4386e463 100644 --- a/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToString.java +++ b/processor/src/main/java/org/mapstruct/ap/builtin/XmlGregorianCalendarToString.java @@ -18,10 +18,8 @@ */ package org.mapstruct.ap.builtin; -import org.mapstruct.ap.model.BuiltInMappingMethod; -import static java.util.Arrays.asList; +import org.mapstruct.ap.model.BuiltInMethod; import javax.xml.datatype.XMLGregorianCalendar; -import org.mapstruct.ap.model.MethodReference; import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; @@ -31,44 +29,28 @@ import org.mapstruct.ap.model.common.TypeFactory; * * @author Sjaak Derksen */ -public class XmlGregorianCalendarToString extends BuiltInMappingMethod { +public class XmlGregorianCalendarToString extends BuiltInMethod { - private static final Class SOURCE = XMLGregorianCalendar.class; - private static final Class TARGET = String.class; - - private final TypeFactory typeFactory; - private ConversionContext conversionContext; + private final Parameter parameter; + private final Type returnType; public XmlGregorianCalendarToString( TypeFactory typeFactory ) { - this.typeFactory = typeFactory; + this.parameter = typeFactory.createParameter( "xcal" , XMLGregorianCalendar.class ); + this.returnType = typeFactory.getType( String.class ); } @Override - public MethodReference createMethodReference() { - return new MethodReference( - getName(), - asList( new Parameter[] { typeFactory.createParameter( "xcal" , SOURCE ) } ), - typeFactory.getType( TARGET ), - getContextParm() - ); + public Parameter getParameter() { + return parameter; } @Override - public Type source() { - return typeFactory.getType( SOURCE ).erasure(); + public Type getReturnType() { + return returnType; } @Override - public Type target() { - return typeFactory.getType( TARGET ).erasure(); - } - - @Override - public void setConversionContext(ConversionContext conversionContext) { - this.conversionContext = conversionContext; - } - - private String getContextParm() { + public String getContextParameter(ConversionContext conversionContext) { return conversionContext.getDateFormat() != null ? "\"" + conversionContext.getDateFormat() + "\"" : "null"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java b/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java index 7cfc9127f..94b41a111 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java @@ -40,8 +40,7 @@ public interface ConversionProvider { * * @param sourceReference A reference to the source object, e.g. * {@code beanName.getFoo()}. - * @param conversionContext ConversionContext providing optional information required for creating - the conversion. + * @param conversionContext ConversionContext providing optional information required for creating the conversion. * * @return A conversion from source to target. */ @@ -52,8 +51,7 @@ public interface ConversionProvider { * * @param targetReference A reference to the targetReference object, e.g. * {@code beanName.getFoo()}. - * @param conversionContext ConversionContext providing optional information required for creating - the conversion. + * @param conversionContext ConversionContext providing optional information required for creating the conversion. * * @return A conversion from target to source. */ diff --git a/processor/src/main/java/org/mapstruct/ap/model/BuiltInMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/BuiltInMappingMethod.java deleted file mode 100644 index 32dc44107..000000000 --- a/processor/src/main/java/org/mapstruct/ap/model/BuiltInMappingMethod.java +++ /dev/null @@ -1,112 +0,0 @@ -/** - * 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; - -import java.util.Collections; -import java.util.Set; -import org.mapstruct.ap.model.common.ConversionContext; -import org.mapstruct.ap.model.common.ModelElement; -import org.mapstruct.ap.model.common.Type; -import org.mapstruct.ap.util.Strings; - -/** - * Implementations create: - * 1) an implementation of this build in method. - * 2) a reference to a build in method, to use in property mappings - * 3) a name for logging purposes. - * - * @author Sjaak Derksen - */ -public abstract class BuiltInMappingMethod extends ModelElement { - - /** - * Creates a reference to the conversion method - * - * @return reference to method implementation - */ - public abstract MethodReference createMethodReference(); - - - /** - * Sets the conversion context which is used to add context information such as date / time - * conversion pattern, etc. - * - * @param conversionContext ConversionContext providing optional information required for creating - the conversion. - */ - public void setConversionContext(ConversionContext conversionContext) { } - - /** - * hashCode - * - * @return hashCode - */ - @Override - public int hashCode() { - return this.getClass().hashCode(); - } - - /** - * equals based on class - * - * @param obj other class - * @return true when classes are the same - */ - @Override - public boolean equals( Object obj ) { - if ( obj == null ) { - return false; - } - return ( getClass() == obj.getClass() ); - } - - /** - * tests whether generics do match. Default true. - * - * @param sourceType - * @param targetType - * @return - */ - public boolean doGenericsMatch( Type sourceType, Type targetType ) { - return true; - } - - /** - * method name - * @return default method name is equal to class name of conversionmethod - */ - public String getName() { - return Strings.decapitalize( this.getClass().getSimpleName() ); - } - - /** - * imported types default. Only used types should be added. Source and Target types are coming via - * the MethodReference - * - * @return set of used types. - */ - @Override - public Set getImportTypes() { - return Collections.emptySet(); - } - - public abstract Type source(); - - public abstract Type target(); -} diff --git a/processor/src/main/java/org/mapstruct/ap/model/BuiltInMethod.java b/processor/src/main/java/org/mapstruct/ap/model/BuiltInMethod.java new file mode 100644 index 000000000..0cb344f1b --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/model/BuiltInMethod.java @@ -0,0 +1,166 @@ +/** + * 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; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import org.mapstruct.ap.model.common.Accessibility; +import org.mapstruct.ap.model.common.ConversionContext; +import org.mapstruct.ap.model.common.ModelElement; +import org.mapstruct.ap.model.common.Parameter; +import org.mapstruct.ap.model.common.Type; +import org.mapstruct.ap.model.source.BasicMethod; +import org.mapstruct.ap.util.Strings; + +/** + * Implementations create: + * 1) an implementation of this build in method. + * 2) a reference to a build in method, to use in property mappings + * 3) a name for logging purposes. + * + * @author Sjaak Derksen + */ +public abstract class BuiltInMethod extends ModelElement implements BasicMethod { + + + /** + * method name + * @return default method name is equal to class name of build in mehtod + */ + @Override + public String getName() { + return Strings.decapitalize( this.getClass().getSimpleName() ); + } + + /** + * imported types default. Only used types should be added. Source and Target types are coming via + * the MethodReference + * + * @return set of used types. + */ + @Override + public Set getImportTypes() { + return Collections.emptySet(); + } + + @Override + public boolean matches( Type sourceType, Type targetType ) { + if ( targetType.erasure().isAssignableTo( getReturnType().erasure() ) + && sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) { + return doTypeVarsMatch( sourceType, targetType ); + } + return false; + } + + /** + * + * @return all parameters are source parameters for build-in methods. + */ + @Override + public List getSourceParameters() { + return getParameters(); + } + + /** + * declaring mapper is always null, being the MapperImpl + * @return null + */ + @Override + public Type getDeclaringMapper() { + return null; + } + + @Override + public List getParameters() { + return Arrays.asList( new Parameter[] { getParameter() } ); + } + + /** + * target parameter mechanism not supported for build-in-method + * @return null + */ + @Override + public Parameter getTargetParameter() { + return null; + } + + /** + * the conversion context is used to format an auxiliary parameter in the method call + * 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 + */ + public String getContextParameter(ConversionContext conversionContext) { + return null; + } + + /** + * hashCode + * + * @return hashCode + */ + @Override + public int hashCode() { + return this.getClass().hashCode(); + } + + /** + * equals based on class + * + * @param obj other class + * @return true when classes are the same + */ + @Override + public boolean equals( Object obj ) { + if ( obj == null ) { + return false; + } + return ( getClass() == obj.getClass() ); + } + + /** + * Analyzes the Java Generics type variables in the parameter do match the type variables in the build in method + * same goes for the returnType. + * + * @param parameter source + * @param returnType target + * @return + */ + public boolean doTypeVarsMatch( Type parameter, Type returnType ) { + return true; + } + + /** + * There's currently only one parameter foreseen instead of a list of parameter + * + * @return the parameter + */ + public abstract Parameter getParameter(); + + @Override + public Accessibility getAccessibility() { + return Accessibility.PRIVATE; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java index dd0d66407..c571c858d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java +++ b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java @@ -53,14 +53,14 @@ public class Mapper extends ModelElement { private final List annotations; private final List mappingMethods; private final List referencedMappers; - private final Set builtInMethods; + private final Set builtInMethods; private final boolean suppressGeneratorTimestamp; private final Accessibility accessibility; private Mapper(TypeFactory typeFactory, String packageName, boolean superTypeIsInterface, String interfaceName, String implementationName, List mappingMethods, List referencedMappers, boolean suppressGeneratorTimestamp, - Set builtInMethods, Accessibility accessibility) { + Set builtInMethods, Accessibility accessibility) { this.packageName = packageName; this.superTypeIsInterface = superTypeIsInterface; this.interfaceName = interfaceName; @@ -80,7 +80,7 @@ public class Mapper extends ModelElement { private TypeElement element; private List mappingMethods; private List mapperReferences; - private Set builtInMethods; + private Set builtInMethods; private Elements elementUtils; private boolean suppressGeneratorTimestamp; @@ -100,7 +100,7 @@ public class Mapper extends ModelElement { return this; } - public Builder builtInMethods(Set builtInMethods) { + public Builder builtInMethods(Set builtInMethods) { this.builtInMethods = builtInMethods; return this; } @@ -157,7 +157,7 @@ public class Mapper extends ModelElement { addWithDependents( importedTypes, annotation.getType() ); } - for ( BuiltInMappingMethod builtInMethod : builtInMethods ) { + for ( BuiltInMethod builtInMethod : builtInMethods ) { for ( Type type : builtInMethod.getImportTypes() ) { addWithDependents( importedTypes, type ); } @@ -250,7 +250,7 @@ public class Mapper extends ModelElement { return accessibility; } - public Set getBuiltInMethods() { + public Set getBuiltInMethods() { return builtInMethods; } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java index 14e217d4b..5acad89d9 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java @@ -27,7 +27,7 @@ import org.mapstruct.ap.model.common.Accessibility; import org.mapstruct.ap.model.common.ModelElement; 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.BasicMethod; import org.mapstruct.ap.util.Strings; /** @@ -43,7 +43,7 @@ public abstract class MappingMethod extends ModelElement { private final Parameter targetParameter; private final Accessibility accessibility; - protected MappingMethod(Method method) { + protected MappingMethod(BasicMethod method) { this.name = method.getName(); this.parameters = method.getParameters(); this.returnType = method.getReturnType(); 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 d3bfa4ef1..550ecf9f5 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MethodReference.java @@ -19,9 +19,8 @@ package org.mapstruct.ap.model; import java.util.HashSet; -import java.util.List; import java.util.Set; -import org.mapstruct.ap.model.common.Parameter; +import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.source.Method; @@ -36,16 +35,17 @@ public class MethodReference extends MappingMethod { private final MapperReference declaringMapper; private final String contextParam; - public MethodReference(Method method, MapperReference declaringMapper) { + + public MethodReference(Method method, MapperReference declaringMapper ) { super( method ); this.declaringMapper = declaringMapper; - this.contextParam = null; + this.contextParam = null; } - public MethodReference(String name, List parameters, Type returnType, String contextParam ) { - super( name, parameters, returnType, null ); - this.contextParam = contextParam; + public MethodReference(BuiltInMethod method, ConversionContext contextParam ) { + super( method ); this.declaringMapper = null; + this.contextParam = method.getContextParameter( contextParam ); } public MapperReference getDeclaringMapper() { @@ -63,4 +63,11 @@ 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/source/BasicMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/BasicMethod.java new file mode 100644 index 000000000..9355d1619 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/model/source/BasicMethod.java @@ -0,0 +1,55 @@ +/** + * 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.source; + +import java.util.List; + +import org.mapstruct.ap.model.common.Accessibility; +import org.mapstruct.ap.model.common.Parameter; +import org.mapstruct.ap.model.common.Type; + +/** + * + * @author Sjaak Derksen + */ +public interface BasicMethod { + + /** + * + * @param sourceType + * @param targetType + * @return + */ + boolean matches( Type sourceType, Type targetType ); + + List getSourceParameters(); + + Type getDeclaringMapper(); + + String getName(); + + List getParameters(); + + Type getReturnType(); + + Parameter getTargetParameter(); + + Accessibility getAccessibility(); +} diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java index f402c1097..107200a6d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Method.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Method.java @@ -22,8 +22,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; + import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; +import javax.lang.model.util.Types; import org.mapstruct.ap.model.common.Accessibility; import org.mapstruct.ap.model.common.Parameter; @@ -40,7 +42,7 @@ import org.mapstruct.ap.util.Strings; * * @author Gunnar Morling */ -public class Method { +public class Method implements BasicMethod { private final Type declaringMapper; private final ExecutableElement executable; @@ -48,6 +50,7 @@ public class Method { private final Parameter targetParameter; private final Type returnType; private final Accessibility accessibility; + private final Types typeUtils; private Map> mappings; private IterableMapping iterableMapping; @@ -57,13 +60,22 @@ public class Method { public static Method forMethodRequiringImplementation(ExecutableElement executable, List parameters, Type returnType, Map> mappings, - IterableMapping iterableMapping, MapMapping mapMapping) { + IterableMapping iterableMapping, MapMapping mapMapping, + Types typeUtils ) { - return new Method( null, executable, parameters, returnType, mappings, iterableMapping, mapMapping ); + return new Method( + null, + executable, + parameters, + returnType, + mappings, + iterableMapping, + mapMapping, + typeUtils ); } public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, - List parameters, Type returnType) { + List parameters, Type returnType, Types typeUtils ) { return new Method( declaringMapper, @@ -72,12 +84,13 @@ public class Method { returnType, Collections.>emptyMap(), null, - null + null, + typeUtils ); } public static Method forFactoryMethod(Type declaringMapper, ExecutableElement executable, - Type returnType) { + Type returnType, Types typeUtils) { return new Method( declaringMapper, @@ -86,12 +99,14 @@ public class Method { returnType, Collections.>emptyMap(), null, - null + null, + typeUtils ); } private Method(Type declaringMapper, ExecutableElement executable, List parameters, Type returnType, - Map> mappings, IterableMapping iterableMapping, MapMapping mapMapping) { + Map> mappings, IterableMapping iterableMapping, MapMapping mapMapping, + Types typeUtils ) { this.declaringMapper = declaringMapper; this.executable = executable; this.parameters = parameters; @@ -102,6 +117,8 @@ public class Method { this.accessibility = Accessibility.fromModifiers( executable.getModifiers() ); this.targetParameter = determineTargetParameter( parameters ); + + this.typeUtils = typeUtils; } private Parameter determineTargetParameter(Iterable parameters) { @@ -120,6 +137,7 @@ public class Method { * * @return The declaring mapper type */ + @Override public Type getDeclaringMapper() { return declaringMapper; } @@ -128,14 +146,17 @@ public class Method { return executable; } + @Override public String getName() { return executable.getSimpleName().toString(); } + @Override public List getParameters() { return parameters; } + @Override public List getSourceParameters() { List sourceParameters = new ArrayList(); @@ -162,6 +183,7 @@ public class Method { return targetParameter != null ? targetParameter.getType() : returnType; } + @Override public Type getReturnType() { return returnType; } @@ -203,6 +225,7 @@ public class Method { && equals( getResultType(), method.getSourceParameters().iterator().next().getType() ); } + @Override public Parameter getTargetParameter() { return targetParameter; } @@ -267,8 +290,23 @@ public class Method { /** * Whether an implementation of this method must be generated or not. + * + * @return true when an implementation is required */ public boolean requiresImplementation() { return declaringMapper == null && executable.getModifiers().contains( Modifier.ABSTRACT ); } + + /** + * + * @param sourceType + * @param targetType + * @return + */ + @Override + public boolean matches( Type sourceType, Type targetType ) { + MethodMatcher matcher = new MethodMatcher(typeUtils, this ); + return matcher.matches( sourceType, targetType ); + } + } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java b/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java index ff298c7ea..6baeaa547 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java @@ -61,20 +61,31 @@ import org.mapstruct.ap.model.common.Type; */ public class MethodMatcher { - private final Type parameter; - private final Type returnType; + private final Method candidateMethod; private final Types typeUtils; private final Map genericTypesMap = new HashMap(); - public MethodMatcher(Types typeUtils, Method candidateMethod, Type returnType, Type parameter) { + /** + * package scope constructor + * + * @param typeUtils + * @param candidateMethod + */ + MethodMatcher(Types typeUtils, Method candidateMethod) { this.typeUtils = typeUtils; this.candidateMethod = candidateMethod; - this.parameter = parameter; - this.returnType = returnType; } - public boolean matches() { + /** + * package scoped method + * + * @param sourceType + * @param targetType + * + * @return true when both, sourceType and targetType matches the signature. + */ + boolean matches( Type sourceType, Type targetType ) { // check & collect generic types. List candidateParameters = candidateMethod.getExecutable().getParameters(); @@ -83,14 +94,14 @@ public class MethodMatcher { } TypeMatcher parameterMatcher = new TypeMatcher( Assignability.VISITED_ASSIGNABLE_FROM ); - if ( !parameterMatcher.visit( candidateParameters.iterator().next().asType(), parameter.getTypeMirror() ) ) { + if ( !parameterMatcher.visit( candidateParameters.iterator().next().asType(), sourceType.getTypeMirror() ) ) { return false; } // check return type TypeMirror candidateReturnType = candidateMethod.getExecutable().getReturnType(); TypeMatcher returnTypeMatcher = new TypeMatcher( Assignability.VISITED_ASSIGNABLE_TO ); - if ( !returnTypeMatcher.visit( candidateReturnType, returnType.getTypeMirror() ) ) { + if ( !returnTypeMatcher.visit( candidateReturnType, targetType.getTypeMirror() ) ) { return false; } 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 95a6a4958..c13cd83e7 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -41,7 +41,7 @@ import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.conversion.ConversionProvider; import org.mapstruct.ap.conversion.Conversions; import org.mapstruct.ap.model.common.DefaultConversionContext; -import org.mapstruct.ap.model.BuiltInMappingMethod; +import org.mapstruct.ap.model.BuiltInMethod; import org.mapstruct.ap.builtin.BuiltInMappingMethods; import org.mapstruct.ap.model.BeanMappingMethod; import org.mapstruct.ap.model.DefaultMapperReference; @@ -56,9 +56,9 @@ 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; +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.MethodMatcher; import org.mapstruct.ap.option.Options; import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.prism.MapperPrism; @@ -82,8 +82,7 @@ public class MapperCreationProcessor implements ModelElementProcessor usedBuiltInMethods; + private Set usedBuiltInMethods; @Override public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, List sourceModel) { @@ -95,11 +94,8 @@ public class MapperCreationProcessor implements ModelElementProcessor(); - - this.builtInMethods = new BuiltInMappingMethods(messager, typeFactory ); - this.usedBuiltInMethods = new HashSet(); + this.builtInMethods = new BuiltInMappingMethods( typeFactory ); + this.usedBuiltInMethods = new HashSet(); return getMapper( mapperTypeElement, sourceModel ); } @@ -121,7 +117,7 @@ public class MapperCreationProcessor implements ModelElementProcessor( usedBuiltInMethods ) ) + .builtInMethods( new HashSet( usedBuiltInMethods ) ) .build(); usedBuiltInMethods.clear(); @@ -129,7 +125,7 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences, - Iterable methods, Type parameterType, - Type returnType, String dateFormat) { - List candidatesWithMathingTargetType = new ArrayList(); + private T getBestMatch(Method mappingMethod, String mappedElement, + Iterable methods, Type parameterType, + Type returnType) { + List candidatesWithMathingTargetType = new ArrayList(); - for ( Method method : methods ) { + for ( T method : methods ) { if ( method.getSourceParameters().size() != 1 ) { continue; } - MethodMatcher m = new MethodMatcher( typeUtils, method, returnType, parameterType ); - if ( m.matches() ) { + if ( method.matches( parameterType, returnType ) ) { candidatesWithMathingTargetType.add( method ); } } - List candidatesWithBestMatchingSourceType = new ArrayList(); + List candidatesWithBestMatchingSourceType = new ArrayList(); int bestMatchingSourceTypeDistance = Integer.MAX_VALUE; - // find the methods with the minimum distance regarding source parameter type - for ( Method method : candidatesWithMathingTargetType ) { + // find the methods with the minimum distance regarding getParameter getParameter type + for ( T method : candidatesWithMathingTargetType ) { Parameter singleSourceParam = method.getSourceParameters().iterator().next(); int sourceTypeDistance = parameterType.distanceTo( singleSourceParam.getType() ); @@ -776,7 +784,7 @@ public class MapperCreationProcessor implements ModelElementProcessor 1 ) { messager.printMessage( Kind.ERROR, @@ -791,32 +799,15 @@ public class MapperCreationProcessor implements ModelElementProcessor candidatesWithBestMathingType, int bestMatchingTypeDistance, - Method method, int currentTypeDistance) { + + private int addToCandidateListIfMinimal(List candidatesWithBestMathingType, + int bestMatchingTypeDistance, T method, int currentTypeDistance) { if ( currentTypeDistance == bestMatchingTypeDistance ) { candidatesWithBestMathingType.add( method ); } @@ -829,6 +820,29 @@ 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; + } + } + return new MethodReference( method, mapperReference ); + } + return null; + } + + private MethodReference getMappingMethodReference( BuiltInMethod method, Type returnType, String dateFormat ) { + if ( method != null ) { + usedBuiltInMethods.add( method ); + ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat ); + return new MethodReference( method, ctx ); + } + return null; + } + /** * Reports an error if source the property can't be mapped from source to target. A mapping if possible if one of * the following conditions is true: @@ -952,10 +966,10 @@ public class MapperCreationProcessor implements ModelElementProcessor process(ProcessorContext context, TypeElement mapperTypeElement, Void sourceModel) { this.messager = context.getMessager(); this.typeFactory = context.getTypeFactory(); - + this.typeUtils = context.getTypeUtils(); return retrieveMethods( mapperTypeElement, true ); } @@ -138,7 +140,8 @@ public class MethodRetrievalProcessor implements ModelElementProcessor