Java元注释是否在运行时可用?

时间:2010-07-14 12:36:54

标签: java metadata annotations

我创建了一个元注释并将其应用于注释,但无法找到任何方法来查找在运行时与注释关联的元注释。这实际上是否支持JDK 6?

E.g:

/**
 * 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,我怎样才能在运行时找出该注释是否具有相关的元注释(即“过滤器”)?

1 个答案:

答案 0 :(得分:3)

我的答案已经很抱歉:

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