2010-07-14 45 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)

相关问题