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

IllegalAccessError using functional consumer calling inherited method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • hs25
    • 8
    • hotspot
    • linux_ubuntu

      FULL PRODUCT VERSION :
      java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b118)
      Java HotSpot(TM) Server VM (build 25.0-b60, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Ubuntu 12.10 32bits

      A DESCRIPTION OF THE PROBLEM :
      Given a package private class with a public method and a public subclass that inherits the public method, if we try to use the inherited public method using a lambda expression like:
      c.forEach(subclass::inheritedMethod);

      we get an java.lang.IllegalAccessError

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Considering the following scenario:
      A package 'mypackage' containing a package private class called MyPackagePrivateBaseClass:
      class MyPackagePrivateBaseClass {

          public void doSomething(String input) {
              System.out.println(input);
          }
      }
      and a public subclass called MyPublicClass:
      public class MyPublicClass extends MyPackagePrivateBaseClass {
      }

      If I try to access the inherited public method 'doSomething' from a class in a different package using a lambda expression I get a 'java.lang.IllegalAccessError':

      public class Test {
          public static void main(String[] args) {
              doesWork();
              doesNotWork();
          }

          public static void doesNotWork() {
              MyPublicClass victim = new MyPublicClass();
              List<String> items = Arrays.asList("first", "second", "third");
              items.forEach(victim::doSomething); //illegal access error here
          }

          public static void doesWork() {
              MyPublicClass victim = new MyPublicClass();
              List<String> items = Arrays.asList("first", "second", "third");
              for (String item : items) {
                  victim.doSomething(item);
              }
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      As per the example, it should print:
      first
      second
      third
      ACTUAL -
      Exception in thread "main" java.lang.IllegalAccessError: tried to access class mypackage.MyPackagePrivateBaseClass from class Test
      at Test.doesNotWork(Test.java:19)
      at Test.main(Test.java:13)


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.IllegalAccessError: tried to access class mypackage.MyPackagePrivateBaseClass from class Test
      at Test.doesNotWork(Test.java:19)
      at Test.main(Test.java:13)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package mypackage;
      class MyPackagePrivateBaseClass {

          public void doSomething(String input) {
              System.out.println(input);
          }
      }

      package mypackage;
      public class MyPublicClass extends MyPackagePrivateBaseClass {
      }

      import java.util.Arrays;
      import java.util.List;

      import mypackage.MyPublicClass;

      public class Test {
          public static void main(String[] args) {
              doesWork();
              doesNotWork();
          }

          public static void doesNotWork() {
              MyPublicClass victim = new MyPublicClass();
              List<String> items = Arrays.asList("first", "second", "third");
              items.forEach(victim::doSomething);
          }

          public static void doesWork() {
              MyPublicClass victim = new MyPublicClass();
              List<String> items = Arrays.asList("first", "second", "third");
              for (String item : items) {
                  victim.doSomething(item);
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Make public the private package parent class

            hseigel Harold Seigel (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: