I have the following interface:
interface Foo {
void bar(String a, int b);
}
I want to invoke Foo.bar (on an implementation of Foo) reflectively. However, the arguments are in array and i do not know the size of it.
The following does not work:
void gee(Foo someFoo, Method bar, Object[] args) {
bar.invoke(someFoo, args);
}
That does not work because args is threated by the compiler as a single argument and the array is not "expanded" to vararg but is wrapped (internally) in one more array with single element, i.e.
@Test
public void varArgTest() {
assertTrue(varArgFoo(new Object[] {1, 2}) == 1);
}
private static <T> int varArgFoo(T... arg) {
return arg.length;
}
How can i call Method.invoke() in this case so that the array is threated as vararg?
Or more general question: how do i call vararg method when arguments are in array i do not knew the size of the array until runtime.