Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6730476

invalid "unchecked generic array" warning

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 6
    • tools
    • b94
    • x86
    • linux, windows_xp
    • Verified

      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();

            mcimadamore Maurizio Cimadamore
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: