I ran into a problem with the following code that compiles in eclipse,
but fails under the sun jdk:
public void testClassCollection()
{
Collection<Class<?>> c = null;
Collection<Class> ce = (Collection<Class>) c; // This is a warning
in eclipse, error under sun jdk.
This seems like an overly strict check. I can understand it for types
that allow mutable operations with respect to the wildcard parameter,
such as a Collection of List<?>:
public void testCollectionOfUknownlistAdd()
{
Collection<List<?>> coul = new ArrayList<List<?>>();
List<?> lw = coul.iterator().next();
// This is an error because don't know what ? is
lw.add(new Object());
Collection<List> col = (Collection<List>) coul;
List l = col.iterator().next();
// This is allowed for legacy usage
l.add(new Object());
}
but I don't see how I can unsafely use Class in place of Class<?>. Its
equivalent to this safe read-only usage of an unknown collection, which
eclipse does allow, but the jdk does not:
public void testCollectionOfUknownlistGet()
{
Collection<List<?>> coul = new ArrayList<List<?>>();
List<?> lw = coul.iterator().next();
// This is fine because ? although unknown, is still an Object
Object o1 = lw.get(0);
Collection<List> col = (Collection<List>) coul;
List l = col.iterator().next();
Object o2 = l.get(0);
}