Community
Participate
Working Groups
Please have a look at this code: import java.util.Iterator; import java.util.List; public class A { List list() { return null; } void m0() { List<A> l = list(); } void m1() { for (A a : list()); } void m2() { for (Iterator<A> i = list().iterator(); i.hasNext();); } } I get an unsafe operation warning in m0 and m2 and an error in m1. Now replace "list()" with "((List<A>) list())". Is this an attempt to get rid of the unsafe operation warnings. m2 doesn't raise a warning anymore. But m0 and m1 get an "unnecassary cast" warning. At least in the case of m1, this is a wrong warning because if I let QuickFix remove the cast, it's an error. In the case of m0, I can choose between two warnings but don't have a way to get rid of the warning :) Now, replace "List list()" with "<X> List<X> list()". This removes the warning from m1 but adds an "unnecessary cast" warning to m1. If I remove that cast, a "type mismatch" error is raised. It also adds the same warning to m0, and if I remove that cast, Eclipse is happy. However, I doubt that m0 is now any better than before and it seems that I fouled the type inferer.
I forgot: I'm using Eclipse 3.0 final and latest jdt.core from HEAD and jdt.ui from JDK_1.5 branch.
The unnecessary cast diagnosis definitely looks like a bug.
Actually, the diagnosis is fine, as conversion from raw is always legite. We should however issue an unsafe type operation warning.
Tuned unsafe type operation warnings. Added regression tests GenericTypeTest#test227,test228. Fixed.
This example raises two warnings now - both the unneeded cast and the unsafe case operation. I think, that's too much :) import java.util.List; public class A { List find() { return null; } void m() { for (A a : (List<A>) find()) { System.out.println(a); } } }
I don't think it is that bad. Remember that these 2 diagnosises are independant one from each other. They happen to occur at same location, and are 2 reasons for getting rid of this cast.
Well, my problem is that both warnings are contradictory. If you let QuickFix "repair" the unneccessary cast warning, this creates an error. OTOH, you have to use the specialisation (and live with the other warning) because otherwise the for loop doesn't work. Therefore, I don't think that the cast is uneccessary.