#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; package org.mapstruct.ap.model.source.selector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
@ -37,7 +36,7 @@ public class InheritanceSelector implements MethodSelector {
@Override @Override
public <T extends Method> List<T> getMatchingMethods( public <T extends Method> List<T> getMatchingMethods(
SourceMethod mappingMethod, SourceMethod mappingMethod,
Collection<T> methods, List<T> methods,
Type parameterType, Type parameterType,
Type returnType, Type returnType,
String targetPropertyName String targetPropertyName

View File

@ -18,7 +18,6 @@
*/ */
package org.mapstruct.ap.model.source.selector; package org.mapstruct.ap.model.source.selector;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -39,13 +38,13 @@ public interface MethodSelector {
* *
* @param <T> either SourceMethod or BuiltInMethod * @param <T> either SourceMethod or BuiltInMethod
* @param mappingMethod mapping method, defined in Mapper for which this selection is carried out * @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 parameterType parameter type that should be matched
* @param returnType return type that should be matched * @param returnType return type that should be matched
* @param targetPropertyName some information can be derived from the target property * @param targetPropertyName some information can be derived from the target property
* *
* @return list of methods that passes the matching process * @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); Type returnType, String targetPropertyName);
} }

View File

@ -20,8 +20,8 @@ package org.mapstruct.ap.model.source.selector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -47,7 +47,7 @@ public class MethodSelectors implements MethodSelector {
} }
@Override @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, Type parameterType, Type returnType,
String targetPropertyName) { String targetPropertyName) {

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model.source.selector; package org.mapstruct.ap.model.source.selector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -36,7 +35,7 @@ import org.mapstruct.ap.model.source.SourceMethod;
public class TypeSelector implements MethodSelector { public class TypeSelector implements MethodSelector {
@Override @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, Type parameterType, Type returnType,
String targetPropertyName) { String targetPropertyName) {

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.model.source.selector; package org.mapstruct.ap.model.source.selector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
@ -53,67 +52,57 @@ public class XmlElementDeclSelector implements MethodSelector {
} }
@Override @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, Type parameterType, Type returnType,
String targetPropertyName) { String targetPropertyName) {
List<T> noXmlDeclMatch = new ArrayList<T>(); List<T> nameMatches = new ArrayList<T>();
List<T> nameMatch = new ArrayList<T>(); List<T> scopeMatches = new ArrayList<T>();
List<T> scopeMatch = new ArrayList<T>(); List<T> nameAndScopeMatches = new ArrayList<T>();
List<T> nameAndScopeMatch = new ArrayList<T>();
for ( T candidate : methods ) { for ( T candidate : methods ) {
if ( candidate instanceof SourceMethod ) { if ( !(candidate instanceof SourceMethod ) ) {
continue;
}
SourceMethod candidateMethod = (SourceMethod) candidate; SourceMethod candidateMethod = (SourceMethod) candidate;
XmlElementDeclPrism xmlElememtDecl XmlElementDeclPrism xmlElememtDecl = XmlElementDeclPrism.getInstanceOn( candidateMethod.getExecutable() );
= XmlElementDeclPrism.getInstanceOn( candidateMethod.getExecutable() );
if ( xmlElememtDecl != null ) { if ( xmlElememtDecl == null ) {
continue;
}
String name = xmlElememtDecl.name(); String name = xmlElememtDecl.name();
TypeMirror scope = xmlElememtDecl.scope(); TypeMirror scope = xmlElememtDecl.scope();
TypeMirror target = mappingMethod.getExecutable().getReturnType(); TypeMirror target = mappingMethod.getExecutable().getReturnType();
if ( ( scope != null ) && ( name != null ) ) {
// both scope and name should match when both defined boolean nameIsSetAndMatches = name != null && name.equals( targetPropertyName );
if ( name.equals( targetPropertyName ) && typeUtils.isSameType( scope, target ) ) { boolean scopeIsSetAndMatches = scope != null && typeUtils.isSameType( scope, target );
nameAndScopeMatch.add( candidate );
} if ( nameIsSetAndMatches ) {
} if ( scopeIsSetAndMatches ) {
else if ( ( scope == null ) && ( name != null ) ) { nameAndScopeMatches.add( candidate );
// 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 { else {
// cannot make verdict based on scope or name, so add nameMatches.add( candidate );
noXmlDeclMatch.add( candidate );
} }
} }
else { else if ( scopeIsSetAndMatches ) {
// cannot make a verdict on xmldeclannotation, so add scopeMatches.add( candidate );
noXmlDeclMatch.add( candidate );
}
}
else {
// cannot make a verdict on xmldeclannotation, so add
noXmlDeclMatch.add( candidate );
} }
} }
if ( nameAndScopeMatch.size() > 0 ) { if ( nameAndScopeMatches.size() > 0 ) {
return nameAndScopeMatch; return nameAndScopeMatches;
} }
else if ( scopeMatch.size() > 0 ) { else if ( scopeMatches.size() > 0 ) {
return scopeMatch; return scopeMatches;
} }
else if ( nameMatch.size() > 0 ) { else if ( nameMatches.size() > 0 ) {
return nameMatch; return nameMatches;
}
else {
return methods;
} }
return noXmlDeclMatch;
} }
} }

View File

@ -20,7 +20,6 @@ package org.mapstruct.ap.processor;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -954,7 +953,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
private <T extends Method> T getBestMatch(SourceMethod mappingMethod, private <T extends Method> T getBestMatch(SourceMethod mappingMethod,
String mappedElement, String mappedElement,
Collection<T> methods, List<T> methods,
Type parameterType, Type parameterType,
Type returnType, Type returnType,
String targetPropertyName) { String targetPropertyName) {