Given:
class A
{
public void m(List l) { ... }
}
Let's say I want to invoke method m
with reflection, passing an ArrayList as the parameter to m
:
List myList = new ArrayList();
A a = new A();
Method method = A.class.getMethod("m", new Class[] { myList.getClass() });
method.invoke(a, Object[] { myList });
The getMethod
on line 3 will throw NoSuchMethodException
because the runtime type of myList is ArrayList, not List.
Is there a good generic way around this that doesn't require knowledge of class A's parameter types?