JDK-8152410 : IllegalAccessError when using a public method reference of a package-private class through a public subclass from another package
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: other
  • CPU: x86
  • Submitted: 2016-03-20
  • Updated: 2016-03-31
  • Resolved: 2016-03-30
Related Reports
Duplicate :  
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.


Comments
This issue is duplicate of JDK-8143647 which is fixed for 9 ea build-96 and backported to 8 in build 102. One has to update to latest version of 9, or has to wait for get release the 8 build b102.
30-03-2016

This issue has been fixed in JDK 8. Backport: JDK-8152643
29-03-2016

Test Result: *************** OS : Windows 7 64-bit JDK : ##### 8uXX: Fail 9ea (up to b95): Fail 9ea (b96 onwards): Pass ---------------------------------------------------------------------------------------- output: ###### c:\MyJava\9032740>java -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) c:\MyJava\9032740>javac -d . *.java c:\MyJava\9032740>ls Bar.java Foo.java Something.java package1 package2 c:\MyJava\9032740>java package2/Something foo Exception in thread "main" java.lang.IllegalAccessError: tried to access class package1.Foo from class package2.Something at package2.Something.lambda$main$0(Something.java:14) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) at package2.Something.main(Something.java:14) c:\MyJava\9032740> ============================================================================================================== c:\MyJava\9032740>java -version java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+110) Java HotSpot(TM) 64-Bit Server VM (build 9-ea+110, mixed mode) c:\MyJava\9032740>javac -d . *.java c:\MyJava\9032740>java package2/Something foo foo c:\MyJava\9032740>
21-03-2016

Have a you into this
20-03-2016