-
Bug
-
Resolution: Fixed
-
P3
-
22
-
b21
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
}
}
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
}
}
- csr for
-
JDK-8318203 javac does not reject private method reference with type-variable receiver
- Closed
- relates to
-
JDK-6711619 javac doesn't allow access to protected members in intersection types
- Closed
-
JDK-8073842 Invalid method reference when referencing a method on a wildcard type
- Closed