diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/selector/InheritanceSelector.java b/processor/src/main/java/org/mapstruct/ap/model/source/selector/InheritanceSelector.java index 7c941f3a5..4ad873b8b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/selector/InheritanceSelector.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/selector/InheritanceSelector.java @@ -19,7 +19,6 @@ package org.mapstruct.ap.model.source.selector; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import org.mapstruct.ap.model.common.Parameter; @@ -37,7 +36,7 @@ public class InheritanceSelector implements MethodSelector { @Override public List getMatchingMethods( SourceMethod mappingMethod, - Collection methods, + List methods, Type parameterType, Type returnType, String targetPropertyName diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelector.java b/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelector.java index ea502f224..a48009e24 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelector.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelector.java @@ -18,7 +18,6 @@ */ package org.mapstruct.ap.model.source.selector; -import java.util.Collection; import java.util.List; import org.mapstruct.ap.model.common.Type; @@ -39,13 +38,13 @@ public interface MethodSelector { * * @param either SourceMethod or BuiltInMethod * @param mappingMethod mapping method, defined in Mapper for which this selection is carried out - * @param methods set of available methods + * @param methods list of available methods * @param parameterType parameter type that should be matched * @param returnType return type that should be matched * @param targetPropertyName some information can be derived from the target property * * @return list of methods that passes the matching process */ - List getMatchingMethods(SourceMethod mappingMethod, Collection methods, Type parameterType, + List getMatchingMethods(SourceMethod mappingMethod, List methods, Type parameterType, Type returnType, String targetPropertyName); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelectors.java b/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelectors.java index 40bb33197..83dee2f9f 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelectors.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/selector/MethodSelectors.java @@ -20,8 +20,8 @@ package org.mapstruct.ap.model.source.selector; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; + import javax.lang.model.util.Types; import org.mapstruct.ap.model.common.Type; @@ -47,7 +47,7 @@ public class MethodSelectors implements MethodSelector { } @Override - public List getMatchingMethods(SourceMethod mappingMethod, Collection methods, + public List getMatchingMethods(SourceMethod mappingMethod, List methods, Type parameterType, Type returnType, String targetPropertyName) { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/selector/TypeSelector.java b/processor/src/main/java/org/mapstruct/ap/model/source/selector/TypeSelector.java index 24a22e3c8..d2d631149 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/selector/TypeSelector.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/selector/TypeSelector.java @@ -19,7 +19,6 @@ package org.mapstruct.ap.model.source.selector; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import org.mapstruct.ap.model.common.Type; @@ -36,7 +35,7 @@ import org.mapstruct.ap.model.source.SourceMethod; public class TypeSelector implements MethodSelector { @Override - public List getMatchingMethods(SourceMethod mappingMethod, Collection methods, + public List getMatchingMethods(SourceMethod mappingMethod, List methods, Type parameterType, Type returnType, String targetPropertyName) { diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/selector/XmlElementDeclSelector.java b/processor/src/main/java/org/mapstruct/ap/model/source/selector/XmlElementDeclSelector.java index c1a2c2fb2..f45f5c820 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/selector/XmlElementDeclSelector.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/selector/XmlElementDeclSelector.java @@ -19,7 +19,6 @@ package org.mapstruct.ap.model.source.selector; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import javax.lang.model.type.TypeMirror; @@ -53,67 +52,57 @@ public class XmlElementDeclSelector implements MethodSelector { } @Override - public List getMatchingMethods(SourceMethod mappingMethod, Collection methods, + public List getMatchingMethods(SourceMethod mappingMethod, List methods, Type parameterType, Type returnType, String targetPropertyName) { - List noXmlDeclMatch = new ArrayList(); - List nameMatch = new ArrayList(); - List scopeMatch = new ArrayList(); - List nameAndScopeMatch = new ArrayList(); + List nameMatches = new ArrayList(); + List scopeMatches = new ArrayList(); + List nameAndScopeMatches = new ArrayList(); for ( T candidate : methods ) { - if ( candidate instanceof SourceMethod ) { - SourceMethod candidateMethod = (SourceMethod) candidate; - XmlElementDeclPrism xmlElememtDecl - = XmlElementDeclPrism.getInstanceOn( candidateMethod.getExecutable() ); - if ( xmlElememtDecl != null ) { - String name = xmlElememtDecl.name(); - TypeMirror scope = xmlElememtDecl.scope(); - TypeMirror target = mappingMethod.getExecutable().getReturnType(); - if ( ( scope != null ) && ( name != null ) ) { - // both scope and name should match when both defined - if ( name.equals( targetPropertyName ) && typeUtils.isSameType( scope, target ) ) { - nameAndScopeMatch.add( candidate ); - } - } - else if ( ( scope == null ) && ( name != null ) ) { - // name should match when defined - if ( name.equals( targetPropertyName ) ) { - nameMatch.add( candidate ); - } - } - else if ( ( scope != null ) && ( name == null ) ) { - // scope should match when defined - if ( typeUtils.isSameType( scope, target ) ) { - scopeMatch.add( candidate ); - } - } - else { - // cannot make verdict based on scope or name, so add - noXmlDeclMatch.add( candidate ); - } + if ( !(candidate instanceof SourceMethod ) ) { + continue; + } + + SourceMethod candidateMethod = (SourceMethod) candidate; + XmlElementDeclPrism xmlElememtDecl = XmlElementDeclPrism.getInstanceOn( candidateMethod.getExecutable() ); + + if ( xmlElememtDecl == null ) { + continue; + } + + String name = xmlElememtDecl.name(); + TypeMirror scope = xmlElememtDecl.scope(); + TypeMirror target = mappingMethod.getExecutable().getReturnType(); + + boolean nameIsSetAndMatches = name != null && name.equals( targetPropertyName ); + boolean scopeIsSetAndMatches = scope != null && typeUtils.isSameType( scope, target ); + + if ( nameIsSetAndMatches ) { + if ( scopeIsSetAndMatches ) { + nameAndScopeMatches.add( candidate ); } else { - // cannot make a verdict on xmldeclannotation, so add - noXmlDeclMatch.add( candidate ); + nameMatches.add( candidate ); } } - else { - // cannot make a verdict on xmldeclannotation, so add - noXmlDeclMatch.add( candidate ); + else if ( scopeIsSetAndMatches ) { + scopeMatches.add( candidate ); } } - if ( nameAndScopeMatch.size() > 0 ) { - return nameAndScopeMatch; + if ( nameAndScopeMatches.size() > 0 ) { + return nameAndScopeMatches; } - else if ( scopeMatch.size() > 0 ) { - return scopeMatch; + else if ( scopeMatches.size() > 0 ) { + return scopeMatches; } - else if ( nameMatch.size() > 0 ) { - return nameMatch; + else if ( nameMatches.size() > 0 ) { + return nameMatches; + } + else { + return methods; } - return noXmlDeclMatch; } } 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 af39120b0..2769a0053 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -20,7 +20,6 @@ package org.mapstruct.ap.processor; import java.text.MessageFormat; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -954,7 +953,7 @@ public class MapperCreationProcessor implements ModelElementProcessor T getBestMatch(SourceMethod mappingMethod, String mappedElement, - Collection methods, + List methods, Type parameterType, Type returnType, String targetPropertyName) {