public abstract class MethodHandle extends Object
In the case of #invokeExact, if the arguments do not match, based on a check of the MethodHandle's type()
,
a WrongMethodTypeException will be thrown.
In the case of #invoke, each of the arguments will be converted to the correct type, before the call is initiated. If the conversion cannot occur, a WrongMethodTypeException will be thrown.
Similar to #invoke, #invokeWithArguments will convert each of the arguments and place them on the stack before the call is initiated. If the conversion cannot occur, a WrongMethodTypeException will be thrown.
A MethodHandle can be created using the MethodHandles factory.
Modifier and Type | Method and Description |
---|---|
MethodHandle |
asCollector(Class<?> arrayClass,
int collectCount)
Returns a MethodHandle that collects the requested incoming arguments, which must match the
types in MethodType incomingArgs, into an array of arrayClass, called T.
|
MethodHandle |
asFixedArity()
Return a fixed arity version of the current MethodHandle.
|
MethodHandle |
asSpreader(Class<?> arrayClass,
int spreadCount)
Produce a MethodHandle that has an array of type arrayClass as its last argument and replaces the
array with spreadCount arguments from the array before calling the original MethodHandle.
|
MethodHandle |
asType(MethodType newType)
Returns a MethodHandle that presents as being of MethodType newType.
|
MethodHandle |
asVarargsCollector(Class<?> arrayParameter)
Create an varargs collector adapter on this MethodHandle.
|
MethodHandle |
bindTo(Object value)
Bind the value as the first argument to the MethodHandle
|
Object |
invoke(Object... args)
Invoke the receiver MethodHandle against the supplied arguments.
|
Object |
invokeExact(Object... args)
Invoke the receiver MethodHandle against the supplied arguments.
|
Object |
invokeWithArguments(List<?> args)
Helper method to call
invokeWithArguments(Object[]) . |
Object |
invokeWithArguments(Object... args)
Invoke the MethodHandle using an Object[] of arguments.
|
boolean |
isVarargsCollector()
Determine whether this is an
asVarargsCollector(Class) MethodHandle. |
String |
toString()
Answers a string containing a concise, human-readable
description of the receiver.
|
MethodType |
type()
The MethodType of the MethodHandle.
|
public final Object invokeExact(Object... args) throws Throwable, WrongMethodTypeException
args
- The argument list for the polymorphic signature callThrowable
- - To ensure type safety, must be marked as throwing Throwable.WrongMethodTypeException
- - If the resolved method type is not exactly equal to the MethodHandle's typepublic final Object invoke(Object... args) throws Throwable, WrongMethodTypeException, ClassCastException
args
- The argument list for the polymorphic signature callThrowable
- - To ensure type safety, must be marked as throwing Throwable.WrongMethodTypeException
- - If the resolved method type cannot be converted to the MethodHandle's typeClassCastException
- - if a conversion failspublic MethodType type()
public MethodHandle asSpreader(Class<?> arrayClass, int spreadCount) throws IllegalArgumentException, WrongMethodTypeException
The array must contain exactly spreadCount arguments to be passed to the original methodhandle. The array
may be null in the case when spreadCount is zero. Incorrect argument array size will cause the method to
throw an IllegalArgumentException
instead of invoking the target.
arrayClass
- - the source array for the spread argumentsspreadCount
- - how many arguments to spread from the arrayClassIllegalArgumentException
- - if arrayClass is not an array, the methodhandle has too few or too many parameters to satisfy spreadCountWrongMethodTypeException
- - if it cannot convert from one MethodType to the new type.public MethodHandle asCollector(Class<?> arrayClass, int collectCount) throws IllegalArgumentException, WrongMethodTypeException, NullPointerException
arrayClass
- - the class of the collect array. Usually matches the type of the last argument.collectCount
- - the number of arguments of type 'T' to collectIllegalArgumentException
- if arrayClass is not an array or is not assignable to the last parameter of the MethodHandle, or collectCount is an invalid array size (less than 0 or more than 254)WrongMethodTypeException
- if an asType call would fail when converting the final parameter to arrayClassNullPointerException
- if arrayClass is nullpublic MethodHandle asType(MethodType newType) throws ClassCastException
newType
- the MethodType for invoking this method withClassCastException
- if any of the requested coercions are invalid.public Object invokeWithArguments(Object... args) throws Throwable, WrongMethodTypeException, ClassCastException
args
- An array of Arguments, with length at exactly type().parameterCount() to be used in the call.Throwable
- May throw anything depending on the receiver MethodHandle.WrongMethodTypeException
- if the target cannot be adjusted to the number of Objects being passedClassCastException
- if an argument cannot be convertedpublic Object invokeWithArguments(List<?> args) throws Throwable, WrongMethodTypeException, ClassCastException, NullPointerException
invokeWithArguments(Object[])
.args
- - An array of arguments, with length at exactly type().parameterCount() to be used in the call.Throwable
- May throw anything depending on the receiver MethodHandle.WrongMethodTypeException
- if the target cannot be adjusted to the number of Objects being passedClassCastException
- if an argument cannot be convertedNullPointerException
- if the args list is nullpublic MethodHandle asVarargsCollector(Class<?> arrayParameter) throws IllegalArgumentException
asVarargsCollector(Class)
MethodHandles, invokeExact requires that the arguments
exactly match the underlying MethodType.
invoke acts as normally unless the arities differ. In that case, the trailing
arguments are converted as though by a call to asCollector(Class, int)
before invoking the underlying
methodhandle.
arrayParameter
- - the type of the array to collect the arguments intoIllegalArgumentException
- - if the arrayParameter is not an array class or cannot be assigned to the last parameter of the MethodTypepublic boolean isVarargsCollector()
asVarargsCollector(Class)
MethodHandle.asVarargsCollector(Class)
handle, false otherwise.public MethodHandle asFixedArity()
This is identical to the current method handle if isVarargsCollector()
is false.
If the current method is a varargs collector, then the returned handle will be the same but without the varargs nature.
public MethodHandle bindTo(Object value) throws IllegalArgumentException, ClassCastException
value
- the object to bind inIllegalArgumentException
- if the value is not compatible with the first argumentClassCastException
- if the value can't be cast to the type of the first argumentEclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2025 IBM Corp. and others.