开发者

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.


Did you try:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜