rest icon indicating copy to clipboard operation
rest copied to clipboard

Enhancement Proposal: ResourceInfo.getAnnotation(Class annotationClass)

Open mkarg opened this issue 1 year ago • 9 comments

I hereby propose the addition of the following method:

public <A extends Annotation> A ResourceInfo.getResourceAnnotation(Class<A> annotationClass)

The idea is that this method returns the annotation instance of the given annotationClass with full respect to the rules found in spec chapter 3.6. This effectively means, this method returns either the super class annotation, or the interface annotation, or the resource class annotation, or the resource method annotation, depending on whether any JAX-RS annotation is found in the particular deeper levels.

The benefit of this method is that there is no need for filters / interceptors to re-implement this algorithm again and again for custom annotations, in case the annotation author wants to apply the same rules as found in chapter 3.6 (hence: the new annotation shall "look and feel" like an official JAX-RS annotation). Implementations could make the already existing algorithm publicly available here, so it is guaranteed that the exact same treatment happens for custom annotations and for official annotations.

mkarg avatar Nov 20 '24 10:11 mkarg

Kindly asking for more opinions, in particular from committers.

If nobody vetos ResourceInfo.getResourceAnnotations upfront, I will provide a PR and compliant implementation (PR for Jersey) in the next weeks.

mkarg avatar Nov 25 '24 06:11 mkarg

There had been not upfront vetos, so I assume everybody is fine with adopting this enhancement. If not, please clearly tell me NOW. Thanks.

mkarg avatar Dec 17 '24 23:12 mkarg

When moving to CDI, I hope the annotation operations can be by CDI utilities directly. Currently, almost every spec has some util to handle the reflection-related operations.

hantsy avatar Jan 04 '25 01:01 hantsy

Should there ba a getResourceClassAnnotations() and/or getResourceMethodAnnotations()?

jamezp avatar Jan 04 '25 01:01 jamezp

@mkarg Thanks for bringing this up. I like the idea. We actually had to reimplement the annotation inheritance rules for the MVC spec implementation as well. So +1 for this idea. Just one though: Shouldn't it be getResourceAnnotation instead of getResourceAnnotations (so singular instead of plural)?

Should there ba a getResourceClassAnnotations() and/or getResourceMethodAnnotations()?

I personally don't see a real benefit of having these. Especially separate ones for class- and method-level annotations.

chkal avatar Jan 04 '25 08:01 chkal

Should there ba a getResourceClassAnnotations() and/or getResourceMethodAnnotations()?

Never had the actual need this separation.

mkarg avatar Jan 04 '25 10:01 mkarg

Shouldn't it be getResourceAnnotation instead of getResourceAnnotations (so singular instead of plural)?

Correct, I ~will fix~ fixed the original description.

mkarg avatar Jan 04 '25 10:01 mkarg

As nobody vetoed, I have sketched a first draft. I think it makes sense to continue our discussion there.

mkarg avatar Jan 06 '25 18:01 mkarg

Seems like a useful addition, as is the suggestion to support lookup for methods as well I think.

spericas avatar Jan 29 '25 21:01 spericas