c.toArray might (incorrectly) not return Object[] (see 6260652)

David Holmes - Sun Microsystems David.Holmes at Sun.COM
Fri May 22 11:22:29 UTC 2009

Hi Doug,

Thanks for the info, one query though ...

Doug Lea said the following on 05/22/09 21:08:
> David Holmes - Sun Microsystems wrote:
>> Okay well the bug is still open. I think the original intent was to 
>> change toArray() to match this, but I think it's far too late to 
>> change that behaviour now. So the only "fix" is to delete that 
>> sentence from the javadoc.
> This bug is and its history are well-known to a few of us.
> A fix once got vetoed in the approval process out of concern
> that existing applications might rely on this incorrect behavior.
> However, the bug causes all sorts of collateral damage. For
> example, the ArrayList(Collection) constructor includes
>     public ArrayList(Collection<? extends E> c) {
>         elementData = c.toArray();
>         size = elementData.length;
>         // c.toArray might (incorrectly) not return Object[] (see 6260652)
>         if (elementData.getClass() != Object[].class)
>             elementData = Arrays.copyOf(elementData, size, Object[].class);
>     }

Ummm why didn't it just use:

     elementData = c.toArray(new Object[c.size()]);



> It would be nice to know whether the number of collateral damage
> bugs in existing applications is greater than the number of breakages
> that would occur if this were fixed. This is not easy to estimate
> though because only a few usages of "Object[] a = x.toArray()"
> when toArray doesn't return Object[] are actually wrong.
> -Doug
>> Were you looking for an actual change in the implementation?
>> Note this is just my opinion on this, I'm not a decision maker here :)
>> Cheers,
>> David

More information about the core-libs-dev mailing list