与eclipse和maven之间类型转换的不同的编译行为 [英] Different compilation behavior with type cast between eclipse and maven
问题描述
这是我的代码示例:
private static void fac(Map<? extends Serializable,? extends Serializable> mapTo){
//do sth.
}
public static void main(String[] args) {
Map<String,Object> mapFrom=null;
fac((Map<? extends Serializable, ? extends Serializable>) mapFrom);
}
上面的代码在eclipse中成功编译在maven中失败了(由javac导致的不兼容类型错误?)。
The code above is compiled successfully in eclipse(with a type safety warning) but failed in maven(an "incompatible types" error caused by javac?).
现在我必须像这样改变我的代码:
Now I have to change my code like this:
public static void main(String[] args) {
Map<String,Object> mapFrom=null;
fac((Map) mapFrom);
}
我确认java版本是一样的,我的问题是:
I've confirmed the java version is the same, and my question is:
-
为什么它们有不同的行为?
Why do they have different behavior?
推荐答案
自己的Java编译器; Maven使用 javac
。大多数时候,两者接受相同的代码,但泛型是复杂的,编译器有bug。在Java 6的 javac
中有一些已知的错误,例如导致问题。
Eclipse comes with its own Java compiler; Maven uses javac
. Most of the time, both accept the same code but generics are complicated and compiler do have bugs. There are a couple of known bugs in javac
of Java 6 which cause problems, for example.
Oracle不会修复它们。解决方案是使用Java 7运行Maven并配置 maven-compiler-plugin
生成Java 6字节代码(参见 Kumar Sambhav的回答)。
Oracle will not fix them. The solution is to use Java 7 to run Maven and configure the maven-compiler-plugin
the generate Java 6 byte code (see Kumar Sambhav's answer).
这篇关于与eclipse和maven之间类型转换的不同的编译行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!