【发布时间】:2014-04-11 07:57:24
【问题描述】:
在带有 jdk 1.7 的 Eclipse Kepler 4.2 中,我在 Eclipse 中收到以下错误:
The method or(capture#2-of ?) in the type Optional<capture#2-of ?> is not applicable for the arguments (Object)
而运行mvn compile时编译成功。
类如下所示:
package testit;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.base.Optional;
public class Test {
private static final Object NO_VALUE = new Object();
public void method(Map<String, ?> map) {
for (Entry<String, ?> entry : map.entrySet()) {
Optional.fromNullable(entry.getValue()).or(NO_VALUE);
// ^^ error here
}
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.test</groupId>
<artifactId>testit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
类似的代码在生产环境中运行了很长时间而没有错误(在这方面)。它在 Maven、Jenkins、Intelij 中编译,但不在 Eclipse 中。问题是为什么它不能在 Eclipse 中编译?
【问题讨论】:
-
+1 也在这里确认。在您的问题中提供好的示例代码做得很好。有趣的是,Eclipse 中嵌入的 Maven 也可以构建它。
-
您能否检查一下Eclipse 中编译器的合规性设置是否适用于Java 7?
-
@robermann 在我的情况下,他们是。 Eclipse m2e 基于 POM 配置编译器合规性设置。