Java getMethod with superclass parameters in method

Posted by Jonathon on Stack Overflow See other posts from Stack Overflow or by Jonathon
Published on 2010-04-05T19:38:37Z Indexed on 2010/04/05 19:43 UTC
Read the original article Hit count: 475

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about reflection