使用 Maven 编译时不断出现此错误:
type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object
泛型类型干扰不能应用于原始类型。但我认为自 Java5 以来,装箱/拆箱机制在原始类型和包装类之间无缝工作。
无论如何,奇怪的是 Eclipse 没有报告任何错误并顺利编译。我正在使用 JDK1.6.0_12。这可能是什么问题?
最佳答案
当您的代码是通用的并且它调用另一个具有通用返回类型的方法时,可能会发生此问题。有时编译器会在试图弄清楚如何解析方法调用/返回类型时感到困惑。
可以通过在代码中添加显式强制转换来解决。
// Old code:
public T getValue() {
return otherMethod(); // otherMethod has the signature: <RT> RT otherMethod() { ... }
}
// New code:
@SuppressWarnings("unchecked")
public T getValue() {
return (T) otherMethod(); // the cast tells the compiler what to do.
}
关于java - 相同的源代码,Eclipse 构建成功但 Maven (javac) 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1609531/