Name: mc57594 Date: 03/06/97
Create a bean with an inner class to handle mouse events for example.
Have the mouseDownEvent (or whatever event) try to access one
of the private vars of the containing class. Run this in the bean box.
A java.lang.IllegalAccessError is generated. Whenever the inner class
methods are called (i.e. by the dispatched delivering events.).
Note: This is using the JDK1.1 event model.
Note2: This is actually in the released BDK
company - Shafir Inc. , email - ###@###.###
======================================================================
This bug has nothing to do with beans. It is a compiler bug: sometimes
the compiler generates code allowing an inner class to access a private
variable in an outer class without accessors. If the linker is checking
for such accesses, you get an IllegalAccessError.
Here is example code that demonstrates the problem:
public class Outer
{
private int m_X;
protected int m_Y;
public static void main(String args[]) {
(new Outer()).test2();
}
public class Inner
{
public void test1() {
m_X = 1;
m_Y = 2;
}
}
public void test2() {
(new Inner()).test1();
}
}
Here is the code generated for test1() (from javap -c Outer\$Inner):
Method void test1()
0 aload_0
1 getfield #7 <Field Outer this$0>
4 iconst_1
5 putfield #5 <Field int m_X>
8 aload_0
9 getfield #7 <Field Outer this$0>
12 iconst_2
13 putfield #6 <Field int m_Y>
16 return
Interestingly, this thing runs just fine in spite of the fact that it
illegally accesses private fields in another class. Nakul tells
me that this is a known problem having to do with checks that are
disabled when loading from CLASSPATH, but a scan of bugs about CLASSPATH
didn't show me a bug report that looked like it might be about this.
I leave it to somebody else to file that bug if necessary.
nicholas.sterling@Eng 1997-06-09
Another customer has reported the bug that is demonstrated with the following code snippet:
Inner class cannot access private, protected fields of outer class
Description:
// The inner class "Inner" should be able to access the
// field "x" of the enclosing class. However, if "x" is
// declared private, the JDK 1.2EA1 appletviewer throws
// the exception java.lang.IllegalAccessError.
//
// Disassembling the Java bytecode reveals that the problem
// lies with the javac compiler. The compiler is supposed
// to generate accessor and update methods to get to the
// private and protected fields of the enclosing class, but
// the bytecode generated by javac tries to access "x"
// directly (which is a security violation, since Outer
// is a different class than Inner).
//
// This bug is present in all versions of JDK 1.1 as well.
public class Outer extends java.applet.Applet {
private int x = 0;
public void init () {new Inner().inc();}
class Inner {
void inc() {x++;}
}
}
- relates to
-
JDK-4059046 Setting private data member of an inner class doesn't work
-
- Closed
-