[jboss-dev] Does this seem like a compiler bug?

Scott M Stark sstark at redhat.com
Wed Nov 21 13:56:39 EST 2007


The typical example of why List<?> is not a List<Object> is:
List<String> ls = ...;
List<Object> lo = ls;
lo.add(new Object());
String s = ls.get(0);

I guess the equivalent Class, Class<?> example is:
List<Class<String>> ls = ...;
List<Class> lc = ls;
lc.add(Object.class);
Class<String> cs = ls.get(0);
String s = cs.newInstance(); // CCE here

>From that perspective I can see that Class<?> is expressing a stricter
collection definition. However, for that case our usage of
Collection<Class<?>> is incorrect as the Collection is not bound to a
specific type, although unknown, Class.

Scott M Stark wrote:
> By validating whether the parameter is used in any assignment
> expressions in the class. I don't see how the Collection<Class> to a
> Collection<Object> comparison is the same. This has a completely
> different erasure for the collection element, where as Class<?> and
> Class have the same erasure.
> 
> David M. Lloyd wrote:
> 
>> 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
> 
> _______________________________________________
> jboss-development mailing list
> jboss-development at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/jboss-development




More information about the jboss-development mailing list