2011-04-12 44 views
2

我有一個Java元註解這樣的Java元註解在運行時

@Retention(value = RetentionPolicy.RUNTIME) 
@Target(value = { ElementType.ANNOTATION_TYPE }) 
public @interface Qualifier { 
} 

然後我還有一個註釋:

@Qualifier 
@Retention(value = RetentionPolicy.RUNTIME) 
@Target(value = { ElementType.TYPE }) 
public @interface LeafEntity { 
    String name() default ""; 
} 

在運行時,我可以提取使用

getClass().getAnnotation(LeafEntity.class); 
的LeafEntity註釋

不過,我想知道是否有可能訪問有關限定符什麼?我的意思是,如果您無法訪問註釋,註釋的目的是什麼?

我已經嘗試了所有的以下內容:

getClass().getAnnotation(Qualifier.class); 
getClass().getAnnotation(LeafEntity.class).getClass().getAnnotation(Qualifier.class); 
getClass().getAnnotation(LeafEntity.class) instanceof Qualifier.class; 

如果有人知道如何訪問限定符註解我將不勝感激的例子..

的原因,這是很重要的是,我有一個基地註釋稱爲Qualifier。我希望允許我的用戶定義他們喜歡的任何註釋,只需將Qualifier註釋應用到它。然後,我將掃描該類以查找由Qualifier批註註釋的任何批註。但是如果我無法訪問或識別Qualifier註釋,這是不可能的。

在此先感謝。

回答

4

你嘗試:

getClass().getAnnotation(LeafEntity.class).annotationType().getAnnotation(Qualifier.class); 
+0

是的,這是票。謝謝。 – user620165 2011-04-12 10:27:33

1

你能不能說得預選賽默認值?這樣你就可以提取它。

public @interface Qualifier { 
    boolean value default true; 
}