java 泛型 : compiler error not shown in eclipse

标签 java eclipse maven maven-3 maven-compiler-plugin

我有这些类(class):

public class EntityDataModel<T extends AbstractEntity>
{
    ...
}

public abstract class BarChartBean<E extends ChartEntry, T>
{
    protected EntityDataModel<? extends T> currentModel;

    ...
}

我可以在 eclipse 上毫无问题地编译和运行这段代码,但是当我调用 mvn compile 时,会抛出这个错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project edea2: Compilation failure: Compilation failure:
[ERROR] C:\Develop\...\BarChartBean.java:[53,30] error: type argument ? extends T#1 is not within bounds of type-variable T#2
[ERROR] where T#1,T#2 are type-variables:
[ERROR] T#1 extends Object declared in class BarChartBean
[ERROR] T#2 extends AbstractEntity declared in class EntityDataModel

错误是不言自明的,从理论上讲,javac 是正确的,eclipse 编译器是错误的。

为什么会有这样的差异?

这里是环境的详细信息:

  • eclipse

    • Mars.2 版本 (4.5.2)
    • jdk 1.8.0_71
    • 编译器合规级别:1.8
    • Errors/Warnings
  • 专家

    • Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
    • Maven 主页:C:\Develop\tools\apache-maven-3.3.3
    • Java 版本:1.8.0_71,供应商:Oracle Corporation
    • Java 主目录:C:\Program Files\Java\jdk1.8.0_71\jre
    • 默认语言环境:it_IT,平台编码:Cp1252
    • 操作系统名称:“windows 10”,版本:“10.0”,arch:“amd64”,系列:“dos”
    • maven-compiler-plugin:

      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.5.1</version>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
              <showDeprecation>true</showDeprecation>
              <showWarnings>true</showWarnings>
          </configuration>
      </plugin>
      

问题:如何对齐 eclipse 编译器行为到 javac(但我不想在 eclipse 中使用 javac)?

最佳答案

这是 Eclipse Java 编译器和官方 JDK 编译器 (because these are different indeed) 之间的另一个不匹配。并且 javac 并不总是这个游戏中的 right Actor ,您确实可以遇到 Eclipse 编译器中未发生的 javac 错误。

已经报告了类似的问题:Bug 456459: Discrepancy between Eclipse compiler and javac - Enums, interfaces, and generics

要使 Maven 与 Eclipse 保持一致,您可以按如下方式 configure maven-compiler-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerId>eclipse</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-eclipse</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>
</plugin>

基本上,您是在告诉 Maven 使用 Eclipse Java 编译器。我能够重现您的问题并应用此配置,然后 Maven 构建就可以了。但是,我不推荐这种方法。

另一方面,配置 Eclipse 以使用 JDK 编译器有点困难,主要是因为 Eclipse 编译器是 IDE 功能的一部分。 Stack Overflow q/a 中解释了一个过程:How to run Javac from Eclipse

关于java 泛型 : compiler error not shown in eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070326/

相关文章:

java - 无法在 wro4j-maven-plugin (NPE) 中使用 ConfigurableWroManagerFactory

java - ava.lang.NullPointerException : null at org. apache.catalina.loader.WebappClassLoader.findResources(WebappClassLoader.java:1368)~[na:na]

java - Spring MVC 3.1 RedirectAttributes 不工作

java - Swing 文本组件无法从剪贴板粘贴大文本数据 : java. io.IOException: Owner failed to convert data

eclipse - 如何在Eclipse RCP目标平台中将源代码位置附加到插件?

java - 在 Eclipse (3.6 Helios) 中使用 Spring (3.0.5)

java - 在脚本中使用 maven 或在末尾添加脚本运行器任务

java - 无法使用 Java 在 MongoDB 中上传 JSON

java - 当 Single.zip 中的一个来源失败时如何返回 Single.error()?

java - 是否有与 Javascript 方法 fromCharCode() 等效的 Java?