Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there 2 stack frames for a lambda invocation?

The following code:

public static void main(String[] args) {
    Collections.singleton(1).stream().forEach(i -> new Exception().printStackTrace());
}

prints:

java.lang.Exception
    at PrintLambdaStackTrace.lambda$main$0(PrintLambdaStackTrace.java:6)
    at PrintLambdaStackTrace$$Lambda$1/1831932724.accept(Unknown Source)
    at java.util.Collections$2.tryAdvance(Collections.java:4717)
    at java.util.Collections$2.forEachRemaining(Collections.java:4725)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    at PrintLambdaStackTrace.main(PrintLambdaStackTrace.java:6)

How is the lambda invocation implemented? Why are there 2 stack frames?

like image 382
Bogdan Calmac Avatar asked Jul 15 '15 16:07

Bogdan Calmac


1 Answers

PrintLambdaStackTrace$$Lambda$1/1831932724.accept(Unknown Source)

This is a generated class which implements the required interface. Its accept method is just a stub, delegating to a method which was generated at compile time and added to the PrintLambdaStackTrace class. This class is generated at lambda linkage time (the first time a lambda instance needs to be created).

PrintLambdaStackTrace.lambda$main$0(PrintLambdaStackTrace.java:6)

This is the method which actually implements lambda's behavior. It belongs to the PrintLambdaStackTrace class.

like image 76
Marko Topolnik Avatar answered Nov 02 '22 22:11

Marko Topolnik