Lambda rewriting heuristics

On this page

Still need help?

The Atlassian Community is here for you.

Ask the community

Following document applies for version 4.0.5 or above

 

Clover applies some heuristic methods in order to reduce compilation errors connected with lambda expression instrumentation which are described with more details in here. During instrumentation Clover looks for invocation of some of java.util.stream.Stream and Guava classes' methods which are called with a lambda expression as an argument. With such conditions Clover rewrites lambda expressions into a block form.

With implemented heuristic, such code:

public static Stream<String> test(List<String> input) {
    return input.stream()
            .map(e -> e.toUpperCase())
            .filter(e -> !e.isEmpty());
}

will be instrumented with the following manner:

public static Stream<String> test(List<String> input) {  
    return input.stream()
            .map(e ->  {return e.toUpperCase();})
            .filter(e -> {return !e.isEmpty();});
}

 

This method has its own limitations, however. It does not apply to method references (refer to this article), so we advice to use lambda expressions instead of method references or lower lambda instrumentation level to from "all" to "all_but_references" (see clover-setup).

The other issue you may encounter is compilation error about unexpected return value (look here). It happens when there's a method with name matching one of the listed below, but taking as an argument a lambda returning void. 

Method list for stream heuristics 

The following methods trigger lambda expression to block form rewriting by Clover:

  • collect
  • compose
  • concat
  • filter
  • flatMap
  • from
  • generate
  • iterate
  • min
  • map
  • max
  • peek
  • reduce
  • transform
  • transformValues
  • thenApply
  • thenCompose

 

Last modified on Jul 10, 2015

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.