具有通用类类型的Java反射getDeclaredMethod()

use*_*555 4 java generics reflection

我在A班有一个方法:

class Parameter {
...
}

class A {
   private <T extends B> void call(T object, Parameter... parameters){
   ...
   }
}
Run Code Online (Sandbox Code Playgroud)

现在我想使用反射来获取方法"call",

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

谢谢.

Pau*_*ora 6

他们应该是BParameter[],因为B擦除T和可变参数作为数组实现的:

Method method = a.getClass().getDeclaredMethod(
        "call",
        B.class,
        Parameter[].class
);
Run Code Online (Sandbox Code Playgroud)

请注意,您有语法错误:<T extends of B>应该是<T extends B>.

另请注意,您显示它的方法根本不需要是通用的.这也可以:

private void call(B object, Parameter... parameters) { ... }
Run Code Online (Sandbox Code Playgroud)