开发者

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

开发者 https://www.devze.com 2023-02-24 00:37 出处:网络
In the source of javax.swing.text.DefaultCaret.Handler.insertUpdate(DocumentEvent) I found the following lines (starting at line 1685):

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开发者_StackOverflow中文版 from the package can.

Any ideas why this is?


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


First idea would be that the package javax.swing.text is sealed, and you are not allowed to add new classes to it. Does the same happen with your own package?


Edit: From my comment:

A bit late, but I think a reason could be that your class and the swing classes are loaded by different class loaders, and packages loaded from different class loaders are not considered being the same package, even if they have the same name.

You can find out which class loader is used by Name.class.getClassLoader(), or object.getClass().getClassLoader(). Print these. (It may be that the ClassLoader of the swing class is null, which represents the bootstrap class loader (implemented by the VM, created before the Class and ClassLoader class is loaded). Your applications class loader is quite likely another one.

For creating a class with your own class loader, use an URLClassLoader. This should have your application's class loader as a parent (to allow the classes to access each other at all), but use another URL (e.g. load classes which are not on the usual class path).

You could do something like this:

  • put your Outer class in the application's class path.
  • create a main class (in the same class path) like this:

    class Main {
        public static void main(String[] ignored) throws Exception {
            URL url = new URL("file:///path/to/other/dir");
            ClassLoader cl = new URLClassLoader(url, Main.class.getClassLoader());
            Class<?> testClass = cl.loadClass("package.Test");
            testClass.getMethod("test").invoke(null);
        }
    }
    
  • create a test class like this in the same package as Outer, but rooted at the URL in the main class:

    class Test {
       public static void test() {
          Object o = new Object();
          if (o instanceof Outer.Inner) {
              System.out.println("yay");
          } else {
              System.out.println("aww");
         }
       }
    }
    

If my theory is correct, this should give the same type of exception. (I did not try it.)

0

精彩评论

暂无评论...
验证码 换一张
取 消