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

IllegalAccessError when using a public method reference of a package-private class through a public subclass from another package

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      other

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_74"
      Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The problem seems to manifest itself while using a public method reference of a package-private class through a public subclass from another package.

      Please consider the following two classes which reside in "package1":

      -----------
      Foo.java
      -----------
      package package1;

      abstract class Foo {

      public String getFoo() {
      return "foo";
      }

      }

      -----------
      Bar.java
      -----------
      package package1;

      public class Bar extends Foo {

      public String getBar() {
      return "bar";
      }

      }

      Now please consider the following class which reside in "package2":

      --------------------
      Something.java
      --------------------

      package package2;

      import java.util.stream.Stream;

      import package1.Bar;

      public class Something {

      public static void main(String[] args) {

              System.out.println(new Bar().getFoo());
              // "foo"
              
              Stream.of(new Bar()).map(Bar::getFoo).forEach(System.out::println);
              // IllegalAccessError

          }

      }

      As you can see from the comments in the code:
      - directly accessing the public method "getFoo" of the package-private class "Foo" through
      its subclass "Bar" from the "package2" does not result in any error;
      - using a method reference of the public method "getFoo" of the package-private class "Foo" through its subclass "Bar" in the "package2" results in an IllegalAccessError.

      The problem seems to be very similar to the following:
      https://bugs.openjdk.java.net/browse/JDK-8029707


      REPRODUCIBILITY :
      This bug can be reproduced always.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                aroy Abhijit Roy (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: