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:
invokeExact
method requires the caller and callee method types to have identical components. This method is public, safe, and signature-polymorphic.invoke
method allows the caller and callee method types to differ, as long as the callee can be viewed under the caller type, using the asType
method. This method is public, safe, and signature-polymorphic.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 by MethodType.genericType
. This method is public.In addition, method handles can be viewed by various transforms that change their apparent type, and hence their acceptable invocation types and modes:
asType
and explicitCastArguments
, are public. Allowable conversions are drawn from those natural either to the Java language (on erased types only) or to the JVM itself. The explicitCastArguments
view allows some conversions not permitted by the asType
view, such as narrowing primitive casts.asSpreader
, is public. The array can be of any element type.asCollector
and asVarargsCollector
, are public. The array can be of any element type.discuss
discuss
discuss
discuss