eclipse 编译器或 javac 中的 Bug(“无法确定 T 的类型参数”)
下面的代码
public class GenericsTest2 {
public static void main(String[] args) throws Exception {
Integer i = readObject(args[0]);
System.out.println(i);
}
public static <T> T readObject(String file) throws Exception {
return readObject(new ObjectInputStream(new FileInputStream(file)));
// closing the stream in finally removed to get a small example
}
@SuppressWarnings("unchecked")
public static <T> T readObject(ObjectInputStream stream) throws Exception {
return (T)stream.readObject();
}
}
在 eclipse 中编译,但不能使用 javac 编译(无法确定 T 的类型参数;对于上限为 T,java.lang.Object 的类型变量 T,不存在唯一的最大实例)。
当我将读取对象(字符串文件)更改为
@SuppressWarnings("unchecked")
public static <T> T readObject(String file) throws Exception {
return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
}
它在 eclipse 和 javac 中编译。谁是正确的,eclipse编译器还是javac?