简体   繁体   中英

Java Meta Annotations at Runtime

I have a java meta annotation as such

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.ANNOTATION_TYPE })
public @interface Qualifier {
}

I then have another annotation:

@Qualifier
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
public @interface LeafEntity {
   String name() default "";
}

At runtime I can extract the LeafEntity annotation using

getClass().getAnnotation(LeafEntity.class);

However, I would like to know if it is possible to access anything about the Qualifier? I mean what is the purpose of annotations on annotations if you can't access anything about them?

I have tried all of the following:

getClass().getAnnotation(Qualifier.class);
getClass().getAnnotation(LeafEntity.class).getClass().getAnnotation(Qualifier.class);
getClass().getAnnotation(LeafEntity.class) instanceof Qualifier.class;

If someone knows how to access the Qualifier annotation I would appreciate an example..

The reason this is important is that I have a base annotation called Qualifier. I would like to allow my users to define any annotation they like simply applying the Qualifier annotation to it. Then I would scan the class for any annotation that was annotated by the Qualifier annotation. But that's impossible if I can't access or identify Qualifier annotations.

Thanks in Advance.

你试过了吗:

getClass().getAnnotation(LeafEntity.class).annotationType().getAnnotation(Qualifier.class);

Couldn't you give Qualifier a value with a default? This way you'll be able to extract it.

public @interface Qualifier { 
    boolean value default true;
}

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.

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