JDK-6569633 : Varargs: parser error when varargs element type is an array
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2007-06-14
  • 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 b128Fixed
Related Reports
Relates :  
Description
Description:
    As per Section 8.4.1 (Formal Parameters) in JLS 3.0, the grammar specified for variable arity parameter is as follows:

    LastFormalParameter:
    	VariableModifiers Type...opt VariableDeclaratorId
    	FormalParameter
    	
    VariableDeclaratorId:
    	Identifier
    	VariableDeclaratorId [ ]

The following code which seems to be valid as per the grammar throws Compilation error:

<code>
public  class VarArgs 
{
  void  method1 (Integer... var3[] ){
  }
 
}

</code>

Compilation result is :
<output>
VarArgs.java:3: ')' expected
  void  method1 (Integer... in[]){
                              ^
VarArgs.java:3: illegal start of type
  void  method1 (Integer... in[]){
                               ^
VarArgs.java:3: <identifier> expected
  void  method1 (Integer... in[]){
                                ^
VarArgs.java:3: ';' expected
  void  method1 (Integer... in[]){
                                 ^
VarArgs.java:7: class, interface, or enum expected
}
^
5 errors


</output>
There isnt any mention in the JLS that [] should not appear at the end of the declaration for a variable arity parameter.
The following code compiles without error as expected:

public  class VarArgs 
{
  void  method1 (Integer []... var3 )  {
  }
 
}

Tried in :
<version>
bash-3.00$ java -version
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b13)
Java HotSpot(TM) Client VM (build 1.7.0-ea-b13, mixed mode)

bash-3.00$ uname -a
SunOS hrajan 5.10 Generic sun4u sparc SUNW,Sun-Blade-100
</version>

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/812c6251ea78
24-01-2011

WORK AROUND This syntax will work just fine: class VarArgs { void method1 (Integer []... var3 ) { ... } }
09-12-2010

EVALUATION .
18-11-2010

EVALUATION The type of a variable arity parameter can be an array type, e.g. 'Integer[]... x'. JLS3 allows 'Integer[]... x' and its legacy equivalent, 'Integer... x[]'. However, javac and Eclipse never supported the legacy equivalent for a variable-arity parameter. They required an Identifier at the end, not a VariableDeclaratorId. So while this is a compiler bug today, it shouldn't be. A spec change (6998735) is in order.
09-11-2010