Compilation error with Clover enabled - variable of type Object
Symptoms
Clover instrumented code fails to compile with following error:
src/LambdaAndStreams.java:35: error: cannot find symbol
.filter(e -> {__CLR4_0_500ibutyhjw.R.inc(17);return !e.isEmpty();});
^
symbol: method isEmpty()
location: variable e of type Object
src/LambdaAndStreams.java:35: error: incompatible types: Stream<Object> cannot be converted to Stream<String>
.filter(e -> {__CLR4_0_500ibutyhjw.R.inc(17);return !e.isEmpty();});
^
2 errors
Where original code looks like:
public static Stream<String> test(List<String> input) {
return input.stream()
.map(String::toUpperCase)
.filter(e -> !e.isEmpty());
}
Cause
Clover proxies lambda expression with method wrapper defined with generic signatures. Type inferring algorithm JDK 8 the compiler uses has its own limitations and the compiler may not be able to infer proper type when a method reference is used and which results in compilation error. Since it's not possible to apply the same heuristics for method references as Clover uses for lambda expressions compilation of instrumented code fails.
Solution
Rewrite method references into lambda expressions, ideally into lambda block, i.e
public static Stream<String> test(List<String> input) { return input.stream() .map(e -> e.toUpperCase) // was String::toUpperCase .filter(e -> !e.isEmpty()); }
Disable instrumentation of method references (since 4.0.5)
Use instrumentLambda = "all_but_reference", "block", "expression" or "none"
Ant - build.xml
<clover-setup instrumentLambda="all_but_reference"/>
Maven - pom.xml
<configuration> <instrumentLambda>all_but_reference</instrumentLambda> </configuration>
Grails - BuildConfig.groovy
clover { instrumentLambda = "all_but_reference" }
Eclipse
Open
Project Properties
>Clover
page >Instrumentation
tab.
InMiscellaneous
box select proper value inInstrument lambda functions
drop-down.IntelliJ IDEA
OpenFile
>Settings
>Project Settings
>Clover
page >Compilation
.
InMiscellaneous
box select proper value inInstrument lambda functions
drop-down.
Bug tracker:
- CLOV-1762Getting issue details... STATUS
- CLOV-1465Getting issue details... STATUS