這聽起來像你只需要使用getMethod
而不是getDeclaredMethod
。的getDeclaredMethod
整點是,它僅發現在類中聲明你調用它的方法:
返回反映此Class對象表示的類或接口的指定已聲明方法的Method對象。
鑑於getMethod
具有:
C被搜索的任何匹配的方法。如果找不到匹配的方法,步驟1的算法是遞歸調用上C.
的超這隻會發現公共方法雖然。如果你追求的方法是不公開的,你應該自己遞歸起來類層次結構,層次結構中的每個類使用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();
}
+1,一個更好的解決方案比我早。 – rgettman
如果有幫助,我使用這個的原因是由於該方法首先是私人的。然後你編輯。好的,我會嘗試編輯。 – PaulBGD
@Ultimate:嗯,這很重要。如果你剛纔提到過,這會有所幫助。 –