#135 Simplifying XmlElementDeclSelector a bit

This commit is contained in:
Gunnar Morling 2014-02-26 23:14:23 +01:00
parent 40ed18af0b
commit f8aa758ecf
6 changed files with 44 additions and 59 deletions

View File

@ -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 <T extends Method> List<T> getMatchingMethods(
SourceMethod mappingMethod,
Collection<T> methods,
List<T> methods,
Type parameterType,
Type returnType,
String targetPropertyName

View File

@ -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 <T> 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
*/
<T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, Collection<T> methods, Type parameterType,
<T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, List<T> methods, Type parameterType,
Type returnType, String targetPropertyName);
}

View File

@ -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 <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, Collection<T> methods,
public <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, List<T> methods,
Type parameterType, Type returnType,
String targetPropertyName) {

View File

@ -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 <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, Collection<T> methods,
public <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, List<T> methods,
Type parameterType, Type returnType,
String targetPropertyName) {

View File

@ -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 <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, Collection<T> methods,
public <T extends Method> List<T> getMatchingMethods(SourceMethod mappingMethod, List<T> methods,
Type parameterType, Type returnType,
String targetPropertyName) {
List<T> noXmlDeclMatch = new ArrayList<T>();
List<T> nameMatch = new ArrayList<T>();
List<T> scopeMatch = new ArrayList<T>();
List<T> nameAndScopeMatch = new ArrayList<T>();
List<T> nameMatches = new ArrayList<T>();
List<T> scopeMatches = new ArrayList<T>();
List<T> nameAndScopeMatches = new ArrayList<T>();
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;
}
}

View File

@ -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<List<Sourc
private <T extends Method> T getBestMatch(SourceMethod mappingMethod,
String mappedElement,
Collection<T> methods,
List<T> methods,
Type parameterType,
Type returnType,
String targetPropertyName) {