Uploaded image for project: 'Clover'
  1. Clover
  2. CLOV-1371

Javac compilation fails for type-casted lambda expression instrumented by Clover

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Medium
    • 3.2.0
    • 3.2.0
    • Instrumentation
    • 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"));
      

      Attachments

        Activity

          People

            mparfianowicz Marek Parfianowicz
            mparfianowicz Marek Parfianowicz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: