JDK-6404194 : javac parser generates incorrect end position for annotations with parentheses.
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-03-26
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b03Fixed
Related Reports
Relates :  
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
EVALUATION Should be fixed using the supplied patch.
21-04-2006

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; }
26-03-2006

WORK AROUND None possible, since parentheses are optional for empty annotations.
26-03-2006