- A Concurrent Affair - https://www.concurrentaffair.org -

@Inherited for Invariant Annotations?

When I wrote the API code for xajavac [1], my Java compiler variant that allows subtyping for annotations, I discovered the @Inherited [2] annotation for the first time. This annotation is used on another annotation and indicates that the other annotation should be inherited from superclasses to subclasses. Specifically, annotations with @Inherited also appear in the array of annotations returned by Class.getAnnotations() [3] if they annotated any of the superclasses of the class in question; in contrast, the array returned by Class.getDeclaredAnnotations only returns the annotations that are actually annotation the class itself.

When I wrote the implementation of the invariant checker outlined in my Master’s thesis, I implemented and described an inheritance mechanism for these invariant annotations that didn’t rely on the @Inherited annotation. I introduced separates meta-annotation, @PredicateLink and @Combine, which I needed anyway and which also incorporated the meaning of @Inherited.

Now that I have discovered @Inherited, I’m wondering whether I could or even should use it. That would mean making non-inheritance the default behavior, and letting @Inherited imply the inheriting case. I don’t think that’s the behavior I want. A @NotInherited meta-annotation would be better.

[4] [5]Share [6]