[英]Can a Java class which implements an interface inherit the annotations automatically?
Suppose I had an interface with some annotation(s), for example:假设我有一个带有一些注释的接口,例如:
@SpecialClass
public interface IFoo { /* ... */ }
And suppose I make a class that implements the interface:假设我制作了一个实现接口的 class:
public class Foo implements IFoo { /* ... */ }
Is it possible for class Foo
to somehow "inherit" or automatically copy all or some of the annotations from IFoo
and its members (eg automagically annotate Foo
as @SpecialClass
, etc.)? class
Foo
是否有可能以某种方式“继承”或自动复制IFoo
及其成员的所有或部分注释(例如,自动将Foo
注释为@SpecialClass
等)?
This would be convenient for implementing web service classes (eg those generated by the JAX-WS "wsimport" tool) by just implementing their annotated interfaces without explicitly having to copy the interface annotations to the implementing class (eg javax.jws.WebService
, javax.xml.ws.RequestWrapper
, etc).这对于实现 web 服务类(例如,由 JAX-WS “wsimport” 工具生成的服务类)很方便,只需实现它们的注释接口,而无需显式地将接口注释复制到实现 class(例如
javax.jws.WebService
, javax.xml.ws.RequestWrapper
等)。
EDIT: I'm leaving this answer here for general information and future readers, but Andreas pointed out an important bit of the Javadoc which I'd missed:编辑:我将这个答案留给一般信息和未来的读者,但 Andreas 指出了我错过的 Javadoc 的一个重要部分:
Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class.
请注意,如果注释类型用于注释 class 以外的任何内容,则此元注释类型无效。 Note also that this meta-annotation only causes annotations to be inherited from superclasses;
另请注意,此元注释仅导致注释从超类继承; annotations on implemented interfaces have no effect.
已实现接口上的注释无效。
In other words, it wouldn't help in this situation.换句话说,在这种情况下它无济于事。 Also it's only useful if you have control over the annotation itself, of course.
当然,它也只有在您可以控制注释本身时才有用。
I suspect the real answer is that you simply have to apply the annotation everywhere.我怀疑真正的答案是您只需在任何地方应用注释。 If you're worried about forgetting one, you might want to write a unit test which finds all your classes (easier said than done, I realise) and checks that the annotation is present for all classes implementing the given interface.
如果您担心忘记一个,您可能想要编写一个单元测试来查找您的所有类(我意识到说起来容易做起来难)并检查所有实现给定接口的类是否存在注释。
Have you tried applying the Inherited
annotation to the SpecialClass
annotation itself?您是否尝试过将
Inherited
注释应用于SpecialClass
注释本身?
Indicates that an annotation type is automatically inherited.
指示注释类型是自动继承的。 If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type.
如果注解类型声明中存在 Inherited 元注解,并且用户在 class 声明上查询注解类型,并且 class 声明没有此类型的注解,则将自动查询该类的超类以获取注解类型。 This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached.
将重复此过程,直到找到此类型的注释,或到达 class 层次结构(对象)的顶部。 If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.
如果没有超类具有此类型的注释,则查询将指示所讨论的 class 没有此类注释。
That certainly sounds like exactly what you want.这听起来确实是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.