JVM Knowledge

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 P_1, ..., P_p (p ≥ 1) are the type parameters of m, let α_1, ..., α_p be inference variables, and let θ be the substitution [P_1:=α_1, ..., P_p:=α_p].

  • An initial bound set, B_0, is generated from the declared bounds of P_1, ..., P_p, as described in §18.1.3.

  • For all i (1 ≤ i ≤ p), if P_i appears in the throws clause of m, then the bound throws α_i is implied. These bounds, if any, are incorporated with B_0 to produce a new bound set, B_1.

  • A set of constraint formulas, C, is generated as follows.

    Let F_1, ..., F_n be the formal parameter types of m, and let e_1, ..., e_k be the actual argument expressions of the invocation. Then:

    • To test for applicability by strict invocation:

      If k ≠ n, or if there exists an i (1 ≤ i ≤ n) such that e_i is pertinent to applicability (§15.12.2.2) and either i) e_i is a standalone expression of a primitive type but F_i is a reference type, or ii) F_i is a primitive type but e_i is not a standalone expression of a primitive type; then the method is not applicable and there is no need to proceed with inference.

      Otherwise, C includes, for all i (1 ≤ i ≤ k) where e_i is pertinent to applicability, ‹e_i → F_i θ›.

    • To test for applicability by loose invocation:

      If k ≠ n, the method is not applicable and there is no need to proceed with inference.

      Otherwise, C includes, for all i (1 ≤ i ≤ k) where e_i is pertinent to applicability, ‹e_i → F_i θ›.

    • To test for applicability by variable arity invocation:

      Let F'_1, ..., F'_k be the first k variable arity parameter types of m (§15.12.2.4). C includes, for all i (1 ≤ i ≤ k) where e_i is pertinent to applicability, ‹e_i → F'_i θ›.

  • C is reduced (§18.2) and the resulting bounds are incorporated with B_1 to produce a new bound set, B_2.

  • Finally, the method m is applicable if B_2 does not contain the bound false and resolution of all the inference variables in B_2 succeeds (§18.4).

Consider the following method invocation and assignment:

List<Number> ln = Arrays.asList(1, 2.0);

A most specific applicable method for the invocation must be identified as described in §15.12. The only potentially applicable method (§15.12.2.1) is declared as follows:

public static <T> List<T> asList(T... a)

Trivially (because of its arity), this method is neither applicable by strict invocation (§15.12.2.2) nor applicable by loose invocation (§15.12.2.3). But since there are no other candidates, in a third phase the method is checked for applicability by variable arity invocation.

The initial bound set, B, is a trivial upper bound for a single inference variable, α:

{ α <: Object }

The initial constraint formula set is as follows:

{ ‹1 → α›, ‹2.0 → α› }

These are reduced to a new bound set, B_1:

{ α <: Object, Integer <: α, Double <: α }

Then, to test whether the method is applicable, we attempt to resolve these bounds. We succeed, producing the rather complex instantiation

α = Number & Comparable<? extends Number & Comparable<?>>

We have thus demonstrated that the method is applicable; since no other candidates exist, it is the most specific applicable method. Still, the type of the method invocation, and its compatibility with the target type in the assignment, is not determined until further inference can occur, as described in the next section.