为什么javac抱怨与类的类型参数无关的generics?

请按顺序阅读代码中的注释,问题详情如下。
为什么会发生这种差异?
如果可能,请引用JLS。

import java.util.*; /** * Suppose I have a generic class * @param  with a type argument. */ class Generic { // Apart from using T normally, T paramMethod() { return null; } // the class' interface also contains Generic Java Collections // which are not using T, but unrelated types. List unrelatedMethod() { return null; } } @SuppressWarnings("unused") public class Test { // If I use the class properly (with qualified type arguments) void properUsage() { Generic g = new Generic(); // everything works fine. String s = g.paramMethod(); List pos = g.unrelatedMethod(); // OK error: incompatible types: List := List List thisShouldErrorCompile = g.unrelatedMethod(); } // But when I use the raw type, *ALL* the generics support is gone, even the Collections'. void rawUsage() { // Using Generic as the type turns fixes the warnings below. Generic g = new Generic(); // OK error: incompatible types: String := Object String s = g.paramMethod(); // WTF warning: unchecked conversion: List := raw List List pos = g.unrelatedMethod(); // WTF warning: unchecked conversion: List := raw List List thisShouldErrorCompile = g.unrelatedMethod(); } } 

边注

我最初在IntelliJ IDEA中发现了这个,但我想编译器与javac是兼容的,因为当我使用以下代码编译上面的代码时,它给出了相同的错误/警告。

 $ javac -version javac 1.7.0_05 $ javac Test.java -Xlint:unchecked ... $ javac Test.java -Xlint:unchecked -source 1.5 -target 1.5 ... 

从JLS 4.8原始类型

原始类型的使用仅允许作为遗留代码兼容性的让步。 在将generics引入Java编程语言之后编写的代码中使用原始类型是非常不鼓励的。

未从其超类或超接口inheritance的原始类型C的构造函数(第8.8节),实例方法(第8.4节,第9.4节)或非静态字段(第8.3节)M的类型是对应的原始类型在对应于C的通用声明中擦除其类型

哪个 – 如果你仔细阅读 – 暗示所有类型都被删除 ,而不仅仅是你遗漏的类型。