Details
-
Bug
-
Resolution: Fixed
-
Medium
-
3.2.0
-
None
Description
A following code:
Object o = (Runnable) () -> System.out.println("lambda expression with class cast"); ((Runnable)o).run();
is being instrumented by Clover as follows:
Object o = (Runnable) RECORDER.lambdaInc(111, () -> System.out.println("lambda expression with class cast")); ((Runnable)o).run();
As a result javac is unable to compile such code - it cannot infer proper type from the context:
error: incompatible types: cannot infer type-variable(s) I,T
(argument mismatch; Object is not a functional interface)
where I,T are type-variables:
I extends Object declared in method <I,T>lambdaInc(int,T)
T extends I declared in method <I,T>lambdaInc(int,T)
Workaround:
Change lambda from expression to block:
Object o = (Runnable) () -> { System.out.println("lambda expression with class cast"); };
or
Remove class cast:
Runnable o = () -> System.out.println("lambda expression with class cast");
Possible fix:
Wrap class cast inside lambdaInc():
Object o = RECORDER.lambdaInc(111, (Runnable) () -> System.out.println("lambda expression with class cast"));