users@jersey.java.net

Re: [Jersey] Re: Inheritance of Annotations and Jersey?

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 8 Jul 2009 17:13:48 -0700

On Wed, Jul 8, 2009 at 6:24 AM, Martin Probst<mail_at_martin-probst.com> wrote:
>> I just discovered (...in the hard way...) that annotations are not
>> inherited in JAX-RS/Jersey.
>
> To be a bit more precise, there is the meta-annotation @Inherited,
> which is present on @Produces and on @Consumes, but not on the other
> jax-rs annotations.
>
> But @Inherited is not sufficient to really get transparent inheritance
> of annotations, as it doesn't work with implementing interfaces, and
> it only works for annotations on classes, not for annotations on
> methods.
>
> Pretty weird if you ask me.

You could actually think of it as a design flaw of the way Java
annotations work.
So for frameworks to provide method-annotation inheritance, they have
to implement it explicitly, possibly using other libraries (there is
at least one, but whose name escapes me right now -- google for 'java
annotation inheritance' or something).

For example: Jackson json processor does implement full method
annotation inheritance; including inheritance from interface and
mixing/matching them (i.e. different annotations from different method
overrides). Plus side is that it is possible to do something I call
"mix-in annotations" -- specify virtual overrides for class/method
annotations, to "tag" classes/interfaces with additional annotations
(which can still be overridden by sub-classes). Why is this important?
Because it is then possible to associate annotations without modifying
actual classes: necessary when dealing with classes from other third
party libraries.

-+ Tatu +-