提交 a7d24368 编写于 作者: S smarks

8231800: Better listing of arrays

Reviewed-by: alanb, rhalade, ahgross, igerasim
上级 1b39e8ee
...@@ -175,14 +175,16 @@ public class ArrayList<E> extends AbstractList<E> ...@@ -175,14 +175,16 @@ public class ArrayList<E> extends AbstractList<E>
* @throws NullPointerException if the specified collection is null * @throws NullPointerException if the specified collection is null
*/ */
public ArrayList(Collection<? extends E> c) { public ArrayList(Collection<? extends E> c) {
elementData = c.toArray(); Object[] a = c.toArray();
if ((size = elementData.length) != 0) { if ((size = a.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652) if (c.getClass() == ArrayList.class) {
if (elementData.getClass() != Object[].class) elementData = a;
elementData = Arrays.copyOf(elementData, size, Object[].class); } else {
elementData = Arrays.copyOf(a, size, Object[].class);
}
} else { } else {
// replace with empty array. // replace with empty array.
this.elementData = EMPTY_ELEMENTDATA; elementData = EMPTY_ELEMENTDATA;
} }
} }
......
...@@ -254,8 +254,7 @@ public class PriorityQueue<E> extends AbstractQueue<E> ...@@ -254,8 +254,7 @@ public class PriorityQueue<E> extends AbstractQueue<E>
private void initElementsFromCollection(Collection<? extends E> c) { private void initElementsFromCollection(Collection<? extends E> c) {
Object[] a = c.toArray(); Object[] a = c.toArray();
// If c.toArray incorrectly doesn't return Object[], copy it. if (c.getClass() != ArrayList.class)
if (a.getClass() != Object[].class)
a = Arrays.copyOf(a, a.length, Object[].class); a = Arrays.copyOf(a, a.length, Object[].class);
int len = a.length; int len = a.length;
if (len == 1 || this.comparator != null) if (len == 1 || this.comparator != null)
......
...@@ -171,11 +171,13 @@ public class Vector<E> ...@@ -171,11 +171,13 @@ public class Vector<E>
* @since 1.2 * @since 1.2
*/ */
public Vector(Collection<? extends E> c) { public Vector(Collection<? extends E> c) {
elementData = c.toArray(); Object[] a = c.toArray();
elementCount = elementData.length; elementCount = a.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652) if (c.getClass() == ArrayList.class) {
if (elementData.getClass() != Object[].class) elementData = a;
elementData = Arrays.copyOf(elementData, elementCount, Object[].class); } else {
elementData = Arrays.copyOf(a, elementCount, Object[].class);
}
} }
/** /**
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
...@@ -135,8 +136,7 @@ public class CopyOnWriteArrayList<E> ...@@ -135,8 +136,7 @@ public class CopyOnWriteArrayList<E>
elements = ((CopyOnWriteArrayList<?>)c).getArray(); elements = ((CopyOnWriteArrayList<?>)c).getArray();
else { else {
elements = c.toArray(); elements = c.toArray();
// c.toArray might (incorrectly) not return Object[] (see 6260652) if (c.getClass() != java.util.ArrayList.class)
if (elements.getClass() != Object[].class)
elements = Arrays.copyOf(elements, elements.length, Object[].class); elements = Arrays.copyOf(elements, elements.length, Object[].class);
} }
setArray(elements); setArray(elements);
...@@ -762,6 +762,9 @@ public class CopyOnWriteArrayList<E> ...@@ -762,6 +762,9 @@ public class CopyOnWriteArrayList<E>
*/ */
public int addAllAbsent(Collection<? extends E> c) { public int addAllAbsent(Collection<? extends E> c) {
Object[] cs = c.toArray(); Object[] cs = c.toArray();
if (c.getClass() != ArrayList.class) {
cs = cs.clone();
}
if (cs.length == 0) if (cs.length == 0)
return 0; return 0;
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
...@@ -822,9 +825,10 @@ public class CopyOnWriteArrayList<E> ...@@ -822,9 +825,10 @@ public class CopyOnWriteArrayList<E>
try { try {
Object[] elements = getArray(); Object[] elements = getArray();
int len = elements.length; int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class) if (len == 0 && (c.getClass() == CopyOnWriteArrayList.class ||
c.getClass() == ArrayList.class)) {
setArray(cs); setArray(cs);
else { } else {
Object[] newElements = Arrays.copyOf(elements, len + cs.length); Object[] newElements = Arrays.copyOf(elements, len + cs.length);
System.arraycopy(cs, 0, newElements, len, cs.length); System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements); setArray(newElements);
......
...@@ -263,8 +263,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E> ...@@ -263,8 +263,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
} }
Object[] a = c.toArray(); Object[] a = c.toArray();
int n = a.length; int n = a.length;
// If c.toArray incorrectly doesn't return Object[], copy it. if (c.getClass() != java.util.ArrayList.class)
if (a.getClass() != Object[].class)
a = Arrays.copyOf(a, n, Object[].class); a = Arrays.copyOf(a, n, Object[].class);
if (screen && (n == 1 || this.comparator != null)) { if (screen && (n == 1 || this.comparator != null)) {
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册