简体   繁体   English

运行时的Java元注释

[英]Java Meta Annotations at Runtime

I have a java meta annotation as such 我有一个java元注释

@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 在运行时,我可以使用提取LeafEntity注释

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. 这很重要的原因是我有一个名为Qualifier的基本注释。 I would like to allow my users to define any annotation they like simply applying the Qualifier annotation to it. 我想允许我的用户定义他们喜欢的任何注释,只需将Qualifier注释应用于它。 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? 难道你不能给Qualifier一个默认值吗? This way you'll be able to extract it. 这样你就可以提取它。

public @interface Qualifier { 
    boolean value default true;
}

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

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