public MethodHandle asType (MethodType newType)

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:

(Note: Both T0 and T1 may be regarded as static types, because neither corresponds specifically to the dynamic type of any actual argument or return value.)

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.

Parameters:
newType    the expected type of the new method handle

Returns:  a method handle which delegates to this after performing any necessary argument conversions, and arranges for any necessary return value conversions

Exceptions:
NullPointerException    if newType is a null reference
WrongMethodTypeException    if the conversion cannot be made

See also:
MethodHandles.explicitCastArguments