简体   繁体   English

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

[英]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.WebServicejavax.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.

相关问题 Java实例抽象类,实现接口 - Java instance abstract class, which implements interface 实现泛型接口的Java泛型类 - Java Generic Class which Implements a Generic Interface Java注释-如何检查带注释的元素是否实现了接口? - Java Annotations - How can you check if an annotated element implements an interface? Java:从实现接口的类访问接口函数 - Java: Accessing a interface function from a class which implements the interface 我可以使用scala类来实现Java的Java接口吗? - Can I use a scala class which implements a java interface from Java? 为什么我不能从Scala访问实现Java接口的类中声明的变量? - Why can't I access a variable declared in a class, which implements a Java interface, from Scala? 如何从实现我的接口的JAR归档中加载类? (java) - How can I load a class from a JAR-archive which implements my interface? (java) 是否等于Java类中的实现,该类实现了扩展Iterable的接口? - Equals implementation in a java-class which implements an interface that extends Iterable? 如何创建实现接口的抽象类对象(JAVA) - How to create abstract class object which implements interface ( JAVA ) IllegalArgumentException 当传入一个在java中实现接口的类时反射 - IllegalArgumentException when passing in a class which implements an interface in java reflect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM