试试这个:
Method[] method = cls.getClass().getDeclaredMethods();
,而不是
Method[] method = cls.getDeclaredMethods();
见这个例子:
import java.lang.reflect.Method;
public class Example {
private void one() {
}
private void two() {
}
private void three() {
}
public static void main(String[] args) {
Example program = new Example();
Class progClass = program.getClass();
// Get all the methods associated with this class.
Method[] methods = progClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Method " + (i + 1) + " :"
+ methods[i].toString());
}
}
}
输出:
Method 1 :public static void Example.main(java.lang.String[])
Method 2 :private void Example.one()
Method 3 :private void Example.two()
Method 4 :private void Example.three()
这似乎是一个代理对象。如果您不想包含这些方法,则可能需要检查方法的其他属性。或者你使用反射来检查实现的接口? 你怎么得到这个课程? – rmalchow 2013-04-22 10:04:21
请包括类cls。 – javadev 2013-04-22 10:05:31
请看看我编辑的问题,认为这是足够的几分服务,抱歉误导。 – 2013-04-22 10:08:09