继承类注解

发布于 2024-12-01 07:08:07 字数 347 浏览 0 评论 0原文

有没有办法让类从超类继承注释?

例如

@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 技术交流群。

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

发布评论

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

评论(5

执妄 2024-12-08 07:08:07

如果您自己定义注释类,则可以使用 @Inherited 元注释:

表示自动继承注释类型。如果注释类型声明上存在 Inherited 元注释,并且用户在类声明上查询注释类型,并且类声明没有该类型的注释,则将自动查询该类的超类以获取该注释类型。

If you define your annotations classes yourself, then you can use @Inherited meta annotation:

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.

等待我真够勒 2024-12-08 07:08:07

类注解不能被子类继承。

您可以做的是“强制”子类在编译时使用注释:

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

赴月观长安 2024-12-08 07:08:07

注释不会被继承。但是使用注释的框架(在本例中为 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.

浅笑依然 2024-12-08 07:08:07

类注解不能被子类继承,但非私有非构造函数成员方法和字段上的注解以及它们关联的方法/字段可以继承。所以你可以尝试使用这些来达到你想要的效果。

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.

终止放荡 2024-12-08 07:08:07

当一个类标记有一个注释,而该注释本身又用 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.

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