raw types warnings and instanceof

Rémi Forax forax at univ-mlv.fr
Mon Nov 3 02:54:45 PST 2008

Martin Buchholz a écrit :
> 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
The problem with  Class<Comparable> is that it's a rare type, a mix 
between parameterized type
and raw type and you clearly want to detect them, by example a use of 
List<List>> should raise a warning.

In my opinion, there is two way to solve the problem:
1) change the JLS to use wildcard instead of raw type in getClass() and 
     this require to add an unsafe conversion between a rare type and 
its equivalent using a wildcard
     to avoid to emit an error in code that compile with prevous version.
2) Have a special rule for Class<>, i.e allow Class of a raw type 
without emitting any warning.


More information about the compiler-dev mailing list