United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6337964 should ignore last comma in annotation array
JDK-6337964 : should ignore last comma in annotation array

Details
Type:
Bug
Submit Date:
2005-10-17
Status:
Closed
Updated Date:
2012-01-13
Project Name:
JDK
Resolved Date:
2012-01-13
Component:
tools
OS:
windows_2003,windows_xp
Sub-Component:
javac
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0,6,6u10
Fixed Versions:

Related Reports
Backport:
Backport:
Duplicate:
Relates:

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

A DESCRIPTION OF THE PROBLEM :
According to the JLS3 (see
http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.7).

    NormalAnnotation:
        @ TypeName ( ElementValuePairsopt )

    ElementValuePairs:
        ElementValuePair
        ElementValuePairs , ElementValuePair

    ElementValuePair:
        Identifier = ElementValue

    ElementValue:
        ConditionalExpression
        Annotation
        ElementValueArrayInitializer

    ElementValueArrayInitializer:
        { ElementValuesopt ,opt }

    ElementValues:
        ElementValue
        ElementValues , ElementValue

So a ElementValueArrayInitializer can have an optional comma before the closing
brace. If present, it is simply ignored.

javac does not allow a comma after the last entry.

Original bug reported at: -
https://bugs.eclipse.org/bugs/show_bug.cgi?id=112433

STEPS TO REPRODUCE :
Just try and compile the sample test case below (taken from annotations sample from java.sun.com)

---------- BEGIN SOURCE ----------
// Test.java
public class Test {
	@RequestForEnhancement(
    		id       = 23,
	        synopsis = "Sample Synopsis",
           	engineer = "Test",
           	date     = "9/1/2007",
	    )
	public static void main(String[] args) {
	}
}

/**
 * Describes the Request-For-Enhancement(RFE) that led
 * to the presence of the annotated API element.
 */
public @interface RequestForEnhancement {
    int    id();
    String synopsis();
    String engineer() default "[unassigned]"; 
    String date()    default "[unimplemented]"; 
}


---------- END SOURCE ----------


REPRODUCIBILITY :
This bug can be reproduced always.

                                    

Comments
EVALUATION

A fine idea.
                                     
2009-09-24
PUBLIC COMMENTS

See
http://hg.openjdk.java.net/jdk7/tl/langtools/rev/d0f541480556
                                     
2009-09-24
SUGGESTED FIX

--- old/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	2009-09-24 14:11:58.000000000 -0700
+++ new/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	2009-09-24 14:11:58.000000000 -0700
@@ -2236,7 +2236,7 @@
 
     /* AnnotationValue          = ConditionalExpression
      *                          | Annotation
-     *                          | "{" [ AnnotationValue { "," AnnotationValue } ] "}"
+     *                          | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}"
      */
     JCExpression annotationValue() {
         int pos;
@@ -2253,7 +2253,7 @@
                 buf.append(annotationValue());
                 while (S.token() == COMMA) {
                     S.nextToken();
-                    if (S.token() == RPAREN) break;
+                    if (S.token() == RBRACE) break;
                     buf.append(annotationValue());
                 }
             }
--- /dev/null	2009-08-12 17:12:33.000000000 -0700
+++ new/test/tools/javac/annotations/pos/TrailingComma.java	2009-09-24 14:11:59.000000000 -0700
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6337964
+ * @summary javac incorrectly disallows trailing comma in annotation arrays
+ * @author darcy
+ * @compile TrailingComma.java
+ */
+
+import java.lang.annotation.*;
+
+@interface TestAnnotation {
+    SuppressWarnings[] value() default {@SuppressWarnings({"",})};
+}
+
+
+@TestAnnotation({@SuppressWarnings(),
+                 @SuppressWarnings({"Beware the ides of March.",}),
+                 @SuppressWarnings({"Look both ways", "Before Crossing",}), })
+public class TrailingComma {
+}
                                     
2009-09-24



Hardware and Software, Engineered to Work Together