RFR: 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
Martin Buchholz
martinrb at google.com
Tue Jun 30 17:04:00 UTC 2015
On Tue, Jun 30, 2015 at 3:02 AM, Paul Sandoz <paul.sandoz at oracle.com> wrote:
>
> On Jun 26, 2015, at 11:54 PM, Martin Buchholz <martinrb at 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 recall also being the author of that comment.
I suspect we cannot remove this check,
Agreed.
> but we could update the comment referring to JDK-external collection
> implementations.
>
>
The comment remains correct even if 6260652 is fixed.
I can't think up a clearly better one.
> 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()]);
> }
>
Agreed. I'm unfamiliar with that code - it should probably be fixed in
another change.
More information about the core-libs-dev
mailing list