Inheriting class annotations
Asked Answered
D

5

20

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 ? */
}
Disrate answered 24/8, 2011 at 9:59 Comment(0)
C
10

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

Cypriot answered 24/8, 2011 at 10:3 Comment(2)
The annotation processor is a little outside the scope of this particular change but I'll try simply out of curiousity. I was more confused by the lack of inheritance in a java class, seeing as the understanding I had of annotations was they are pretty much compile time meta.Disrate
Link is broken, this is now not an answerChildbed
A
22

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.

Analysand answered 28/10, 2011 at 13:29 Comment(1)
Important caveat from the above linked documentation: 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.Fatwitted
C
10

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

Cypriot answered 24/8, 2011 at 10:3 Comment(2)
The annotation processor is a little outside the scope of this particular change but I'll try simply out of curiousity. I was more confused by the lack of inheritance in a java class, seeing as the understanding I had of annotations was they are pretty much compile time meta.Disrate
Link is broken, this is now not an answerChildbed
D
7

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.

Dry answered 24/8, 2011 at 10:12 Comment(1)
thanks, at least I know it'll be something to look forward to when upgrading to EJB 3.1 ;)Disrate
P
5

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.

Pierrette answered 24/8, 2011 at 10:7 Comment(1)
As its only the class level annotation I was after, to make all sublclasses of AbstractBeanActionException cause a rollback I'll annotate the subclasses.Disrate
P
2

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.

Popsicle answered 24/8, 2011 at 15:44 Comment(1)
see JB Nizet 's for a similar answer, inherited is added in 3.1Disrate

© 2022 - 2024 — McMap. All rights reserved.