eclipse 编译器或 javac 中的错误(“无法确定 T 的类型参数”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/314572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bug in eclipse compiler or in javac ("type parameters of T cannot be determined")
提问by Tobias Schulte
The following code
以下代码
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();
}
}
compiles in eclipse, but not with javac (type parameters of T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T,java.lang.Object).
在 Eclipse 中编译,但不能使用 javac(无法确定 T 的类型参数;对于具有上限 T,java.lang.Object 的类型变量 T 不存在唯一的最大实例)。
When I change readObject(String file) to
当我将 readObject(String file) 更改为
@SuppressWarnings("unchecked")
public static <T> T readObject(String file) throws Exception {
return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
}
it compiles in eclipse and with javac. Who is correct, the eclipse compiler or javac?
它在 eclipse 和 javac 中编译。谁是正确的,eclipse编译器还是javac?
采纳答案by Fabian Steeg
I'd say it's the bug in the Sun compiler reported hereand here, because if you change your line to the one below it works with both, which seems to be exactly what is described in the bug reports.
我会说这是在这里和这里报告的 Sun 编译器中的错误,因为如果您将您的行更改为下面的那一行,它将同时适用于两者,这似乎正是错误报告中描述的内容。
return GenericsTest2.<T>readObject(new ObjectInputStream(new FileInputStream(file)));
回答by Chris Jester-Young
In this case, I'd say your code is wrong (and the Sun compiler is right). There is nothing in your input arguments to readObject
to actually infer the type T
. In that case, you're better off to let it return Object, and let clients manually cast the result type.
在这种情况下,我会说您的代码是错误的(而 Sun 编译器是正确的)。您的输入参数中没有任何内容readObject
可以实际推断类型T
。在这种情况下,最好让它返回 Object,并让客户端手动转换结果类型。
This should work (though I haven't tested it):
这应该有效(虽然我还没有测试过):
public static <T> T readObject(String file) throws Exception {
return GenericsTest2.<T>readObject(new ObjectInputStream(new FileInputStream(file)));
}
回答by m.genova
Oracle JDK6 u22 should be correct but I've this problem with JDK6 u24 too
Oracle JDK6 u22 应该是正确的,但我对 JDK6 u24 也有这个问题
This is a bug of eclipse bug 98379.
这是 Eclipse错误 98379 的错误。
This was not corrected but the problem is resolved via workaround like example in eclipse bugs (see link)
这未得到纠正,但问题已通过解决方法解决,例如 eclipse 错误中的示例(请参阅链接)
回答by OndroMih
If you can modify your method readObject to work transparently when called, you may also use following:
如果您可以修改您的方法 readObject 在调用时透明地工作,您还可以使用以下内容:
public static <T> T readObject(String file, Class<T> type) throws Exception {
return type.cast(readObject(new ObjectInputStream(new FileInputStream(file))));
}
This way, caller is forced to specify the type of the result and compiler knows how to cast the result.
这样,调用者被迫指定结果的类型,编译器知道如何转换结果。
回答by RJC
I found this issue in java version "1.6.0_22". It disappeared when I upgraded to java version "1.6.0_32" as it was fixed in update 25.
我在 Java 版本“1.6.0_22”中发现了这个问题。当我升级到 Java 版本“1.6.0_32”时它消失了,因为它在更新 25 中得到了修复。