Method invocation via reflection when the argument is of type Object []

I use reflection to call a class method dynamically built at runtime :

public String createJDBCProvider(Object[] args)

Here's how:

Method m = adminTask.getClass().getMethod("createJDBCProvider", Object[].class);
id = (String) m.invoke(adminTask, new Object[]{ "a", "b", "c" });

IDEA warns me that I am guilty of redundant array creation for calling varargs method.

The method that I call actually accepts Object[], not Object ..., but they are probably equivalent and interchangeable, I think, so I'm moving forward.

At runtime, I get:

java.lang.IllegalArgumentException: wrong number of arguments

So it seems that mine Object[]is being passed as a sequence of Objects. Is that what is happening? If so, how can I make him not do this?

+5
source share
2 answers

, , , . :

id = (String) m.invoke(adminTask, new Object[]{ new Object[] {"a", "b", "c"} });
+6

:

Method m = adminTask.getClass().getMethod("createJDBCProvider", Object[].class);
id = (String) m.invoke(adminTask, new String[]{ "a", "b", "c" });

invoke public Object invoke(Object obj, *Object... args*), Idea , , vararg .

+1

All Articles