Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8314621

ClassNotFoundException due to lambda reference to elided anonymous inner class

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 22
    • 10, 11, 14, 17, 20, 21, 22
    • tools
    • b25
    • generic
    • generic

    Description

      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


      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: