相同的源代码,Eclipse构建成功但Maven(javac)失败

EnT*_*Cas 8 java eclipse generics compiler-errors

使用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
Run Code Online (Sandbox Code Playgroud)

泛型类型干扰不能应用于原始类型.但我认为自Java5以来,装箱/拆箱机制在原始类型和包装类之间无缝地工作.

无论如何,奇怪的是Eclipse没有报告任何错误并愉快地编译.我正在使用JDK1.6.0_12.这可能是什么问题?

Mik*_*per 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.
}
Run Code Online (Sandbox Code Playgroud)


Jay*_*Jay 3

有几点需要注意:

  1. Eclipse 和 Maven 都使用相同的 java/bin 安装
  2. Eclipse 和 Maven 使用相同的库,其中一个可能有另一个没有的东西。

  • 我刚刚意识到 Eclipse 附带的编译器与 Sun 的官方 java 编译器不同。 (2认同)