JDK-6337964 : should ignore last comma in annotation array
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0,6,6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2003,windows_xp
  • CPU: x86
  • Submitted: 2005-10-17
  • Updated: 2012-01-13
  • Resolved: 2012-01-13
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 Availabitlity Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u30Fixed 7 b74Fixed
Related Reports
Duplicate :  
Relates :  
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
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

EVALUATION A fine idea.
2009-09-24