0
试图在IDEA中运行项目并发生异常。该项目始终仅在Eclipse上运行。Eclipse中的通用方法调用
下面是示例代码:
package com.test;
public class GenericMethod {
private Object value;
@SuppressWarnings("unchecked")
public <X> X getValue() {
return (X) value;
}
public void setValue(Object value) {
this.value = value;
}
}
和使用它的类:
package com.test;
public class GenericMethodTest {
public static void main(String[] args) {
GenericMethod method = new GenericMethod();
method.setValue(3);
int[] array = new int[] {1, 2};
array[0] = method.getValue();
System.out.println(array[0]);
}
}
所以,如果你试图在Eclipse中运行这个例子 - 一切正常。它编译并显示正确的结果。
但是,如果你尝试运行,比方说,在IDEA或者如果你只是使用ant或您的控制台和javac命令 - 你会得到编译错误:
Buildfile: C:\test\build.xml
build:
[javac] C:\test\build.xml:13: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to C:\test\bin
[javac] C:\test\src\com\test\GenericMethodTest.java:15: type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object
[javac] array[0] = method.getValue();
[javac] ^
[javac] 1 error
发生了什么事?
Eclipse是否使用它自己的编译器?
谁有bug?