diff --git a/src/share/classes/java/util/ArrayList.java b/src/share/classes/java/util/ArrayList.java index 133a39ac785b417ce95a720cde77db851143e6a2..48628a43050fd7555806c42724138595540a63c7 100644 --- a/src/share/classes/java/util/ArrayList.java +++ b/src/share/classes/java/util/ArrayList.java @@ -175,14 +175,16 @@ public class ArrayList extends AbstractList * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection c) { - elementData = c.toArray(); - if ((size = elementData.length) != 0) { - // c.toArray might (incorrectly) not return Object[] (see 6260652) - if (elementData.getClass() != Object[].class) - elementData = Arrays.copyOf(elementData, size, Object[].class); + Object[] a = c.toArray(); + if ((size = a.length) != 0) { + if (c.getClass() == ArrayList.class) { + elementData = a; + } else { + elementData = Arrays.copyOf(a, size, Object[].class); + } } else { // replace with empty array. - this.elementData = EMPTY_ELEMENTDATA; + elementData = EMPTY_ELEMENTDATA; } } diff --git a/src/share/classes/java/util/PriorityQueue.java b/src/share/classes/java/util/PriorityQueue.java index e1e3dfb3f380be8adb8985bc2018f7f1833c1d5a..595dc18bb4c56c98749b22d36bf60dd8dc605609 100644 --- a/src/share/classes/java/util/PriorityQueue.java +++ b/src/share/classes/java/util/PriorityQueue.java @@ -254,8 +254,7 @@ public class PriorityQueue extends AbstractQueue private void initElementsFromCollection(Collection c) { Object[] a = c.toArray(); - // If c.toArray incorrectly doesn't return Object[], copy it. - if (a.getClass() != Object[].class) + if (c.getClass() != ArrayList.class) a = Arrays.copyOf(a, a.length, Object[].class); int len = a.length; if (len == 1 || this.comparator != null) diff --git a/src/share/classes/java/util/Vector.java b/src/share/classes/java/util/Vector.java index 733226f9e9128501dce2b673414ba59fb774c347..3837a338e143dcb4cf5cff194728a9dff33ce01a 100644 --- a/src/share/classes/java/util/Vector.java +++ b/src/share/classes/java/util/Vector.java @@ -171,11 +171,13 @@ public class Vector * @since 1.2 */ public Vector(Collection c) { - elementData = c.toArray(); - elementCount = elementData.length; - // c.toArray might (incorrectly) not return Object[] (see 6260652) - if (elementData.getClass() != Object[].class) - elementData = Arrays.copyOf(elementData, elementCount, Object[].class); + Object[] a = c.toArray(); + elementCount = a.length; + if (c.getClass() == ArrayList.class) { + elementData = a; + } else { + elementData = Arrays.copyOf(a, elementCount, Object[].class); + } } /** diff --git a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java index 1f310ef48850e996f66582ec49915e222fee866f..f068640a3fd32ede407fd814ef33f9a5aa587b6d 100644 --- a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java +++ b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java @@ -34,6 +34,7 @@ package java.util.concurrent; import java.util.AbstractList; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; @@ -135,8 +136,7 @@ public class CopyOnWriteArrayList elements = ((CopyOnWriteArrayList)c).getArray(); else { elements = c.toArray(); - // c.toArray might (incorrectly) not return Object[] (see 6260652) - if (elements.getClass() != Object[].class) + if (c.getClass() != java.util.ArrayList.class) elements = Arrays.copyOf(elements, elements.length, Object[].class); } setArray(elements); @@ -762,6 +762,9 @@ public class CopyOnWriteArrayList */ public int addAllAbsent(Collection c) { Object[] cs = c.toArray(); + if (c.getClass() != ArrayList.class) { + cs = cs.clone(); + } if (cs.length == 0) return 0; final ReentrantLock lock = this.lock; @@ -822,9 +825,10 @@ public class CopyOnWriteArrayList try { Object[] elements = getArray(); int len = elements.length; - if (len == 0 && cs.getClass() == Object[].class) + if (len == 0 && (c.getClass() == CopyOnWriteArrayList.class || + c.getClass() == ArrayList.class)) { setArray(cs); - else { + } else { Object[] newElements = Arrays.copyOf(elements, len + cs.length); System.arraycopy(cs, 0, newElements, len, cs.length); setArray(newElements); diff --git a/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java b/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java index d124a3ea9e5934508a11135e724244481feb0e83..268b21cae048dfabd2cf0fd7272c446ce28401f6 100644 --- a/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java +++ b/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java @@ -263,8 +263,7 @@ public class PriorityBlockingQueue extends AbstractQueue } Object[] a = c.toArray(); int n = a.length; - // If c.toArray incorrectly doesn't return Object[], copy it. - if (a.getClass() != Object[].class) + if (c.getClass() != java.util.ArrayList.class) a = Arrays.copyOf(a, n, Object[].class); if (screen && (n == 1 || this.comparator != null)) { for (int i = 0; i < n; ++i)