问题
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