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

Visibility rules not applied for lambda when referencing class with inner class

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      In package a, there's a Outer with a _private_ class inner Inner, which in turn contains a _public_ inner class Supplier. Outer has a method with a parameter of type Supplier. In package b, I invoke that method with a Lambda expression.

      I'd expect compilation to fail as the Lambda implements a type which isn't accessible from package b, but compilation succeeds.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      mkdir -p compiler-bug/src/a compiler-bug/src/b compiler-bug/classes
      cd compiler-bug

      cat > src/a/Outer.java <<EOF
      package a;

      public class Outer {

        private static class Inner {

          @FunctionalInterface
          public interface Supplier {
            int getInt();
          }
        }

        public static void invoke(Inner.Supplier supplier) {}
      }
      EOF

      cat > src/b/Reference.java <<EOF
      package b;

      import a.Outer;

      public class Reference {

        public static void main(String[] args) {
          Outer.invoke(() -> 42);
        }
      }
      EOF

      javac src/a/Outer.java src/b/Reference.java -d classes

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Compilation fails. E.g. the Eclipse compiler ecj reports:

      "The type Outer.Inner from the descriptor computed for the target context is not visible here."
      ACTUAL -
      Compilation succeeds.

      ---------- BEGIN SOURCE ----------
      See "Steps to Reproduce".
      ---------- END SOURCE ----------

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

              Created:
              Updated: