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

javac does not reject private method reference with type-variable receiver

    XMLWordPrintable

Details

    • b21

    Description

      The following code compiles unexpectedly:

      import java.util.function.*;

      class Test {
        private String asString() {
          return "bar";
        }

        static <T extends Test> Function<T, String> foo() {
          return T::asString;
        }
      }


      Note that the type variable T has the bound Test. As per JLS 4.4, the members of T are the members of the intersection derived from T. In this case, as there's only one class bound, the intersection type is a notional class C that extends Test. Such a notional class does _not_ feature "asString" in its members.

      Note that when "asString" is called directly (e.g. w/o a method reference) the code fails as expected:

      class Test {
        private String asString() {
          return "bar";
        }

      static <T extends Test> String foo(T t) {
          return t.asString(); // error: "asString" has private access in Test
        }
      }

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              mcimadamore Maurizio Cimadamore
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: