gpt4 book ai didi

java - ajc 不会将 lambda 编译为 vararg 参数

转载 作者:行者123 更新时间:2023-11-30 08:22:50 24 4
gpt4 key购买 nike

我正在使用 ajc 1.8、java 8 并遇到编译器问题。这是示例代码。

 public class ExecutorTests {
List<Runnable> tasks = Arrays.asList(
() -> {
System.out.println("task1 start");
try {
Thread.sleep(1000);
} catch (Exception ignored) {}
System.out.println("task1 end");
},
() -> {
System.out.println("task2 start");
try {
Thread.sleep(1000);
} catch (Exception ignored) {}
System.out.println("task2 end");
},
() -> {
System.out.println("task3 start");
try {
Thread.sleep(1000);
} catch (Exception ignored) {}
System.out.println("task3 end");
}
);

@Test
public void executeInSync(){
tasks.stream().forEach(Runnable::run);
}
}

此代码使用 javac 正确编译和执行,同时 ajc 失败并显示以下内容:

enter image description here

如果我将 lambda 替换为匿名类,这将编译并运行,但我想找到不会强制我返回匿名类的解决方法、任何 vm 参数或任何其他解决方法?

我最近使用 ajc 的 java 8 代码编译问题已使用 -noverify 标志解决。

也许我会使用加载时间编织来解决所有问题?

最佳答案

显然 target typing/type inference没有在这里工作(我还不确定为什么)尽管你声明了 List<Runnable>结果,泛型类型 Arrays.asList无法弄清楚您要实现哪个功能接口(interface)。

我不确定这是否可行,但您可以设置 asList 的通用类型Runnable 的方法手动。要做到这一点,只需写

List<Runnable> tasks = Arrays.<Runnable>asList(
... // ^^^^^^^^^^ you need to add this
);

关于java - ajc 不会将 lambda 编译为 vararg 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24122672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com