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

Jan Lahoda jlahoda at openjdk.org
Thu Nov 16 13:48:32 UTC 2023


On Thu, 16 Nov 2023 04:29:51 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> 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

Looks OK to me.

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

Marked as reviewed by jlahoda (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/16683#pullrequestreview-1734409564


More information about the compiler-dev mailing list