When testing that one applicable method is more specific than another (§15.12.2.5), where the second method is generic, it is necessary to test whether some instantiation of the second method's type parameters can be inferred to make the first method more specific than the second.
Let m_1 be the first method and m_2 be the second method. Where m_2 has type parameters P_1, ..., P_p, let α_1, ..., α_p be inference variables, and let θ be the substitution [P_1:=α_1, ..., P_p:=α_p].
Let e_1, ..., e_k be the argument expressions of the corresponding invocation. Then:
- If
m_1andm_2are applicable by strict or loose invocation (§15.12.2.2, §15.12.2.3), then let S_1, ..., S_k be the formal parameter types ofm_1, and let T_1, ..., T_k be the result of θ applied to the formal parameter types ofm_2. - If
m_1andm_2are applicable by variable arity invocation (§15.12.2.4), then let S_1, ..., S_k be the first k variable arity parameter types ofm_1, and let T_1, ..., T_k be the result of θ applied to the first k variable arity parameter types ofm_2.
Note that no substitution is applied to S_1, ..., S_k; even if m_1 is generic, the type parameters of m_1 are treated as type variables, not inference variables.
The process to determine if m_1 is more specific than m_2 is as follows:
-
First, an initial bound set, B, is constructed from the declared bounds of P_1, ..., P_p, as specified in §18.1.3.
-
Second, for all i (1 ≤ i ≤ k), a set of constraint formulas or bounds is generated.
If T_i is a proper type, the result is true if S_i is more specific than T_i for
e_i(§15.12.2.5), and false otherwise. (Note that S_i is always a proper type.)Otherwise, if T_i is not a functional interface type, the constraint formula ‹S_i
<:T_i› is generated.Otherwise, T_i is a parameterization of a functional interface, I. It must be determined whether S_i satisfies the following five conditions:
- S_i is a functional interface type.
- S_i is not a superinterface of I, nor a parameterization of a superinterface of I.
- S_i is not a subinterface of I, nor a parameterization of a subinterface of I.
- If S_i is an intersection type, at least one element of the intersection is not a superinterface of I, nor a parameterization of a superinterface of I.
- If S_i is an intersection type, no element of the intersection is a subinterface of I, nor a parameterization of a subinterface of I.
If all five conditions are true, then the following constraint formulas or bounds are generated (where U_1 ... U_k and R_1 are the parameter types and return type of the function type of the capture of S_i, and V_1 ... V_k and R_2 are the parameter types and return type of the function type of T_i):
-
If
e_iis an explicitly typed lambda expression:- For all j (1 ≤ j ≤ k), ‹U_j = V_j›.
- If R_2 is
void, true. - Otherwise, if R_1 and R_2 are functional interface types, and neither interface is a subinterface of the other, and
e_ihas at least one result expression, then these rules are applied recursively to R_1 and R_2, for each result expression ine_i. - Otherwise, if R_1 is a primitive type and R_2 is not, and
e_ihas at least one result expression, and each result expression ofe_iis a standalone expression (§15.2) of a primitive type, true. - Otherwise, if R_2 is a primitive type and R_1 is not, and
e_ihas at least one result expression, and each result expression ofe_iis either a standalone expression of a reference type or a poly expression, true. - Otherwise, ‹R_1
<:R_2›.
-
If
e_iis an exact method reference:- For all j (1 ≤ j ≤ k), ‹U_j = V_j›.
- If R_2 is
void, true. - Otherwise, if R_1 is a primitive type and R_2 is not, and the compile-time declaration for
e_ihas a primitive return type, true. - Otherwise if R_2 is a primitive type and R_1 is not, and the compile-time declaration for
e_ihas a reference return type, true. - Otherwise, ‹R_1
<:R_2›.
-
If
e_iis a parenthesized expression, these rules are applied recursively to the contained expression. -
If
e_iis a conditional expression, these rules are applied recursively to each of the second and third operands. -
Otherwise, false.
If the five constraints on S_i are not satisfied, the constraint formula ‹S_i
<:T_i› is generated instead. -
Third, if
m_2is applicable by variable arity invocation and has k+1 parameters, then where S_{k+1} is the k+1'th variable arity parameter type ofm_1and T_{k+1} is the result of θ applied to the k+1'th variable arity parameter type ofm_2, the constraint ‹S_{k+1}<:T_{k+1}› is generated. -
Fourth, the generated bounds and constraint formulas are reduced and incorporated with B to produce a bound set B'.
If B' does not contain the bound false, and resolution of all the inference variables in B' succeeds, then
m_1is more specific thanm_2.Otherwise,
m_1is not more specific thanm_2.