繁体   English   中英

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

[英]Are Java meta-annotations available at runtime?

我创建了一个元注释并将其应用于注释,但无法找到任何方法来查找在运行时与注释关联的元注释。 这在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”注释的方法很简单,但是一旦掌握了Annotation,我怎样才能在运行时找出该注释是否具有相关的元注释(即“过滤器”)?

我的答案已经很抱歉:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM