简体   繁体   English

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

[英]java generics: compiler error not shown in eclipse

I have these classes: 我有这些课程:

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

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

    ...
}

I can compile and run this code on eclipse without problem, but when I invoke mvn compile , this error is thrown: 我可以在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

The error is pretty self-explanatory, and theoretically speaking, javac is right and eclipse compiler is wrong. 错误是非常明显的,从理论上讲,javac是正确的,eclipse编译器是错误的。

Why there's such a difference? 为什么会有这样的差异?

Here you are the details of the environment: 在这里,您将了解环境的详细信息:

  • Eclipse 日食

    • Mars.2 Release (4.5.2) Mars.2发布(4.5.2)
    • jdk 1.8.0_71 jdk 1.8.0_71
    • Compiler compliance level: 1.8 编译器合规性级别:1.8
    • 错误/警告
  • Maven Maven的

    • Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00) Apache Maven 3.3.3(7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37 + 02:00)
    • Maven home: C:\\Develop\\tools\\apache-maven-3.3.3 Maven home:C:\\ Develop \\ tools \\ apache-maven-3.3.3
    • Java version: 1.8.0_71, vendor: Oracle Corporation Java版本:1.8.0_71,供应商:Oracle Corporation
    • Java home: C:\\Program Files\\Java\\jdk1.8.0_71\\jre Java home:C:\\ Program Files \\ Java \\ jdk1.8.0_71 \\ jre
    • Default locale: it_IT, platform encoding: Cp1252 默认语言环境:it_IT,平台编码:Cp1252
    • OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos" 操作系统名称:“windows 10”,版本:“10.0”,arch:“amd64”,系列:“dos”
    • maven-compiler-plugin: Maven的编译器插件:

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

Question : How can I align the eclipse compiler behavior to javac (but I don't want to use javac in eclipse)? 问题 :如何 eclipse编译器行为与javac 对齐 (但我不想在eclipse中使用javac)?

That's yet another mismatch between the Eclipse Java compiler and the official JDK compiler (because these are different indeed) . 这是Eclipse Java编译器和官方JDK编译器之间的另一个不匹配(因为它们确实不同) And javac is not always the right actor in this game, you can indeed hit a javac bug not occurring in the Eclipse compiler. 并且javac在这个游戏中并不总是正确的角色,你确实可以找到Eclipse编译器中没有出现的javac bug。

A similar issue has already been reported: Bug 456459 : Discrepancy between Eclipse compiler and javac - Enums, interfaces, and generics . 已经报告了类似的问题: 错误456459 :Eclipse编译器和javac之间的差异 - 枚举,接口和泛型

To align Maven with Eclipse, you can configure the maven-compiler-plugin as following: 要将Maven与Eclipse对齐,可以按如下方式配置 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>

Basically, you are telling Maven to use the Eclipse Java compiler. 基本上,您告诉Maven使用Eclipse Java编译器。 I was able to reproduce your issue and applying this configuration the Maven build was then fine. 我能够重现您的问题并应用此配置,Maven构建就可以了。 However, I would not recommend this approach. 但是,我不推荐这种方法。

On the other hand, to configure Eclipse to use the JDK compiler is a bit more difficult, basically because the Eclipse compiler is part of the IDE features. 另一方面,配置Eclipse以使用JDK编译器要困难一些,主要是因为Eclipse编译器是IDE功能的一部分。 A procedure is explained in the Stack Overflow q/a: How to run Javac from Eclipse . Stack Overflow中解释了一个过程q / a: 如何从Eclipse运行Javac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM