On Wed, 21 Nov 2007 09:33:15 -0800
Scott M Stark <sstark(a)redhat.com> wrote:
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<?>:
How would the compiler know that the parameter may be used for mutable
operations? There's no way it could differentiate between the two
cases.
The "?" is treated differently from an unqualified type because an
unqualified List can allow you to add any Object type, whereas "?"
means there is *some* specific type required but you don't know what it
is; therefore it is unsafe to make any assumptions regarding what can
be accepted. This fails to compile for the same reason that casting
from a Collection<Class> to a Collection<Object> fails. The compiler
can't tell how you intend to make use of the parameter.
I think this is an Eclipse bug, not a JDK bug.
- DML