5

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.

2
  • 1
    Class a = new A(); is not valid. You either mean Class a = new A().getClass(); or A a = new A();. Commented Aug 24, 2013 at 5:11
  • sorry, I mean A a = new A(). Commented Aug 24, 2013 at 7:46

1 Answer 1

7

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) { ... }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, It works! I also have updated the syntax error. Btw, I wonder why B.class works here and what Parameter[].class means.
@user537555 Glad to help! .class returns the Class literal representing that type. It works for array types like Parameter[].class and even primitive types like int.class (that's how you can look up a method taking an int).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.