-
Bug
-
Resolution: Cannot Reproduce
-
Medium
-
3.2.0
-
None
NOT REPRODUCIBLE
In this code sample:
return map.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().getAverage()
));
the Collectors.toMap() takes a "Map.Entry::getKey" method reference as input argument. However, the getKey method is not static.
Wrapping it by Clover's lambdaInc() method leads to a compilation error like this:
[javac] symbol: method lambdaInc(int,Map.Entry::getKey,int) [javac] location: class __CLR4_0_22ci2cii4lxhoaj [javac] Foo.java:33: error: invalid method reference [javac] __CLR4_0_22ci2cii4lxhoaj.lambdaInc(3045,Map.Entry::getKey,3046), [javac] ^ [javac] non-static method getKey() cannot be referenced from a static context [javac] where K is a type-variable: [javac] K extends Object declared in interface Entry
Possible workarounds ??
1) Disable instrumentation of expression-like lambda functions (method references are treated as expression-like lambda) - use instrumentLambda="block" or "none".
2) Wrap such method reference with ///CLOVER:OFF and ///CLOVER:ON keywords, e.g.:
return map.entrySet().stream() .collect(Collectors.toMap( ///CLOVER:OFF Map.Entry::getKey, ///CLOVER:ON e -> e.getValue().getAverage() ));
3) Use a variable with a non-static method reference or change to a lambda expression.
return map.entrySet().stream() .collect(Collectors.toMap( e -> e.getKey(), // unfortunately IDEA suggests to change it back e -> e.getValue().getAverage() ));
[CLOV-1606] Java8: method reference to a non-static method fails to compile
Workflow | Original: New Clover Workflow [ 898074 ] | New: New Clover Workflow - Restricted [ 1474480 ] |
Workflow | Original: Clover Workflow [ 896532 ] | New: New Clover Workflow [ 898074 ] |
Workflow | Original: reviewflow [ 790364 ] | New: Clover Workflow [ 896532 ] |
Sprint | Original: Sprint 1 [ 1909 ] |
Rank | New: Ranked lower |
Sprint | New: Sprint 1 [ 1909 ] |
Rank | New: Ranked higher |
Sprint | Original: Sprint 2 [ 1910 ] |
Sprint | Original: Sprint 1 [ 1909 ] | New: Sprint 2 [ 1910 ] |
Fix Version/s | New: won't fix [ 38394 ] | |
Fix Version/s | Original: 4.0.4 [ 46891 ] | |
Resolution | New: Cannot Reproduce [ 5 ] | |
Status | Original: In Progress [ 3 ] | New: Closed [ 6 ] |