Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8266876

A method reference to an overriding static function fails to compile

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Worked on java 1.8.0_231 Oracle
      Not working on java 11.0.3 Oracle

      A DESCRIPTION OF THE PROBLEM :
      When you refer to a static method by method reference, if it shares the name with a non static method, the code will fail to compile, even if the signatures are different

      REGRESSION : Last worked in version 8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Successful compilation and run
      ACTUAL -
      Compilation error with message:
      Error:(26, 32) java: incompatible types: cannot infer type-variable(s) R
          (argument mismatch; invalid method reference
            unexpected static method setField(Bob) found in unbound lookup)

      ---------- BEGIN SOURCE ----------
      import com.google.common.collect.ImmutableSet;
      import org.junit.jupiter.api.Nested;
      import org.junit.jupiter.api.Test;

      import java.util.Set;
      import java.util.function.Function;

      public class Bob {

          public static Function<Object, Bob> setField(final Bob builder) {
              return builder::setField;
          }

          public Bob setField(final Object t) {
              return this;
          }

          @Nested
          class BobTest {

              @Test
              void test() {
                  final Set<Bob> bobSet = ImmutableSet.of(new Bob());
                  bobSet.stream().map(Bob::setField);
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Rename the static function

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            adev ANUPAM DEV
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: