6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
authormartin
Mon, 29 Jun 2015 15:02:35 -0700
changeset 31540 6efd719b3330
parent 31539 f850b9d09c91
child 31541 8fc82d01db26
6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class Summary: return Arrays.copyOf(a, a.length, Object[].class) Reviewed-by: igerasim, psandoz
jdk/src/java.base/share/classes/java/util/ArrayList.java
jdk/src/java.base/share/classes/java/util/Arrays.java
jdk/src/java.base/share/classes/java/util/Vector.java
jdk/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
jdk/src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java
jdk/test/java/util/Collection/MOAT.java
--- a/jdk/src/java.base/share/classes/java/util/ArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/src/java.base/share/classes/java/util/ArrayList.java	Mon Jun 29 15:02:35 2015 -0700
@@ -178,7 +178,8 @@
     public ArrayList(Collection<? extends E> c) {
         elementData = c.toArray();
         if ((size = elementData.length) != 0) {
-            // c.toArray might (incorrectly) not return Object[] (see 6260652)
+            // defend against c.toArray (incorrectly) not returning Object[]
+            // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
             if (elementData.getClass() != Object[].class)
                 elementData = Arrays.copyOf(elementData, size, Object[].class);
         } else {
--- a/jdk/src/java.base/share/classes/java/util/Arrays.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/src/java.base/share/classes/java/util/Arrays.java	Mon Jun 29 15:02:35 2015 -0700
@@ -3820,7 +3820,7 @@
 
         @Override
         public Object[] toArray() {
-            return a.clone();
+            return Arrays.copyOf(a, a.length, Object[].class);
         }
 
         @Override
--- a/jdk/src/java.base/share/classes/java/util/Vector.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/src/java.base/share/classes/java/util/Vector.java	Mon Jun 29 15:02:35 2015 -0700
@@ -174,7 +174,8 @@
     public Vector(Collection<? extends E> c) {
         elementData = c.toArray();
         elementCount = elementData.length;
-        // c.toArray might (incorrectly) not return Object[] (see 6260652)
+        // defend against c.toArray (incorrectly) not returning Object[]
+        // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
         if (elementData.getClass() != Object[].class)
             elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
     }
--- a/jdk/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Mon Jun 29 15:02:35 2015 -0700
@@ -134,7 +134,8 @@
             elements = ((CopyOnWriteArrayList<?>)c).getArray();
         else {
             elements = c.toArray();
-            // c.toArray might (incorrectly) not return Object[] (see 6260652)
+            // defend against c.toArray (incorrectly) not returning Object[]
+            // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
             if (elements.getClass() != Object[].class)
                 elements = Arrays.copyOf(elements, elements.length, Object[].class);
         }
--- a/jdk/src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java	Mon Jun 29 15:02:35 2015 -0700
@@ -142,7 +142,8 @@
     public IdentityArrayList(Collection<? extends E> c) {
         elementData = c.toArray();
         size = elementData.length;
-        // c.toArray might (incorrectly) not return Object[] (see 6260652)
+        // defend against c.toArray (incorrectly) not returning Object[]
+        // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
         if (elementData.getClass() != Object[].class)
             elementData = Arrays.copyOf(elementData, size, Object[].class);
     }
--- a/jdk/test/java/util/Collection/MOAT.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/jdk/test/java/util/Collection/MOAT.java	Mon Jun 29 15:02:35 2015 -0700
@@ -356,13 +356,7 @@
             }
 
             check(c.toArray().length == c.size());
-            check(c.toArray().getClass() == Object[].class
-                  ||
-                  // !!!!
-                  // 6260652: (coll) Arrays.asList(x).toArray().getClass()
-                  // should be Object[].class
-                  (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
-                  );
+            check(c.toArray().getClass() == Object[].class);
             for (int size : new int[]{0,1,c.size(), c.size()+1}) {
                 Integer[] a = c.toArray(new Integer[size]);
                 check((size > c.size()) || a.length == c.size());