Skip to content

Commit

Permalink
issue80: usage of an extremely dirty trick for matching method calls …
Browse files Browse the repository at this point in the history
…involving wildcards
  • Loading branch information
ftomassetti committed Oct 13, 2016
1 parent 407c343 commit c48864c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Expand Up @@ -36,6 +36,10 @@ private static TypeUsage findCommonType(List<TypeUsage> variadicValues) {
}

public static boolean isApplicable(MethodDeclaration method, String name, List<TypeUsage> paramTypes, TypeSolver typeSolver) {
return isApplicable(method, name, paramTypes, typeSolver, false);
}

private static boolean isApplicable(MethodDeclaration method, String name, List<TypeUsage> paramTypes, TypeSolver typeSolver, boolean withWildcardTolerance) {
if (!method.getName().equals(name)) {
return false;
}
Expand All @@ -62,6 +66,7 @@ public static boolean isApplicable(MethodDeclaration method, String name, List<T
return false;
}
Map<String, TypeUsage> matchedParameters = new HashMap<>();
boolean needForWildCardTolerance = false;
for (int i = 0; i < method.getNoParams(); i++) {
TypeUsage expectedType = method.getParam(i).getType();
TypeUsage actualType = paramTypes.get(i);
Expand All @@ -81,11 +86,15 @@ public static boolean isApplicable(MethodDeclaration method, String name, List<T
}

if (!expectedType.isAssignableBy(actualType)) {
if (actualType.isWildcard() && withWildcardTolerance && !expectedType.isPrimitive()) {
needForWildCardTolerance = true;
continue;
}
return false;
}
}
}
return true;
return !withWildcardTolerance || needForWildCardTolerance;
}

public static boolean isAssignableMatchTypeParameters(ReferenceTypeUsage expected, ReferenceTypeUsage actual,
Expand Down Expand Up @@ -276,7 +285,15 @@ public int compare(MethodDeclaration m1, MethodDeclaration m2) {
* @return
*/
public static SymbolReference<MethodDeclaration> findMostApplicable(List<MethodDeclaration> methods, String name, List<TypeUsage> paramTypes, TypeSolver typeSolver) {
List<MethodDeclaration> applicableMethods = getMethodsWithoutDuplicates(methods).stream().filter((m) -> isApplicable(m, name, paramTypes, typeSolver)).collect(Collectors.toList());
SymbolReference<MethodDeclaration> res = findMostApplicable(methods, name, paramTypes, typeSolver, false);
if (res.isSolved()) {
return res;
}
return findMostApplicable(methods, name, paramTypes, typeSolver, true);
}

public static SymbolReference<MethodDeclaration> findMostApplicable(List<MethodDeclaration> methods, String name, List<TypeUsage> paramTypes, TypeSolver typeSolver, boolean wildcardTolerance) {
List<MethodDeclaration> applicableMethods = getMethodsWithoutDuplicates(methods).stream().filter((m) -> isApplicable(m, name, paramTypes, typeSolver, wildcardTolerance)).collect(Collectors.toList());
if (applicableMethods.isEmpty()) {
return SymbolReference.unsolved(MethodDeclaration.class);
}
Expand Down
Expand Up @@ -30,5 +30,5 @@
Line 94) comments.stream().filter( comment -> !other.contains(comment)).collect(Collectors.toList()) ==> java.util.stream.Stream.collect(java.util.stream.Collector<? super T, A, R>)
Line 94) comments.stream().filter( comment -> !other.contains(comment)) ==> java.util.stream.Stream.filter(java.util.function.Predicate<? super T>)
Line 94) comments.stream() ==> java.util.Collection.stream()
Line 95) other.contains(comment) ==> UNSOLVED
Line 95) other.contains(comment) ==> com.github.javaparser.ast.comments.CommentsCollection.contains(com.github.javaparser.ast.comments.Comment)
Line 96) Collectors.toList() ==> java.util.stream.Collectors.toList()
@@ -1,6 +1,6 @@
Line 46) sortByBeginPosition(nodes, false) ==> com.github.javaparser.utils.PositionUtils.sortByBeginPosition(java.util.List<T>, boolean)
Line 50) Collections.sort(nodes, ( o1, o2) -> PositionUtils.compare(o1, o2, ignoringAnnotations)) ==> java.util.Collections.sort(java.util.List<T>, java.util.Comparator<? super T>)
Line 50) PositionUtils.compare(o1, o2, ignoringAnnotations) ==> UNSOLVED
Line 50) PositionUtils.compare(o1, o2, ignoringAnnotations) ==> com.github.javaparser.utils.PositionUtils.compare(com.github.javaparser.ast.Node, com.github.javaparser.ast.Node, boolean)
Line 54) areInOrder(a, b, false) ==> com.github.javaparser.utils.PositionUtils.areInOrder(com.github.javaparser.ast.Node, com.github.javaparser.ast.Node, boolean)
Line 58) compare(a, b, ignoringAnnotations) ==> com.github.javaparser.utils.PositionUtils.compare(com.github.javaparser.ast.Node, com.github.javaparser.ast.Node, boolean)
Line 63) signum(beginLineWithoutConsideringAnnotation(a) - beginLineWithoutConsideringAnnotation(b)) ==> java.lang.Integer.signum(int)
Expand Down

0 comments on commit c48864c

Please sign in to comment.