2013-04-22 68 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