Search in sources :

Example 1 with MethodAmbiguityException

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);
    }
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodAmbiguityException(com.github.javaparser.resolution.MethodAmbiguityException) ResolvedConstructorDeclaration(com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration) Map(java.util.Map) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MethodAmbiguityException(com.github.javaparser.resolution.MethodAmbiguityException) ResolvedConstructorDeclaration(com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)

Aggregations

MethodAmbiguityException (com.github.javaparser.resolution.MethodAmbiguityException)1 ResolvedConstructorDeclaration (com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)1 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)1 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)1 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1