Search in sources :

Example 1 with ThrowsBound

use of com.github.javaparser.symbolsolver.resolution.typeinference.bounds.ThrowsBound in project javaparser by javaparser.

the class TypeInference method instantiationInference.

public Optional<InstantiationSet> instantiationInference(List<Expression> argumentExpressions, ResolvedMethodDeclaration methodDeclaration) {
    // if (methodCallExpr.getTypeArguments().isPresent()) {
    // throw new IllegalArgumentException("Type inference unnecessary as type arguments have been specified");
    // }
    // Given a method invocation that provides no explicit type arguments, the process to determine whether a
    // potentially applicable generic method m is applicable is as follows:
    // - Where P1, ..., Pp (p ≥ 1) are the type parameters of m, let α1, ..., αp be inference variables, and
    // let θ be the substitution [P1:=α1, ..., Pp:=αp].
    List<ResolvedTypeParameterDeclaration> Ps = methodDeclaration.getTypeParameters();
    List<InferenceVariable> alphas = InferenceVariable.instantiate(Ps);
    Substitution theta = Substitution.empty();
    for (int i = 0; i < Ps.size(); i++) {
        theta = theta.withPair(Ps.get(0), alphas.get(0));
    }
    // - An initial bound set, B0, is constructed from the declared bounds of P1, ..., Pp, as described in §18.1.3.
    BoundSet B0 = boundSetup(Ps, alphas);
    // - For all i (1 ≤ i ≤ p), if Pi appears in the throws clause of m, then the bound throws αi is implied.
    // These bounds, if any, are incorporated with B0 to produce a new bound set, B1.
    BoundSet B1 = B0;
    for (int i = 0; i < Ps.size(); i++) {
        ResolvedTypeParameterDeclaration Pi = Ps.get(i);
        if (appearInThrowsClause(Pi, methodDeclaration)) {
            B1 = B1.withBound(new ThrowsBound(alphas.get(i)));
        }
    }
    // - A set of constraint formulas, C, is constructed as follows.
    // 
    // Let F1, ..., Fn be the formal parameter types of m, and let e1, ..., ek be the actual argument expressions
    // of the invocation. Then:
    List<ResolvedType> Fs = formalParameterTypes(methodDeclaration);
    List<Expression> es = argumentExpressions;
    Optional<ConstraintFormulaSet> C = Optional.empty();
    if (!C.isPresent()) {
        C = testForApplicabilityByStrictInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        C = testForApplicabilityByLooseInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        C = testForApplicabilityByVariableArityInvocation(Fs, es, theta);
    }
    if (!C.isPresent()) {
        return Optional.empty();
    }
    // - C is reduced (§18.2) and the resulting bounds are incorporated with B1 to produce a new bound set, B2.
    BoundSet resultingBounds = C.get().reduce(typeSolver);
    BoundSet B2 = B1.incorporate(resultingBounds, typeSolver);
    if (B2.containsFalse()) {
        return Optional.empty();
    }
    Optional<InstantiationSet> instantiation = B2.performResolution(alphas, typeSolver);
    return instantiation;
}
Also used : ThrowsBound(com.github.javaparser.symbolsolver.resolution.typeinference.bounds.ThrowsBound) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ExpressionHelper.isStandaloneExpression(com.github.javaparser.symbolsolver.resolution.typeinference.ExpressionHelper.isStandaloneExpression) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)1 ExpressionHelper.isStandaloneExpression (com.github.javaparser.symbolsolver.resolution.typeinference.ExpressionHelper.isStandaloneExpression)1 ThrowsBound (com.github.javaparser.symbolsolver.resolution.typeinference.bounds.ThrowsBound)1