Question

I'm trying this code http://ideone.com/gymUrP

Object obj2 = coll.getClass().getMethod("find").invoke(coll, new Object[]{});

is working with an empty second argument (matching find() method) but I didn't manage to make the other methods work, like the one with this signature (Docs)

The exception returned is always java.lang.IllegalArgumentException: wrong number of arguments

edit

Thanks to answers, this is close to what I'll need: (a bit heavy though)

Object[] args = new Object[]{new BasicDBObject("a", 2)};
Class[] types = new Class[args.length];
for (int i=0; i<args.length; i++)
    types[i] = (Class) args[i].getClass().getGenericInterfaces()[0];

Object obj = coll.getClass().getMethod("find", types).invoke(coll, args);
System.out.println(obj);
Was it helpful?

Solution 2

Looking at the Reflection API I'd say you missed the parameterTypes. I'd assume it to be:

Object obj2 = coll.getClass().getMethod("find", new Class[]{})
                             .invoke(coll, new Object[]{});

or

Object obj2 = coll.getClass().getMethod("find",null)
                             .invoke(coll, new Object[]{});

Look at this example as well.

OTHER TIPS

It should work with this call, passing a DBOject parameter and getting the method expecting this parameter:

DBOject dbo = ...
Object obj2 = coll.getClass().getMethod("find", DBOject.class).invoke(coll, dbo);

(I assume coll is a DBCollection object.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top