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

Inner classes inside a package protected class cannot be used in lambda

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested with both Java 18.0.1 and Java 19 EA on Windows 10.

      A DESCRIPTION OF THE PROBLEM :
      Inner classes inside a package protected class cannot be used in lambda if accessed through a public sub class. This is true for inner classes, interfaces and enums.

      The compiler complains when the inner classes are used in lambda expression with "AbstractFoo.MyClass is defined in an inaccessible class or interface". But it's allowed to use the inner classes in other type of expressions, like normal variable declaration. Is this an expected behavior or a bug?

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the source code using javac.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Successfully compiled
      ACTUAL -
      error: AbstractFoo.MyClass is defined in an inaccessible class or interface

      ---------- BEGIN SOURCE ----------
      package com.acme.subpackage;

      /* package protected */ class AbstractFoo {

          public static class MyClass {}

          public enum MyEnum { VALUE1, VALUE2 }

          public interface MyInterface {}

      }

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

      package com.acme.subpackage;

      public final class Foo extends AbstractFoo {}

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

      package com.acme;

      import com.acme.subpackage.Foo;

      import java.util.function.Predicate;

      public class TestMain {

          public static void main(String[] args) {

              // Works
              final Foo.MyClass myClass1 = new Foo.MyClass();

              // Fails with: "AbstractFoo.MyClass is defined in an inaccessible class or interface"
              final Predicate<Foo.MyClass> predicate1 = myClass2 -> false;

              // Works
              final Foo.MyEnum myEnum1 = Foo.MyEnum.VALUE1;

              // Fails with: "AbstractFoo.MyEnum is defined in an inaccessible class or interface"
              final Predicate<Foo.MyEnum> predicate2 = myEnum2 -> false;

              // Works
              final Foo.MyInterface myInterface1;

              // Fails with: "AbstractFoo.MyInterface is defined in an inaccessible class or interface"
              final Predicate<Foo.MyInterface> predicate3 = myInterface2 -> false;
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: