raw types warnings and instanceof
Martin Buchholz
martinrb at google.com
Sun Nov 2 16:45:46 PST 2008
Here's another case where raw type warnings seem
well-intentioned, but going too far.
$ cat ClassRaw.java; jver coll jr ClassRaw
public class ClassRaw {
public static void main(String[] args) throws Throwable {
Class<Comparable> x = Comparable.class;
}
}
==> javac -Xlint:all ClassRaw.java
ClassRaw.java:3: warning: [raw-type] found raw type: java.lang.Comparable
missing type parameters for generic class java.lang.Comparable<T>
Class<Comparable> x = Comparable.class;
^
1 warning
My first impression is that these warnings are more verbose,
more annoying, and less likely to find bugs than even "unchecked",
and that even pedants like myself will change their scripts to do
-Xlint:all,-rawtypes instead of -Xlint:all
Martin
On Sun, Nov 2, 2008 at 16:26, Martin Buchholz <martinrb at google.com> wrote:
> Hi javac maintainers,
>
> This is a bug report, sort of.
>
> Thanks for adding the raw types warnings, but...
>
> they currently warn about
> x instanceof RAW-TYPE
> when they probably should not.
>
> It is very common to do
>
> void f (Foo<T> x) {
> if (x instanceof Bar) {
> Bar<T> y = (Bar<T>) x;
> ....
>
> The workaround is to do:
>
> void f (Foo<T> x) {
> if (x instanceof Bar<?>) {
> Bar<T> y = (Bar<T>) x;
> ....
>
> but the addition of <?> adds noise without providing any safety.
>
> Martin
>
More information about the compiler-dev
mailing list