JDK-8318160 : javac does not reject private method reference with type-variable receiver
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 22
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2023-10-16
  • Updated: 2023-10-31
  • Resolved: 2023-10-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 22
22 b21Fixed
Related Reports
CSR :  
Relates :  
Relates :  
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
  }
}


Comments
Changeset: e2720987 Author: Vicente Romero <vromero@openjdk.org> Date: 2023-10-24 14:45:10 +0000 URL: https://git.openjdk.org/jdk/commit/e2720987b921b95fd8010cea60d2d6e436e5ebaa
24-10-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/16210 Date: 2023-10-17 00:19:56 +0000
17-10-2023

caused by JDK-8073842
16-10-2023