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

(reflect) Add a generic replacement for Array.newInstance() to help write type safe code

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      A DESCRIPTION OF THE REQUEST :
        Bug # 5101892 explains why the Array.newInstance() method can't be retrofitted to be typesafe.

      Please add a new static method to the Array class that is type safe, with a signature like this one:

      public static <T> T[] Array.newArray(Class<T> componentType, int length)

      .

       

      JUSTIFICATION :
      Having this new type-safe method will give us a better way to write code than using the @SupressWarnings("unchecked") annotation. It will lead to cleaner code, and be a small step towards reducing caclification in the Java libraries.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to be able to compile code that creates arrays without getting any warnings.
      ACTUAL -
      When I compile the code, I get warnings that I should be able to fix.

      ---------- BEGIN SOURCE ----------
      import java.util.List;


      class ArrayWarnings
      {
      // @SuppressWarnings("unchecked")
          private List<String>[] createArray(int size)
          {
              return (List<String>[])Array.newInstance(List.class,size);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      @SupressWarnings("unchecked") provides a grezzy option, but -Xlint ignores it at the moment.
      ###@###.### 2005-1-31 03:21:51 GMT

            darcy Joe Darcy
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: