Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpret Java stack trace with lambdas such as ".lambda$null$2" & and "$$Lambda$"

I'm looking for a definition of the parts that occurs around a lambda in the Java 8 stack trace

For example this code

Object inputData = someData();

myList.stream().forEach(listItem -> {

    Query query = (Query) listItem.getSingle().apply(this.getId());

    Object data = diffUtils.applyProjection(query, inputData);

    myStringCollection.stream()
       .filter(destination -> myPredicateMethod(listItem, destination))
       .forEach(destination -> myProcessMethod(destination, data));
}

sometimes produces this exception.

at [CLASS].lambda$null$2([CLASS].java:85)
at [CLASS]$$Lambda$64/730559617.accept(Unknown Source)

What is the different parts describing in the exception? What is the "null", "2", "64", "730559617" and "Unknown Source" telling me?

A more elaborated example can be found here (but here the "null" in my exception above is not present). http://blog.takipi.com/the-dark-side-of-lambda-expressions-in-java-8/

like image 910
Andreas Lundgren Avatar asked Jul 28 '15 17:07

Andreas Lundgren


People also ask

How do you analyze a stack trace?

Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.

What is stack trace in Java with example?

The stack trace includes information about program subroutines and can be used to debug or troubleshoot and is often used to create log files. These exceptions could be custom (defined by the user) or built-in. Examples include RuntimeException , NullPointerException , and ArrayIndexOutofBoundsException .

What is lambdas in Java?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What is the use of lambda expression in Java stack overflow?

The lambda code nested inside a method inside a class can access any effectively-final variables found within that method & class. Creating a class that implements the functional interface does not give you such direct access to the state of the calling code.


1 Answers

There is no standard definition for that this generated class name is. This is deliberate to avoid you writing code which depends on it making it harder for the designers to change it later.

That being said, what little you can read is;

  • the first part of the class name is the class of the call site.
  • the number before the last $ is global counter for lambda. This depends on the order in which code for lambdas is generated.
  • the big number is a generated id. It is different for the same lambda each time you run but doesn't change once you have started.

The "Unknown Source" is telling you this generated code has no debug information associated with it.

We are looking at a library for changing the toString for a lambda to give you an idea of code associated with it. i.e. it will look like the code of the lambda at least for trivial cases.

like image 50
Peter Lawrey Avatar answered Oct 13 '22 00:10

Peter Lawrey