Skip to main content

Generics compiler bug?

2 replies [Last post]
cowwoc
Offline
Joined: 2003-08-24
Points: 0

I just discovered that if you compile the following code:

<br />
import java.util.List;</p>
<p>public class Test<br />
{<br />
  public List getList()<br />
  {<br />
    return null;<br />
  }</p>
<p>  public static void main(String[] args)<br />
  {<br />
    List list = new Test().getList();<br />
  }<br />
}<br />

the compiler will complain:

<br />
warning: [unchecked] unchecked conversion<br />
found   : java.util.List<br />
required: java.util.List<br />
    List list = new Test().getList();<br />
1 warning<br />

I believe this is a compiler bug, because the fact that Test is a raw-type shouldn't cause getList() to return a raw-type too. To further make my point, if you remove the type-parameter from Test's declaration the warning goes away. Can someone please confirm?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexlamsl
Offline
Joined: 2004-09-02
Points: 0

Ah, Java Puzzlers :-)

You are using raw types for Test, which means the generics won't get applied and thus the unchecked warning.

cowwoc
Offline
Joined: 2003-08-24
Points: 0

Sorry but where does it say this is supposed to happen in the specification or Generics FAQ? Wouldn't it be possible to return Test as a raw-type, then getList() returns List and again if the left-hand side is a raw-type you convert that to a raw-type, etc...? I don't understand why you'd need to convert all Generics to raw-types before you know whether the left-hand side is expecting a raw-type or Generified type.