java泛型:eclipse中没有显示编译器错误

时间:2016-06-28 07:55:56

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

我有这些课程:

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
  • 的Maven

    • Apache Maven 3.3.3(7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37 + 02:00)
    • Maven home:C:\ Develop \ tools \ apache-maven-3.3.3
    • Java版本:1.8.0_71,供应商:Oracle Corporation
    • Java home:C:\ Program Files \ Java \ jdk1.8.0_71 \ jre
    • 默认语言环境:it_IT,平台编码:Cp1252
    • 操作系统名称:&#34; windows 10&#34;,版本:&#34; 10.0&#34;,arch:&#34; amd64&#34;,family:&#34; dos&#34; < / LI>
    • 行家编译-插件:

      <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)?

1 个答案:

答案 0 :(得分:6)

这是Eclipse Java编译器和官方JDK编译器(because these are different indeed)之间的另一个不匹配。并且javac并不总是这个游戏中的right actor,你确实可以找到Eclipse编译器中没有出现的javac bug。

已报告类似问题: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

相关问题