Why is this method reference failing at runtime but not the corresponding lambda call?

This appears to be a bug in certain Java versions.

I can replicate it if I compile and run it with JDK 8, specifically:

tj$ javac -version
javac 1.8.0_74
tj$ java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

...but not with JDK 11 or 12, specifically:

tj$ javac -version
javac 11.0.1
tj$ java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

and

tj$ javac -version
javac 12.0.2
tj$ java -version
java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)

I can also replicate it if I compile with JDK 8 but run it with JDK 12's runtime, suggesting a compilation problem.


This is a bug:

Method reference uses wrong qualifying type.

A reference to a method declared in a package-access class (via a public subtype) compiles to a lambda bridge; the qualifying type in the bridge method is the declaring class, not the referenced class. This leads to an IllegalAccessError.

Fixed in Java 9.

Tags:

Java

Eclipse