Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalAccessError when trying instanceof against AbstractDocument.UndoRedoDocumentEvent from javax.swing.text

In the source of javax.swing.text.DefaultCaret.Handler.insertUpdate(DocumentEvent) I found the following lines (starting at line 1685):

if (e instanceof AbstractDocument.UndoRedoDocumentEvent) {
    setDot(offset + length);
    return;
}

But when I try this:

package javax.swing.text;

public class Foo {
    public static void main(String[] args) {
        Object o = new Object();
        if (o instanceof AbstractDocument.UndoRedoDocumentEvent) {
            System.out.println("yay");
        } else {
            System.out.println("aww");
        }
    }
}

it will give:

Exception in thread "main" java.lang.IllegalAccessError: tried to access class javax.swing.text.AbstractDocument$UndoRedoDocumentEvent from class javax.swing.text.Foo
    at javax.swing.text.Foo.main(Foo.java:6)

Why can I not instanceof against that class, but DefaultCaret.Handler can?

Using java version 1.6.0_20

$ java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.7) (6b20-1.9.7-0ubuntu1~10.04.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)

UPDATE:

Based on the answers I tried the following:

file Foo.java:

package javax.swing.text;

public class Foo {
    public static void main(String[] args) {
        Object o = new Object();
        if (o instanceof Outer.Inner) {
            System.out.println("yay");
        } else {
            System.out.println("aww");
        }
    }
}

file Outer.java:

package javax.swing.text;

public class Outer {
    class Inner {
    }
}

This works fine and prints "aww" as expected.

Note that both files are in the package javax.swing.text. Also note that Foo.java already was in the package javax.swing.text in my original question.

As far as I can tell the package is not "sealed". The MANIFEST of rt.jar (the one containing the package javax.swing.text) did not contain "Sealed". The command Package.getPackage("javax.swing.text").isSealed() returns false.

So I can instance of against my own inner class, but not against AbstractDocument.UndoRedoDocumentEvent, even though other classes from the package can.

Any ideas why this is?

like image 881
Lesmana Avatar asked Oct 11 '22 08:10

Lesmana


1 Answers

It looks like UndoRedoDocument is package-protected, and DefaultCaret.Handler and UndoRedoDocument are in the same package (javax.swing.text if I remember correctly).

like image 155
EMMERICH Avatar answered Oct 27 '22 00:10

EMMERICH