use of com.github.javaparser.resolution.MethodAmbiguityException in project javaparser by javaparser.
the class ConstructorResolutionLogic method findMostApplicable.
public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable(List<ResolvedConstructorDeclaration> constructors, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance) {
List<ResolvedConstructorDeclaration> applicableConstructors = constructors.stream().filter((m) -> isApplicable(m, argumentsTypes, typeSolver, wildcardTolerance)).collect(Collectors.toList());
if (applicableConstructors.isEmpty()) {
return SymbolReference.unsolved(ResolvedConstructorDeclaration.class);
}
if (applicableConstructors.size() == 1) {
return SymbolReference.solved(applicableConstructors.get(0));
} else {
ResolvedConstructorDeclaration winningCandidate = applicableConstructors.get(0);
ResolvedConstructorDeclaration other = null;
boolean possibleAmbiguity = false;
for (int i = 1; i < applicableConstructors.size(); i++) {
other = applicableConstructors.get(i);
if (isMoreSpecific(winningCandidate, other, typeSolver)) {
possibleAmbiguity = false;
} else if (isMoreSpecific(other, winningCandidate, typeSolver)) {
possibleAmbiguity = false;
winningCandidate = other;
} else {
if (winningCandidate.declaringType().getQualifiedName().equals(other.declaringType().getQualifiedName())) {
possibleAmbiguity = true;
} else {
// we expect the methods to be ordered such that inherited methods are later in the list
}
}
}
if (possibleAmbiguity) {
// pick the first exact match if it exists
if (!MethodResolutionLogic.isExactMatch(winningCandidate, argumentsTypes)) {
if (MethodResolutionLogic.isExactMatch(other, argumentsTypes)) {
winningCandidate = other;
} else {
throw new MethodAmbiguityException("Ambiguous constructor call: cannot find a most applicable constructor: " + winningCandidate + ", " + other);
}
}
}
return SymbolReference.solved(winningCandidate);
}
}
Aggregations