Produces an adapter method handle which adapts the type of the current method handle to a new type. The resulting method handle is guaranteed to report a type which is equal to the desired new type.
If the original type and new type are equal, returns this
.
The new method handle, when invoked, will perform the following steps:
This method provides the crucial behavioral difference between
invokeExact
and plain, inexact invoke
.
The two methods
perform the same steps when the caller's type descriptor exactly m atches
the callee's, but when the types differ, plain invoke
also calls asType
(or some internal equivalent) in order
to match up the caller's and callee's types.
If the current method is a variable arity method handle argument list conversion may involve the conversion and collection of several arguments into an array, as described elsewhere. In every other case, all conversions are applied pairwise, which means that each argument or return value is converted to exactly one argument or return value (or no return value). The applied conversions are defined by consulting the the corresponding component types of the old and new method handle types.
Let T0 and T1 be corresponding new and old parameter types,
or old and new return types. Specifically, for some valid index i
, let
T0 =newType.parameterType(i)
and T1 =this.type().parameterType(i)
.
Or else, going the other way for return values, let
T0 =this.type().returnType()
and T1 =newType.returnType()
.
If the types are the same, the new method handle makes no change
to the corresponding argument or return value (if any).
Otherwise, one of the following conversions is applied
if possible:
java.lang.reflect.Method.invoke
.)
The unboxing conversion must have a possibility of success, which means that
if T0 is not itself a wrapper class, there must exist at least one
wrapper class TW which is a subtype of T0 and whose unboxed
primitive value can be widened to T1.
The method handle conversion cannot be made if any one of the required pairwise conversions cannot be made.
At runtime, the conversions applied to reference arguments
or return values may require additional runtime checks which can fail.
An unboxing operation may fail because the original reference is null,
causing a NullPointerException
.
An unboxing operation or a reference cast may also fail on a reference
to an object of the wrong type,
causing a ClassCastException
.
Although an unboxing operation may accept several kinds of wrappers,
if none are available, a ClassCastException
will be thrown.
newType | the expected type of the new method handle |
this
after performing
any necessary argument conversions, and arranges for any
necessary return value conversions
NullPointerException | if newType is a null reference | |
WrongMethodTypeException | if the conversion cannot be made |
MethodHandles.explicitCastArguments
Diagram: MethodHandle