2010-07-14 63 views
3

我創建了一個元註釋並將其應用於註釋,但無法找到任何方式來查找哪些元註釋在運行時與註釋相關聯。這在JDK 6中實際支持嗎?在運行時可以使用Java元註釋嗎?

如:

/** 
* Meta-annotation for other annotations declaring them to be "filter" annotations 
*/ 
@Target(ElementType.ANNOTATION_TYPE) // make this a meta-annotation 
@Retention(RetentionPolicy.RUNTIME) // available at runtime 
public @interface Filter 
{ 
} 

/** 
* Marks a test as only being applicable to TypeOne clients 
*/ 
@Target({ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Filter 
public @interface TypeOne 
{ 
} 

public class MyClientClass 
{ 
    // Would like to scan this method to see if the method has an Annotation that is of meta-type "Filter" 
    @TypeOne 
    public void testMethod() 
    { 
    } 
} 

是很簡單的找到了「TypeOne」註釋方法,但一旦我有註釋的手,我怎麼能在運行時,發現如果註釋具有相關聯的元-annotation(即「過濾器」)?

回答

3

我有了答案已經不好意思:

annotation.annotationType().isAnnotationPresent(Filter.class)

相關問題