实现接口的Java类可以自动继承注释吗?

发布于 2024-11-29 03:46:27 字数 587 浏览 1 评论 0原文

假设我有一个带有一些注释的接口,例如:

@SpecialClass
public interface IFoo { /* ... */ }

假设我创建一个实现该接口的类:

public class Foo implements IFoo { /* ... */ }

Foo 是否有可能以某种方式“继承”或自动复制全部或部分内容来自 IFoo 及其成员的注释(例如自动将 Foo 注释为 @SpecialClass 等)?

这对于实现 Web 服务类(例如,由 JAX-WS“wsimport”工具生成的类)来说非常方便,只需实现其带注释的接口,而无需显式地将接口注释复制到实现类(例如 javax.jws. WebServicejavax.xml.ws.RequestWrapper 等)。

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:

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 (e.g. automagically annotate Foo as @SpecialClass, etc.)?

This would be convenient for implementing web service classes (e.g. 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 (e.g. javax.jws.WebService, javax.xml.ws.RequestWrapper, etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九局 2024-12-06 03:46:27

编辑:我将这个答案留在这里供一般信息和未来的读者使用,但 Andreas 指出了我错过的 Javadoc 的重要部分:

请注意,如果带注释的类型用于注释类以外的任何内容,则此元注释类型无效。另请注意,此元注释只会导致注释从超类继承;已实现接口上的注释不起作用。

换句话说,在这种情况下它不会有帮助。当然,只有当您可以控制注释本身时,它才有用。

我怀疑真正的答案是您只需在各处应用注释即可。如果您担心忘记一个,您可能需要编写一个单元测试来查找所有类(我意识到说起来容易做起来难)并检查实现给定接口的所有类是否存在注释。


您是否尝试过应用继承SpecialClass 注释本身进行注释?

表示自动继承注释类型。如果注释类型声明上存在 Inherited 元注释,并且用户在类声明上查询注释类型,并且类声明没有该类型的注释,则将自动查询该类的超类以获取该注释类型。将重复此过程,直到找到该类型的注释,或到达类层次结构的顶部(对象)。如果没有超类具有此类型的注释,则查询将指示相关类没有此类注释。

这当然听起来正是您想要的。

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:

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a 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?

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. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.

That certainly sounds like exactly what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文