Method handle invocation
Method handles have several modes of invocation, involving various degrees of type checking and conversion.Every method handle invocation takes into account two independent sources of type information. The caller (whose call site is invoking the method handle) specifies the caller method type implicitly by the choice of the call descriptor in the call site bytecode. The callee (i.e., the method handle being invoked) has its own callee method type, which is simply its MethodHandle.type
property.
The possible modes of method handle invocation include:
- Exact invocation: The
invokeExact
method requires the caller and callee method types to have identical components. This method is public, safe, and signature-polymorphic. - Generic invocation: The generic
invoke
method allows the caller and callee method types to differ, as long as the callee can be viewed under the caller type, using theasType
method. This method is public, safe, and signature-polymorphic. - Varargs invocation: The overloaded
invokeWithArguments
method allow an arbitrary varargs array or list of untyped arguments to be applied to a callee, and the call succeeds as long as the callee can be viewed under an implied caller type determined as if byMethodType.genericType
. This method is public. - Basic invocation: The
- Invokedynamic invocation: The
In addition, method handles can be viewed by various transforms that change their apparent type, and hence their acceptable invocation types and modes:
- Retyping view: A method handle can be viewed as a range of other compatible callee method types. The view methods,
asType
andexplicitCastArguments
, are public. Allowable conversions are drawn from those natural either to the Java language (on erased types only) or to the JVM itself. TheexplicitCastArguments
view allows some conversions not permitted by theasType
view, such as narrowing primitive casts. - Spread view: A method handle can be viewed as if it accepts a varargs-like array of trailing arguments. The view method,
asSpreader
, is public. The array can be of any element type. - Collecting view: A method handle which accepts a trailing array argument can be viewed as if it takes those arguments positionally. Either a fixed or variable number of positional arguments are acceptable. The view methods,
asCollector
andasVarargsCollector
, are public. The array can be of any element type. - Other transforms, such as for argument permutation or binding, may also be considered as method handle views.
Exact invocation
discuss
Generic invocation
discuss
Varargs invocation
discuss
Basic invocation
discuss