2013-04-22 63 views
0

我使用下面的代码来获取在类中声明的方法。但代码也会返回不必要的值以及声明的方法。获取已声明方法的Java-Annotations

下面是我的代码:

编辑:

​​

我也试图与getMethods()

下面是我的输出:

1:ajc$get$validator 
2:ajc$set$validator 
3:ajc$get$requestToEventTranslator 
4:ajc$set$requestToEventTranslator 
5:ajc$interMethodDispatch2$com_hexgen_api_facade_HexgenWebAPIValidation$validate 
6:handleValidationException 

后,这只我得到的我在课堂上宣布的方法。以上的价值和如何避免它们。

问候

+0

这似乎是一个代理对象。如果您不想包含这些方法,则可能需要检查方法的其他属性。或者你使用反射来检查实现的接口? 你怎么得到这个课程? – rmalchow 2013-04-22 10:04:21

+0

请包括类cls。 – javadev 2013-04-22 10:05:31

+0

请看看我编辑的问题,认为这是足够的几分服务,抱歉误导。 – 2013-04-22 10:08:09

回答

1

试试这个:

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() 
+0

谢谢你的答案Achintya,但现在它提供了很多东西,甚至不是方法等,像'1:newInstance0 2:forName 3:forName 4:forName0'我的实际方法丢失:( – 2013-04-22 10:24:10

+0

@Anto See我的例子可能会有帮助 – 2013-04-22 10:35:51

+0

同样的答案我得到,如果我使用它separtly,但在程序中我不相同,我调查的类是春季班 – 2013-04-22 10:48:52