2017-05-02 25 views
-6

正如你所看到的,我有3個不同的註釋類。除了班級名稱外,所有3個都是相同的。當我要求Tree.getAnnotations()時,唯一顯示的是註釋@Element只有一個註釋顯示

@Element(name = "node") 
public class Tree<T> { 

    private Tree<T>[] children = null; 
    private T value; 

    public Tree(T v, Tree<T>[] trees){ 
     children = trees; 
     value = v; 
    } 

    public Tree(T v){ 
     value = v; 
    } 

    @SubElements(name = "subnodes") 
    public Tree<T>[] getChildren(){ 
     return children; 
    } 

    @ElementField(name = "value") 
    public T getValue(){ 
     return value; 
    } 

} 

這是註釋類:

import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 

@Retention(RetentionPolicy.RUNTIME) 
public @interface Element { 

    String name(); 

} 
+6

請不要鏈接截圖,但直接發佈代碼 –

+0

很抱歉,我不知道該怎麼做 –

+2

@AntonGustafsson複製粘貼? – assylias

回答

1

類確實只有一個註解。另外兩個屬於類方法。爲了找到他們,你可以這樣做:

Method getValue = Tree.class.getMethod("getValue"); //get the method 
getValue.getAnnotations(); //get annotations, only ElementField in this case 

每隔一個方法都一樣。

+0

因此,我應該如何訪問他們?第一次使用這個。 –

+1

@AntonGustafsson編輯添加示例 – kaqqao

+0

@kaqqao謝謝你,我的雙手充滿了工作 – cristianhh