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

(coll) Arrays.ArrayList.toArray() does not return Object[] always

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Javadoc for Collection<E>.toArray<T>(T []) states that toArray(new Object[0]) is identical in function to toArray(). However Arrays.ArrayList.toArray() may return array that is not of type Object[]. This may cause unexpected ArrayStoreException.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run attached source code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      class [Ljava.lang.Object;

      ACTUAL -
      class [Ljava.lang.String;
      Exception in thread "main" java.lang.ArrayStoreException: java.lang.Object
              at Main.main(Main.java:7)

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Main {
      public static void main(String[] args) {
      Object[] array = Arrays.asList("A").toArray();
      System.out.println(array.getClass());
      array[0] = new Object(); // cause ArrayStoreException
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      use toArray(new Object[0])

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              martin Martin Buchholz
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: