所以我想说我正试图从一个类中获取一个方法Method m = plugin.getClass().getDeclaredMethod("getFile");.
但是那个plugin类正在扩展另一个类,即该getFile方法的类.我不太确定是否会使它抛出NoSuchMethodException异常.
我知道plugin扩展的类具有getFile方法.对不起,如果我听起来很混乱,有点累.
Jon*_*eet 65
听起来你只需要使用getMethod而不是getDeclaredMethod.重点getDeclaredMethod是它只找到你在它上面调用它的类中声明的方法:
返回一个Method对象,该对象反映此Class对象所表示的类或接口的指定声明方法.
鉴于getMethod:
搜索C以寻找任何匹配方法.如果没有找到匹配方法,则在C的超类上递归调用步骤1的算法.
那只会找到公共方法.如果您所使用的方法不是公共的,则应使用getDeclaredMethod或getDeclaredMethods在层次结构中的每个类上自行递归类层次结构:
Class<?> clazz = plugin.getClass();
while (clazz != null) {
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
// Test any other things about it beyond the name...
if (method.getName().equals("getFile") && ...) {
return method;
}
}
clazz = clazz.getSuperclass();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21035 次 |
| 最近记录: |