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

There should be way to create generic arrays without warnings or errors

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      linux

      Description



      Name: rmT116609 Date: 09/22/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 :
      There is no way to efficently create generic arrays without compiler warnings.

      Compiler messages are used to indicate that some source code should be changed (e.g. due to a error). After the change, the compiler warning is not issued anymore. For the creation of generic arrays, no such change is available.

      Additionally, the reasoning in http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf , page 15, is not quite exact:

      A creation of an array of a generic type is not dangerous by itself, if it is just just to set or get elements of that generic type. The danger exists only if the compiler is not able to track the intended generic type of a concrete array. Thus, in the example provided by the generics tutorial,

        List<String>[] lsa = new List<String>[10];
        Object o = lsa;

      not the first line should result in a compile time error, but the second line should. This is, because the generic array type List<String>[] is implicitly casted to the non-generic non-array type Object. (This is the only non-array type one can cast an array to.) It is due to the cast, that the compiler cannot track what the intended type of the generic array is, not because a generic array can exist in general. There should never be any warnings or error message issued for cases, where invariants (e.g. the generic type of an array element) can be proven to hold.

      n
      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the source code provided with

      (1)
      javac com/mn/tools/test/bugs/java/GenericArrayTest.java
      (2)
      javac -Xlint:unchecked com/mn/tools/test/bugs/java/GenericArrayTest.java
      (3)
      javac -Xlint:-unchecked com/mn/tools/test/bugs/java/GenericArrayTest.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      (1)
      <empty message>
      (2)
      com/mn/tools/test/bugs/java/GenericArrayTest.java:15: warning: [unchecked] unchecked cast
      found : java.lang.Object[]
      required: T[]
                      return (T[]) new Object[size];
                                   ^
      1 warning
      (3)
      <empty message>

      ACTUAL -
      (1)
      Note: com/mn/tools/test/bugs/java/GenericArrayTest.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      (2)
      com/mn/tools/test/bugs/java/GenericArrayTest.java:15: warning: [unchecked] unchecked cast
      found : java.lang.Object[]
      required: T[]
                      return (T[]) new Object[size];
                                   ^
      1 warning
      (3)
      Note: com/mn/tools/test/bugs/java/GenericArrayTest.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /** GenericArrayTest.java
      */

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

      /**
      Tests for a compile without warning.
      */
      public class GenericArrayTest<T> {

      public T[] createAnArray(int size) {
      // This results in error "generic array creation"
      // return new T[size];
      // This results in warning "warning: [unchecked] unchecked cast"
      return (T[]) new Object[size];
      }
      }
      // :indentSize=8:tabSize=8:
      ---------- END SOURCE ----------
      (Incident Review ID: 311052)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                rmandalasunw Ranjith Mandala (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: