java 泛型:eclipse 中未显示编译器错误
java generics: compiler error not shown in eclipse
我有这些 类:
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 编译器是错误的。
为什么会有这样的差异?
这里是环境的详细信息:
日食
- Mars.2 发布 (4.5.2)
- jdk1.8.0_71
- 编译器合规级别:1.8
Maven
- 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
- OS 名称:"windows 10",版本:“10.0”,架构:"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). And javac is not always the right 在这个游戏中,你确实可以遇到 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.
我有这些 类:
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 编译器是错误的。
为什么会有这样的差异?
这里是环境的详细信息:
日食
- Mars.2 发布 (4.5.2)
- jdk1.8.0_71
- 编译器合规级别:1.8
Maven
- 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
- OS 名称:"windows 10",版本:“10.0”,架构:"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). And javac is not always the right 在这个游戏中,你确实可以遇到 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.