Community
Participate
Working Groups
Build Identifier: 20090920-1017 In the Java compiler settings I can enable "Unchecked generic type operation" to be treated as a warning. But if I do, the following is signaled as a warning. ############# class C<X> { public void m( Collection<?> p ) { for ( Object o : p ) dump( (X)o ); // <-- This is warned about! } } ############# Note, that I cannot simply change the method interface to "Collection<X>"! The case to X is treated as a warning. And IMHO this is always wrong. Otherwise you'd have to treat every single cast (to a concrete type) as a warning or error, which nobody wants. I don't know, if this compiler setting does something useful. But for me it just looks like a bug. Reproducible: Always
Additionally the quick fixes won't work. Moving the cast to a method or a local variable won't do anything. The same warning will be made there.
Hello Marvin, The code snippet below shows what the compiler is warning about : public class C<X> { public void m(Object o) { X x = (X)o; // No exception here as this cast is "unchecked" String s = (String) o; // Exception here as this cast is checked } public static void main(String [] args) { C<String> c = new C<String>(); c.m(new Object()); } } Though both the casts in C#m() "appear" logically to be doing the same thing i.e cast an Object to String, see that only the latter cast throws a ClassCastException. Type variables are not reified at run time (i.e their type information is not completely available at runtime.) Hence neither the compiler nor the VM can arrange to dynamically sanity check the validity of type operations involving them. This is what the compiler is warning about. See that javac issues the same warning with the -Xlint:unchecked command line argument. You can selectively or otherwise suppress this warning as needed.
Verified for 3.6RC4