The following code throws an ArrayStoreException at toArray(). Should not the compiler have caught the blunder and reported an type conflict given that toArray takes a V[] and the registry Map is typed with a value of Map<String,String>?
private Map<String,Map<String,String>> registry;
...
registry=new TreeMap<String,Map<String,String>>(String.CASE_INSENSITIVE_ORDER));
...
void removeTargets(String[] clsarr, String hdl) {
if(clsarr==null) { clsarr=registry.values().toArray(new String[0]); }
...
}
It looks like a compiler bug to me.
Not a compiler bug. Arguably a library bug though.
The type of
Collection.toArray()is<T> T[] toArray(T[] a). Note thatTis a type parameter to thetoArraymethod and has no relationship toCollection‘s type parameter.This bug would have been caught if the signature of
Collection.toArraywere<T super E> T[] toArray(T[] a)but that would make it tough to use since it’s not legal to donew Map<String, String>[0]— you get a “generic array creation” error.