我使用 Java 1.6u32。在遗留代码中,我有方法:
static <T> T lookup(Class<? super T> interfaceClass){...}
它的用法有以下三种:
ConcreteX x = lookup(InterfaceX.class);
InterfaceX x = lookup(InterfaceX.class);
lookup(InterfaceX.class).someMethodOfInterfaceX();
问题是当我使用一些 ant 构建脚本时第三个编译,但是当我在 Intellij Idea 中构建项目时它失败了。错误信息是:
... cannot find symbol:
symbol: method someMethodOfInterfaceX()
location: class java.lang.Object
当我显式转换为 InterfaceX 时,IDEA 构建成功:
((InterfaceX)lookup(InterfaceX.class)).someMethodOfInterfaceX();
代码是错误的,不应该由 ant 编译还是 IDEA 编译器有问题?
编辑: - 用于重现错误的类:
public class ErasureProblem {
public static void main(String[] args) {
ImplementationX n = lookup(InterfaceX.class);
lookup(InterfaceX.class).m();
}
static <T extends Object> T lookup(Class<? super T> cls){
return null;
}
}
public class ImplementationX implements InterfaceX {
@Override
public Integer m() {
return 0;
}
}
public interface InterfaceX {
Integer m();
}