继承类注解
有没有办法让类从超类继承注释?
例如
@ApplicationException(rollback=true)
public abstract class AbstractBeanActionException extends Exception {
/* method body is simply calls to super() */
}
public class OrderBeanException extends AbstractBeanActionException {
/* does this class have to be annotated as well ? */
}
Is there a way to make classes inherit annotations from a superclass ?
e.g.
@ApplicationException(rollback=true)
public abstract class AbstractBeanActionException extends Exception {
/* method body is simply calls to super() */
}
public class OrderBeanException extends AbstractBeanActionException {
/* does this class have to be annotated as well ? */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您自己定义注释类,则可以使用
@Inherited
元注释:If you define your annotations classes yourself, then you can use
@Inherited
meta annotation:类注解不能被子类继承。
您可以做的是“强制”子类在编译时使用注释:
https:// Community.oracle.com/docs/DOC-983563
Class annotations can not be inherited by subclasses.
What you can do is "force" the subclass to use the annotation at compile time:
https://community.oracle.com/docs/DOC-983563
注释不会被继承。但是使用注释的框架(在本例中为 EJB3)可以选择在类层次结构中导航以查看它是否存在于超类中。
查看此注释的javadoc:它有一个
inherited
属性,该属性准确地指示此注释是否也应应用于子类。Annotations are not inherited. But the framework using the annotation (in this case, EJB3), can choose to navigate through the class hierarchy to see if it exists on a superclass.
Look at the javadoc of this annotation: It has an
inherited
property which, precisely, indicates if this annotation should also be applied to subclasses or not.类注解不能被子类继承,但非私有非构造函数成员方法和字段上的注解以及它们关联的方法/字段可以继承。所以你可以尝试使用这些来达到你想要的效果。
Class annotations can not be inherited by subclasses, but annotations on nonprivate non-constructor member methods and fields are inherited, together with the method / field they are associated with. So you may try using these to achieve the desired effect.
当一个类标记有一个注释,而该注释本身又用 java.lang.annotation.Inherited 注释时,您可以询问该类上的注释,并且 Inherited 注释应该显示在结果中。
ApplicationException 未标记为 Inherited。啊好吧。
When a class is marked with an annotation that is itself annotated with java.lang.annotation.Inherited, you can ask for the annotations that are on the class and the Inherited ones should show up in the results.
ApplicationException is not marked as Inherited. Ah well.