On Jun 26, 2015, at 11:54 PM, Martin Buchholz <marti...@google.com> wrote:

> 10 years later ... still asking for approval to commit.
> 
> https://bugs.openjdk.java.net/browse/JDK-6260652
> http://cr.openjdk.java.net/~martin/webrevs/openjdk9/Arrays.asList.toArray/

+1.

This is probably why there is still the following code in ArrayList:

public ArrayList(Collection<? extends E> 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);
    } else {
        // replace with empty array.
        this.elementData = EMPTY_ELEMENTDATA;
    }
}

I suspect we cannot remove this check, but we could update the comment 
referring to JDK-external collection implementations.

I trawled through the JDK code and found one other violation of the toArray 
contract in:

  com.sun.java.util.jar.pack.ConstantPool.Index:

  public Entry[] toArray() {
      return toArray(new Entry[size()]);
  }

Paul.

Reply via email to