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

标签 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

泛型类型干扰不能应用于原始类型。但我认为自 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/

相关文章:

java - 遍历 HashMap 并将值放入列表

java - 在 Java 中使用正则表达式提取值

java - 从绝对路径 Eclipse IFolder

java - Java 泛型中的独立类型参数

c# - List<List<int>>的组合

generics - 在泛型方法中从联合式枚举中获取事件值

java - Play Framework 中 AJAX 调用的部分渲染

java - 重定向到配置的错误页面

eclipse - java.net.BindException : Address already in use: JVM_Bind

Java:使用scanner.nextLine().equals ("example"))需要重复输入