I have created a meta-annotation and applied it to an annotation but can't find any way to find what meta-annotations are associated with the annotation at runtime. Is this actually supported in JDK 6?
Eg:
/**
* 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()
{
}
}
It's simple to find methods with the "TypeOne" annotation, but once I have that Annotation in hand, how can I, at runtime, find out if that annotation has as associated meta-annotation (ie, "Filter")?
我的答案已经很抱歉:
annotation.annotationType().isAnnotationPresent(Filter.class)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.