Request for review : 7121314 : Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec

David Holmes david.holmes at
Sat Mar 10 12:52:21 UTC 2012

On 10/03/2012 12:02 PM, Ulf Zibis wrote:
> Am 09.03.2012 09:16, schrieb Sean Chou:
>> Hi all,
>> AbstractCollection.toArray(T[] ) might return a new array even if the
>> given array has enough room for the returned elements when it is
>> concurrently modified. This behavior violates the spec documented in
>> java.util.Collection .
>> This patch checks the size of returned array and copies the elements
>> to return to the given array if it is large enough.
> More Questions:
> Why don't we have
> public <T super E> T[] toArray(T[] a) ?
> This would prevent from the cast
> r[i] = (T);

It's too late to change the method signature now.

> Wouldn't following statement potentially throw a ClassCastException ?
> r[i] = (T);

Apparently not. I passed in a String[] when it should be Object[] and 
got ArrayStoreException. Checking the bytecode I don't see a checkcast.


> ... but the doc says, it should throw an ArrayStoreException, if the
> runtime type of the specified array is not a supertype of the runtime
> type of every element in this collection.
> -Ulf

More information about the core-libs-dev mailing list