Java's MethodHandle.invokeExact(Object...args) takes a variable-length list of arguments. When I try to pass an array of Object [] instead of a list, though, I get an error. See below:
private void doIt() throws Throwable {
Method meth = Foo.class.getDeclaredMethods()[0];
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.unreflect(meth);
Foo foo = new Foo();
String myStr = "aaa";
Integer myInt = new Integer(10);
Object [] myArray = {foo, myStr, myInt};
mh.invokeExact(foo, myStr, myInt); // prints "Called aaa 10"
mh.invokeExact(myArray); // throws Exception
}
class Foo {
public void bar(String myStr, Integer myInt) {
System.out.println("Called " + myStr + " " + myInt);
}
}
The second call to invokeExact() produces this Exception:
Exception in thread "main" java.lang.invoke.WrongMethodTypeException: (Ljava/lang/String;Ljava/lang/Integer;)V cannot be called with a different arity as ([Ljava/lang/Object;)V
at io.rhubarb.core.TestInvoke.doIt0(TestInvoke.java:26)
at io.rhubarb.core.TestInvoke.main(TestInvoke.java:11)
This might be related to a bug in Eclipse that was fixed two years ago (https://bugs.eclipse.org/bugs/show_bug.cgi?id=385404) but I don't think so, because when I close Eclipse, delete the /target directory, recompile everything using Maven, and run from the command line I get the same results.
I'm using Eclipse Kepler SR2, everything fully up to date, and JDK 1.7.0_25.
MethodHandle.invoke()
andMethodHandle.invokeExact()
are special methods that don't behave like other variable arity methods:So, types of parameters really matter when you call these methods. If you want to pass parameters as
Object[]
, you should useinvokeWithArguments()
instead:See also:
MethodHandle
javadoc