changeset 12320:fb84b4a830f9

6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class Summary: return Arrays.copyOf(a, a.length, Object[].class) Reviewed-by: igerasim, psandoz
author martin
date Mon, 29 Jun 2015 15:02:35 -0700
parents 4955c48b99d7
children 1c8bca2ebba1
files src/java.base/share/classes/java/util/ArrayList.java src/java.base/share/classes/java/util/Arrays.java src/java.base/share/classes/java/util/Vector.java src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java test/java/util/Collection/MOAT.java
diffstat 6 files changed, 10 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/ArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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/src/java.base/share/classes/java/util/Arrays.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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/src/java.base/share/classes/java/util/Vector.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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/src/java.desktop/share/classes/sun/awt/util/IdentityArrayList.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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/test/java/util/Collection/MOAT.java	Thu Jul 09 09:55:36 2015 -0400
+++ b/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());