MethodHandle.invoke()
and MethodHandle.invokeExact()
are special methods that don't behave like other variable arity methods:
As is usual with virtual methods, source-level calls to
invokeExact
andinvoke
compile to aninvokevirtual
instruction. More unusually, the compiler must record the actual argument types, and may not perform method invocation conversions on the arguments. Instead, it must push them on the stack according to their own unconverted types. The method handle object itself is pushed on the stack before the arguments. The compiler then calls the method handle with a symbolic type descriptor which describes the argument and return types.
So, types of parameters really matter when you call these methods. If you want to pass parameters as Object[]
, you should use invokeWithArguments()
instead:
mh.invokeWithArguments(myArray);
See also: