United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6404194 javac parser generates incorrect end position for annotations with parentheses.
JDK-6404194 : javac parser generates incorrect end position for annotations with parentheses.

Details
Type:
Bug
Submit Date:
2006-03-26
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
generic
Sub-Component:
javac
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
The javac parser stores the end position for annotations, which cannot be heuristically determined because parentheses can be optional in an annotation expression.  However, it always stores the end position of the annotation's identifier, rather than the closing parenthesis when parentheses are present:

   @SuppressWarning("foo")
------------------^         current endPos stored in endPositions
-------------------------^  correct endPos

The problem is that com.sun.tools.javac.parser.Parser.annotation() stores the identifier's end position before parsing any fields, then stores that value regardless of whether there were any parentheses.  The suggested fix checks whether the next token is a left-parenthesis, and if so uses the last token's end position, otherwise the stored value.  Although there isn't a test case, this fix was verified with Jackpot.

                                    

Comments
WORK AROUND

None possible, since parentheses are optional for empty annotations.
                                     
2006-03-26
SUGGESTED FIX

Index: Parser.java
===================================================================
RCS file: /cvs/retouche/retouche/Jsr199/src/com/sun/tools/javac/parser/Parser.java,v
retrieving revision 1.25
diff -c -r1.25 Parser.java
*** Parser.java 10 Mar 2006 08:24:52 -0000      1.25
--- Parser.java 26 Mar 2006 00:03:26 -0000
***************
*** 1931,1940 ****
        // accept(AT); // AT consumed by caller
          checkAnnotations();
          JCTree ident = qualident();
!         int endPos = S.prevEndPos();
          List<JCExpression> fieldValues = annotationFieldValuesOpt();
        JCAnnotation ann = F.at(pos).Annotation(ident, fieldValues);
!       storeEnd(ann, endPos);
        return ann;
      }

--- 1931,1941 ----
        // accept(AT); // AT consumed by caller
          checkAnnotations();
          JCTree ident = qualident();
!         int identEndPos = S.prevEndPos();
!         boolean hasParens = S.token() == LPAREN;
          List<JCExpression> fieldValues = annotationFieldValuesOpt();
        JCAnnotation ann = F.at(pos).Annotation(ident, fieldValues);
!       storeEnd(ann, hasParens ? S.prevEndPos() : identEndPos);
        return ann;
      }
                                     
2006-03-26
EVALUATION

Should be fixed using the supplied patch.
                                     
2006-04-21



Hardware and Software, Engineered to Work Together