JDK-5107292 : Generic array initialization expressions are impossible
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5.1
  • CPU: x86
  • Submitted: 2004-09-27
  • Updated: 2004-09-28
  • Resolved: 2004-09-28
Related Reports
Duplicate :  
Description

Name: js151677			Date: 09/26/2004


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

ADDITIONAL OS VERSION INFORMATION :
Linux router 2.6.8.1 #1 Mon Aug 16 23:49:22 CEST 2004 i686 athlon i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
The Java language knows the language construct "array initializer", which makes specifying arrays with predetermined content in source code quite efficient. Array initalizers are specified here: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358

Example:

Comparable[]	testArray = {null, null};

Now, with Java1.5, Comparable is a generic type. Thus, after genericification, the above source code line, should read

Comparable<T>[]	testArray = {null, null};

Unfortunately, such a construct is not compileable. There is no workaround which is about as short as the example above. Thus, currently, there is no straightforward genericification of Java sourcecode possible.

This bug is related with bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5105887 , which contains some reasoning about what the compiler should accept without warnings or errors.

However, I consider this bug to be separate, because a whole language construct is unavailable (for the generic case).

I'm happy to provide additional discussion if requested.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the provided source code (e.g.: with "javac com/mn/tools/test/bugs/java/GenericArrayTest2.java")


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The source code should compile flawlessly.
ACTUAL -
> javac com/mn/tools/test/bugs/java/GenericArrayTest2.java
com/mn/tools/test/bugs/java/GenericArrayTest2.java:11: generic array creation
        Comparable<T>[] testArray1 = {null, null};
                                     ^
1 error


ERROR MESSAGES/STACK TRACES THAT OCCUR :
com/mn/tools/test/bugs/java/GenericArrayTest2.java:11: generic array creation
        Comparable<T>[] testArray1 = {null, null};
                                     ^
1 error


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/** GenericArrayTest2.java
*/

package com.mn.tools.test.bugs.java;

/**
	Tests for a compile without warning.
*/
public class GenericArrayTest2<T> {
	Comparable[]	testArray0 = {null, null};
	Comparable<T>[]	testArray1 = {null, null};
}
// :indentSize=8:tabSize=8:

---------- END SOURCE ----------
(Incident Review ID: 315507) 
======================================================================

Comments
EVALUATION Consider using: Comparable<?>[] testArray = {null, null}; ###@###.### 2004-09-28
28-09-2004