JDK-6622385 : Accessing protected static methods
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-10-26
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7 Other
6u14Fixed 7Fixed hs13Fixed
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Example below throws a runtime exception
Exception in thread "main" java.lang.IllegalAccessError: tried to access method
ab.A.m()V from class c.C
        at c.C.c(C.java:8)
        at D.main(D.java:5)

It compiles and runs w/o exception in jdk 1.5.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
either a compile time error or no error as in jdk 1.5
ACTUAL -
Exception in thread "main" java.lang.IllegalAccessError: tried to access method
ab.A.m()V from class c.C
        at c.C.c(C.java:8)
        at D.main(D.java:5)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package ab;

public abstract class A{
	protected static void m(){
		System.out.println("A.m()");
	}
}
-----------------------------------------------------------------------
package ab;

public abstract class B extends A{
}
-----------------------------------------------------------------------
package c;

import ab.A;
import ab.B;

public class C extends A{
	public static void c(){
		B.m();
	}
}
------------------------------------------------------------------------
import c.C;

public class D {
	public static void main(String [] argv){
		C.c();
	}
}
------------------------------------------------------------------------
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
change a call B.m() to A.m()

Release Regression From : 6
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
WORK AROUND Launch java with the -XX:+RelaxAccessControlCheck command-line flag.
04-01-2008

EVALUATION Code in class C can access class B, since B is public. And, code in class C can access B.m because m is declared in A and C is a subclass of A (JLS 6.6.2, JVMS 5.4.4). The crucial point is that m is static, so there is no additional check at compile-time or run-time that the reference to m is through A (m's declaring class) or a subclass.
03-01-2008

EVALUATION The regression occurs in Mustang b70. The given test case works in b69, and fails in b70. gzilla[play]% for i in /java/re/jdk/6/promoted/all/{b69,b70} ; do echo $i ; $i/binaries/solaris-sparc/bin/javac $(find . -name \*.java ); $i/binaries/solaris-sparc/bin/java -classpath . D; done /java/re/jdk/6/promoted/all/b69 A.m() /java/re/jdk/6/promoted/all/b70 Exception in thread "main" java.lang.IllegalAccessError: tried to access method ab.A.m()V from class c.C at c.C.c(C.java:8) at D.main(D.java:5) gzilla[play]% There were no compiler (java/java/compiler) changes in b70. Period. This looks related to this java/runtime bug, which *was* fixed in b70. 4293149 Priv. member accessible if its visibility chgd after class using it is compiled [[ Of the other fixes in b70, one is java/install, one other is java/runtime but is to do with servlet annotatations, and the rest are java/classes_*. ]] Refiling against java/java/runtime.
03-01-2008

EVALUATION Test case confirmed.
03-01-2008