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?