How to call MethodHandle.invokeExact() with an array of Object[]?
Asked Answered
G

1

10

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.

Gin answered 2/5, 2014 at 18:1 Comment(1)
Why are you using reflection in the first place?Bohs
M
16

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 and invoke compile to an invokevirtual 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:

Mollusc answered 2/5, 2014 at 18:55 Comment(2)
In this case I would say InvokeExact method signature is very misleading. Why expose it as Object... instead of some builder class that can expose ‘addParam’ method?Sphenoid
Yep, this is crazytown!Amused

© 2022 - 2024 — McMap. All rights reserved.