ajc不会将lambda编译为vararg参数 [英] ajc wont compile lambda as an vararg argument

查看:157
本文介绍了ajc不会将lambda编译为vararg参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using ajc 1.8, java 8 and experiencing compiler issue. Here's the sample code.

 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失败以下内容:

This code properly compiles and executes with javac meanwhile ajc fails with following :

如果我用匿名类替换lambdas,这将编译并运行,但我想找到一个解决方法,不会强迫我回到匿名类,任何vm参数或任何其他解决方法?

If I replace lambdas with anonymous classes this will compile and run, but I'd like to find workaround that didnt force me to get back to anonymous classes, any vm arguments or any other workarounds?

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

My recent issue with java 8 code compilation issue with ajc were solved using -noverify flag.

也许我会使用加载时间编织摆脱所有问题?

Maybe I'll get rid of all issues using load time weaving?

推荐答案

显然目标打字 / <一个href =http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html =nofollow>类型推断在这里不起作用(我不知道为什么)尽管事实上你声明了 List< Runnable> ,但泛型类型 Arrays.asList 无法使用找出你想要实现的功能接口。

Apparently target typing/type inference didn't work here (I am not sure why yet) and despite the fact that you declared List<Runnable> as result, generic type Arrays.asList wasn't able to figure out which functional interface you want to implement.

我不确定这是否可行,但您可以将 asList 的泛型类型设置为手动运行。要做到这一点只需写

I am not sure if this will work but you can set generic type of asList method to Runnable manually. To do it just write

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

这篇关于ajc不会将lambda编译为vararg参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆