RFR: 8314621: ClassNotFoundException due to lambda reference to elided anonymous inner class

Vicente Romero vromero at openjdk.org
Thu Nov 16 04:35:44 UTC 2023


If code like the one below is executed:


class ClassNotFoundExceptionDueToPrunedCodeTest {
    public static void main(String... args) {
        var o = false ? new Object() {} : null;
        Runnable r = () -> {
            System.out.println(o == o);
        };
        r.run();
    }
}

the runtime throws ClassNotFoundException. What is happening here is that the compiler is doing some "optimizations" basically in this case not generating code for the "true part" of the conditional as it will never be executed. The issue here is that the type of `o` will be the type of the anonymous class pruned by this optimization. Later on `o` is captured by the lambda and thus the exception. The proposed solution here is not to prune dead code if it is defining an anonymous class to avoid similar situations.
TIA

-------------

Commit messages:
 - 8314621: ClassNotFoundException due to lambda reference to elided anonymous inner class

Changes: https://git.openjdk.org/jdk/pull/16683/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=16683&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8314621
  Stats: 216 lines in 5 files changed: 68 ins; 142 del; 6 mod
  Patch: https://git.openjdk.org/jdk/pull/16683.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/16683/head:pull/16683

PR: https://git.openjdk.org/jdk/pull/16683


More information about the compiler-dev mailing list