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

Java8: method reference to a non-static method fails to compile

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: Medium Medium
    • won't fix
    • 3.2.0
    • Instrumentation
    • 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

            Owen made changes -
            Workflow Original: New Clover Workflow [ 898074 ] New: New Clover Workflow - Restricted [ 1474480 ]
            Piotr Swiecicki made changes -
            Workflow Original: Clover Workflow [ 896532 ] New: New Clover Workflow [ 898074 ]
            Piotr Swiecicki made changes -
            Workflow Original: reviewflow [ 790364 ] New: Clover Workflow [ 896532 ]
            Marek Parfianowicz made changes -
            Sprint Original: Sprint 1 [ 1909 ]
            Marek Parfianowicz made changes -
            Rank New: Ranked lower
            Marek Parfianowicz made changes -
            Sprint New: Sprint 1 [ 1909 ]
            Marek Parfianowicz made changes -
            Rank New: Ranked higher
            Marek Parfianowicz made changes -
            Sprint Original: Sprint 2 [ 1910 ]
            Marek Parfianowicz made changes -
            Sprint Original: Sprint 1 [ 1909 ] New: Sprint 2 [ 1910 ]
            Marek Parfianowicz made changes -
            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 ]

              mparfianowicz Marek Parfianowicz
              mparfianowicz Marek Parfianowicz
              Affected customers:
              0 This affects my team
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: