Java Reflection getDeclaredMethod () with a common 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 call method,

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

thanks.

+4
source share
1 answer

They must be B and Parameter[] , since B is erasure 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> must be <T extends B> .

Also note that the method you are showing does not have to be general. This will work just as well:

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

Source: https://habr.com/ru/post/1498695/


All Articles