Compilation error with Clover enabled - unexpected return value
症状
Clover-instrumented code fails to compile with a following error:
src/LambdaAndStreams.java:75: error: incompatible types: bad return type in lambda expression
mapClass.map((internalMap) -> {__CLR4_0_500ibutb8q0.R.inc(50);return internalMap.put("some", "some");});
^
unexpected return value
1 error
原因
Since version 4.0.5, Clover tries to resolve some compilation errors listed here by rewriting some of lambda expressions to lambda blocks. Clover looks for JDK java.util.stream.Stream's
and some of Guava
classes' with lambda expression as an argument and rewrites them into a lambda block. However, if the lambda passed as argument returns void, it will cause a compilation error:
For example, a lambda expression:
myList.map((a) -> System.out.println("Hello: " + a))
will be rewritten as follows:
myList.map((a) -> { return System.out.println("Hello: " + a);})
The full list of methods which are affected can be found here
ソリューション
1) Please use CLOVER:VOID
directive to instruct Clover that this specific lambda does not return a value, as follows:
(a) -> /*CLOVER:VOID*/ System.out.println("Hello: " + a)
or
(a) -> //CLOVER:VOID
System.out.println("Hello: " + a)
2) Alternatively, you can disable instrumentation of lambda functions (use instrumentLambda = block or none).