ajc wont compile lambda as an vararg argument

一世执手 提交于 2020-01-02 10:18:22

问题


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);
    }
}

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

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?

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?


回答1:


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.

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
);


来源:https://stackoverflow.com/questions/24122672/ajc-wont-compile-lambda-as-an-vararg-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!