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

4

我创建了一个元注解并将其应用于注解,但无法找到任何方法在运行时查找与注解相关联的元注解。 JDK 6是否支持此功能?

例如:

/**
 * 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”注解的方法很简单,但是一旦我得到了该注解,如何在运行时找出它是否具有相关联的元注解(例如,“Filter”)?

1个回答

4

我已经有答案了,抱歉:

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

这行代码与注解和过滤器相关。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接