--- jsr166/src/main/java/util/ArrayList.java 2019/08/10 16:48:05 1.68 +++ jsr166/src/main/java/util/ArrayList.java 2020/07/24 20:57:26 1.71 @@ -109,6 +109,7 @@ import jdk.internal.util.ArraysSupport; public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable { + // OPENJDK @java.io.Serial private static final long serialVersionUID = 8683452581122892189L; /** @@ -177,15 +178,16 @@ public class ArrayList extends Abstra * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection c) { - elementData = c.toArray(); - if ((size = elementData.length) != 0) { - // 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); + 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; } } @@ -855,6 +857,7 @@ public class ArrayList extends Abstra * instance is emitted (int), followed by all of its elements * (each an {@code Object}) in the proper order. */ + // OPENJDK @java.io.Serial private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out element count, and any hidden stuff @@ -882,6 +885,7 @@ public class ArrayList extends Abstra * could not be found * @throws java.io.IOException if an I/O error occurs */ + // OPENJDK @java.io.Serial private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -1136,7 +1140,7 @@ public class ArrayList extends Abstra this.parent = parent; this.offset = parent.offset + fromIndex; this.size = toIndex - fromIndex; - this.modCount = root.modCount; + this.modCount = parent.modCount; } public E set(int index, E element) { @@ -1289,7 +1293,7 @@ public class ArrayList extends Abstra return new ListIterator() { int cursor = index; int lastRet = -1; - int expectedModCount = root.modCount; + int expectedModCount = SubList.this.modCount; public boolean hasNext() { return cursor != SubList.this.size; @@ -1333,7 +1337,7 @@ public class ArrayList extends Abstra final Object[] es = root.elementData; if (offset + i >= es.length) throw new ConcurrentModificationException(); - for (; i < size && modCount == expectedModCount; i++) + for (; i < size && root.modCount == expectedModCount; i++) action.accept(elementAt(es, offset + i)); // update once at end to reduce heap write traffic cursor = i; @@ -1359,7 +1363,7 @@ public class ArrayList extends Abstra SubList.this.remove(lastRet); cursor = lastRet; lastRet = -1; - expectedModCount = root.modCount; + expectedModCount = SubList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } @@ -1385,7 +1389,7 @@ public class ArrayList extends Abstra SubList.this.add(i, e); cursor = i + 1; lastRet = -1; - expectedModCount = root.modCount; + expectedModCount = SubList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); }