【发布时间】:2017-07-06 05:43:15
【问题描述】:
我在文件 Sandbox.java 中有以下类:
package sandbox;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class Sandbox {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
executor.shutdown();
System.out.println(s);
}
}
class Wrapper<T> {
T t;
private Wrapper(T t) {
this.t = t;
}
public T get() {
return t;
}
public static <T> Wrapper<T> of (T t) {
return new Wrapper<>(t);
}
}
Eclipse 中的编译在第 14 行“无法推断 map(Function) 的类型参数”中显示错误。
使用纯 javac (JDK 1.8.0_121) 编译相同的代码没有问题。
如果我将正确的行改为:
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.<String>thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
然后代码在 Eclipse 中编译没有错误。
有谁知道为什么会有这样的行为?是bug吗?
我使用的是 Eclipse 4.6.2.20161208-0625(目前没有发现更新)。
【问题讨论】:
-
您是否通过 Eclipse 使用具有相同合规性标志的相同版本的 java 编译器?我有时会发现 Eclipse 使用了一些意想不到的东西,这取决于项目的设置方式。
-
你是指源层和目标层吗?在这两种情况下,它都是 1.8。我使用在 Eclipse 中配置的编译器实例,而不是使用纯 javac。所以我认为这是 JDT 的问题。
-
schmosel,是的,很可能是同样的问题。但不幸的是,也没有解决方案。
-
我知道有一些 Java 1.8 版本存在这个问题(1.8.0_25 或 1.8.0_45 - 不记得是哪个)。此代码在 1.8.0_111 上编译并运行良好。
标签: java eclipse compiler-errors eclipse-neon