JDK-6730476 : invalid "unchecked generic array" warning
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2008-07-28
  • 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 b94Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Windows XP

A DESCRIPTION OF THE PROBLEM :
The compiler should not issue a warning, because it actually infers the correct type. It does not issue an error, which means that it accepts the assignment of the result of Arrays.asList() to a variable of type List<Integer> without a cast, which means that it correctly inferred the type Integer for the type parameter T of method Arrays.asList().

I stumbled upon this while reading "Java Generics and Collections" by Wadler and Naftalin. Page 12 contains an example similar to this one.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac -Xlint:unchecked Test.java

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should compile without warnings.
ACTUAL -
Compiler prints:

Test.java:2: warning: [unchecked] unchecked generic array creation of type T[] for varargs parameter
  java.util.List<Integer> ints = java.util.Arrays.asList();
                                                        ^
1 warning

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Test {
  java.util.List<Integer> ints = java.util.Arrays.asList();
}

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

CUSTOMER SUBMITTED WORKAROUND :
Supply explicit type argument:

java.util.List<Integer> ints = java.util.Arrays.<Integer>asList();

Comments
SUGGESTED FIX A webrev of this fix is available at the following URL: http://hg.openjdk.java.net/jdk7/tl/langtools/rev/04cf82179fa7
22-04-2010

EVALUATION Another bug due to the fact that javac does not subsitute type parameters inferred from 15.12.2.8 back into the method signature before checking for method applicability. In this case the problem is that javac is seeing a method call to Arrays.asList(T[]) instad of Array.asList(Integer[]) as it should be. The first call is then flagged as unchecked by javac, as it requires a generic array creation. If javac substituted the inferred type Integer for T into the method signature, this problem would go away, since Integer is reifiable, and so is Integer[]. The issue in this CR is related to the one described in CR 6638712. The fix for 6638712 also fix this issue.
28-07-2008