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

Jason T. Greene jason.greene at redhat.com
Wed Nov 21 15:24:52 EST 2007


David M. Lloyd wrote:
> On Wed, 21 Nov 2007 09:33:15 -0800
> Scott M Stark <sstark at 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.

Looks like it is a JDK bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6557279

The rule is that the compiler can't prove the types are incompatible, so 
  should instead throw a warning.

-- 
Jason T. Greene
JBoss, a division of Red Hat



More information about the jboss-development mailing list