Java Reference
In-Depth Information
11.6. Finding the Right Method Revisited
In " Finding the Right Method " on page 224 , we outlined the basic al-
gorithm used by the compiler to determine which form of a method
should be invoked for a given method invocation expression. We can now
consider how generic types and generic methods (and constructors) af-
fect that basic algorithm.
The changes to the algorithm can be summarized as follows:
1. If the method invocation includes explicit type arguments, then
any potentially applicable generic methods must have the same
number of type parameters. Non-generic methods may also be po-
tentially applicable, in which case the actual type arguments are
ignored.
2. If there are no explicit type arguments in the invocation, and a
generic method might be applicable, then first the type arguments
for the generic method are inferred based on the static types of
the argument expressions used in the method invocation. If no
types that satisfy all the constraints on the type parameters can
be inferred, then the method is not applicable.
3. At each phase, once the set of potentially applicable methods is
determined, applicable methods are searched for as previously de-
scribed. For generic methods, either the explicit (if present) or the
inferred type arguments establish whether the argument type is
compatible with the formal parameter type.
4. When the most specific method is searched for, type inference is
again used when generic methods are being considered. However,
this time the type inference is not based on the actual argument
expression types; rather, the initial constraint is between the
formal parameters of the generic method and the formal paramet-
ers of the method against which it is being tested for being most
specific. In simple terms, the most specific test considers only the
declared parameter types of the methods concerned, not the type
 
Search WWH ::




Custom Search