Lambda rewriting heuristics


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 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) {
            .map(e -> e.toUpperCase())
            .filter(e -> !e.isEmpty());

will be instrumented with the following manner:

public static Stream<String> test(List<String> input) {
            .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