JDK-6754167 : IllegalAccessException thrown for successfully compiled and self-contained code
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-09-30
  • Updated: 2011-02-16
  • Resolved: 2008-09-30
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
When a call is made to a method that is specified in an interface and is implemented in a subclass of the current abstract type, and a private method with the same signature exists in a superclass, the code compiles without errors but the method call causes an IllegalAccessException at runtime. According to the JLS, the private method in the superclass is "not inherited", so I expect the correct behavior would be to invoke the subclass method.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expect "2" to be printed to standard out.
ACTUAL -
An IllegalAccessException is thrown.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.IllegalAccessError: tried to access method A1.getI()I from class A2
	at A2.test(AccessBug.java:23)
	at AccessBug.main(AccessBug.java:3)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
----------------AccessBug.java-----------------
public class AccessBug {
   public static void main(String[] args) {
      new A3().test();
   }
}

class A1 {
   private int getI() {
      return 1;
   }
}

interface I1 {
   int getI();
}

abstract class A2 extends A1 implements I1 {
   public void test() {
      // I expect this should print "2". According to the
   	// JLS A1.getI() is "not inherited" by A2, so I guess
   	// it should not affect method resolution. Instead,
   	// an IllegalAccessError is thrown at runtime.
      System.out.println(getI());
   }
}

class A3 extends A2 {
   public int getI() {
      return 2;
   }
}
------------------------------------------
---------- END SOURCE ----------

Comments
EVALUATION see comments on 6684387
30-09-2008