users@jersey.java.net

[Jersey] Annotations ordering

From: Domenico Briganti <dometec_at_gmail.com>
Date: Tue, 31 Jan 2012 22:54:31 +0100

Hi all,

I have a setter method for HttpServletRequest with two annotation, the
@Context for Jersey injection and @NoLog to not logger this call:


        @NoLog
        @Context
        public void setRequest(HttpServletRequest request) {
                this.request = request;
        }

There is a sneaky behaviour that impact on ordering of annotations.
Eg. with this order, jersey doesn't inject the HttpServletRequest:

        @Context
        @NoLog
        public void setRequest(HttpServletRequest request) {
                this.request = request;
        }

I think that order of annotations should not be influential.
The problem is when jerser get the annotations from class,
http://java.net/projects/jersey/sources/svn/content/tags/jersey-1.11/jersey/jersey-server/src/main/java/com/sun/jersey/server/impl/modelapi/annotation/IntrospectionModeller.java?rev=5604
line 583.
That loop get only the last annotation for paramAnnotation.
I think that the loop can be changed adding an if on last else, as:



        for (Annotation annotation : annotations) {
            if
(ANOT_HELPER_MAP.containsKey(annotation.annotationType())) {
                ParamAnnotationHelper helper =
ANOT_HELPER_MAP.get(annotation.annotationType());
                paramAnnotation = annotation;
                paramSource = helper.getSource();
                paramName = helper.getValueOf(annotation);
            } else if (Encoded.class == annotation.annotationType()) {
                paramEncoded = true;
            } else if (DefaultValue.class ==
annotation.annotationType()) {
                paramDefault = ((DefaultValue) annotation).value();
            } else if (paramAnnotation == null) {
                paramAnnotation = annotation;
                paramSource = Source.UNKNOWN;
                paramName = getValue(annotation);
            }
        }

What do you think?

Thank you,
Domenico