ADDITIONAL SYSTEM INFORMATION :
Tested on x86-64 Linux with the following Java versions:
openjdk version "22-ea" 2024-03-19
openjdk version "17.0.6" 2023-01-17
openjdk version "14.0.2" 2020-07-14
openjdk version "13.0.5" 2020-10-20
openjdk version "12.0.2" 2019-07-16
openjdk version "11.0.16" 2022-07-19
openjdk version "10.0.2" 2018-07-17
A DESCRIPTION OF THE PROBLEM :
javac will sometimes elide an anonymous inner class that it determines is never constructed, but if the anonymous type is captured by a `var` variable that is closed over by a lambda, evaluating the lambda expression will fail due to the missing class.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac Test.java
java Test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program should run without raising ClassNotFoundException
ACTUAL -
Program raises ClassNotFoundException at runtime
---------- BEGIN SOURCE ----------
public class Test {
private static final boolean FOO = false;
public static void main(String[] argv) {
var o = FOO? new Object() {} : null;
Runnable r = () -> {
System.out.println(o == o);
};
r.run();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use a named inner class instead, eg:
class O {}
var o = FOO? new O() : null;
FREQUENCY : always
Tested on x86-64 Linux with the following Java versions:
openjdk version "22-ea" 2024-03-19
openjdk version "17.0.6" 2023-01-17
openjdk version "14.0.2" 2020-07-14
openjdk version "13.0.5" 2020-10-20
openjdk version "12.0.2" 2019-07-16
openjdk version "11.0.16" 2022-07-19
openjdk version "10.0.2" 2018-07-17
A DESCRIPTION OF THE PROBLEM :
javac will sometimes elide an anonymous inner class that it determines is never constructed, but if the anonymous type is captured by a `var` variable that is closed over by a lambda, evaluating the lambda expression will fail due to the missing class.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac Test.java
java Test
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program should run without raising ClassNotFoundException
ACTUAL -
Program raises ClassNotFoundException at runtime
---------- BEGIN SOURCE ----------
public class Test {
private static final boolean FOO = false;
public static void main(String[] argv) {
var o = FOO? new Object() {} : null;
Runnable r = () -> {
System.out.println(o == o);
};
r.run();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use a named inner class instead, eg:
class O {}
var o = FOO? new O() : null;
FREQUENCY : always