user537555
user537555

Reputation: 305

Java Reflection getDeclaredMethod() with Generic Class Type

I have a method in class A:

class Parameter {
...
}

class A {
   private <T extends B> void call(T object, Parameter... parameters){
   ...
   }
}

Now I want to use reflection to get the method "call",

A a = new A();
// My question is what should be arguments in getDeclaredMethod
//Method method = a.getClass().getDeclaredMethod()

Thx.

Upvotes: 5

Views: 4793

Answers (1)

Paul Bellora
Paul Bellora

Reputation: 55213

They should be B and Parameter[], since B is the erasure of T and varargs are implemented as arrays:

Method method = a.getClass().getDeclaredMethod(
        "call",
        B.class,
        Parameter[].class
);

Note that you have a syntax error: <T extends of B> should be <T extends B>.

Also note that the method as you're showing it doesn't need to be generic at all. This would work just as well:

private void call(B object, Parameter... parameters) { ... }

Upvotes: 7

Related Questions