我正在探索注释,并发现某些注释似乎具有层次结构。
我正在使用注释在卡片后台生成代码。有不同的卡片类型(因此有不同的代码和注释),但它们之间有一些共同的元素,例如名称。
@Target(value = {ElementType.TYPE})
public @interface Move extends Page{
String method1();
String method2();
}
这将是常见的注释:
@Target(value = {ElementType.TYPE})
public @interface Page{
String method3();
}
在上面的示例中,我希望 Move 继承 method3,但我收到一条警告,指出扩展对于注释无效。我试图让注释扩展一个公共基础注释,但这不起作用。这是否可能,或者只是一个设计问题?
I'm exploring annotations and came to a point where some annotations seems to have a hierarchy among them.
I'm using annotations to generate code in the background for Cards. There are different Card types (thus different code and annotations) but there are certain elements that are common among them like a name.
@Target(value = {ElementType.TYPE})
public @interface Move extends Page{
String method1();
String method2();
}
And this would be the common Annotation:
@Target(value = {ElementType.TYPE})
public @interface Page{
String method3();
}
In the example above I would expect Move to inherit method3 but I get a warning saying that extends is not valid with annotations. I was trying to have an Annotation extends a common base one but that doesn't work. Is that even possible or is just a design issue?
发布评论
评论(5)
不幸的是,没有。显然,它与读取类上的注释而不完全加载它们的程序有关。请参阅为什么无法在 Java 中扩展注释?
但是,如果这些注释是
@Inherited
。另外,除非您需要这些方法进行交互,否则您可以将注释堆叠在您的类上:
是否有某些原因对您不起作用?
Unfortunately, no. Apparently it has something to do with programs that read the annotations on a class without loading them all the way. See Why is it not possible to extend annotations in Java?
However, types do inherit the annotations of their superclass if those annotations are
@Inherited
.Also, unless you need those methods to interact, you could just stack the annotations on your class:
Is there some reason that wouldn't work for you?
您可以使用基本注释而不是继承来注释您的注释。这是使用在 Spring 框架中。
举个例子
,然后您可以使用 Spring 的 AnnotationUtils:
该方法实现为:
AnnotationUtils
还包含用于搜索方法和其他带注释元素的注释的附加方法。 Spring 类也足够强大,可以搜索桥接方法、代理和其他特殊情况,特别是在 Spring 中遇到的情况。You can annotate your annotation with a base annotation instead of inheritance. This is used in Spring framework.
To give an example
You can then check if a class is annotated with
Vehicle
using Spring's AnnotationUtils:This method is implemented as:
AnnotationUtils
also contains additional methods for searching for annotations on methods and other annotated elements. The Spring class is also powerful enough to search through bridged methods, proxies, and other corner-cases, particularly those encountered in Spring.除了 Grygoriys 的注释注释答案之外。
您可以检查例如包含
@Qualifier
注释(或用@Qualifier
注释的注释)通过此循环:您基本上所做的就是获取所有上的注释方法和这些注释中,您可以获得它们的类型,并检查这些类型是否用 @Qualifier 注释。您的注释还需要启用 Target.Annotation_type 才能使其正常工作。
In addition to Grygoriys answer of annotating annotations.
You can check e.g. methods for containing a
@Qualifier
annotation (or an annotation annotated with@Qualifier
) by this loop:What you're basically doing, is to get all annotations present on the method and of those annotations you get their types and check those types if they're annotated with @Qualifier. Your annotation needs to be Target.Annotation_type enabled as well to get this working.
查看 https://github.com/blindpirate/annotation-magic ,这是我的一个库当我有同样的问题时开发。
Check out https://github.com/blindpirate/annotation-magic , which is a library I developed when I had the same question.
你可以像这样@nnotate你的@interface:
You can @nnotate your @interface like this: