尝试针对 javax.swing.text 中的 AbstractDocument.UndoRedoDocumentEvent 进行实例化时出现 IllegalAccessError

发布于 2024-10-31 11:58:58 字数 2143 浏览 5 评论 0原文

javax.swing.text.DefaultCaret.Handler.insertUpdate(DocumentEvent) 的源代码中,我发现了以下几行(从第 1685 行开始):

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

但是当我尝试这样做时:

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");
        }
    }
}

它会给出:

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)

为什么我可以不是针对该类的 instanceof ,但是 DefaultCaret.Handler 可以吗?

使用java版本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)

更新:

根据答案,我尝试了以下操作:

文件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");
        }
    }
}

文件Outer.java

package javax.swing.text;

public class Outer {
    class Inner {
    }
}

这有效很好并按预期打印“aww”。

请注意,这两个文件都位于 javax.swing.text 包中。另请注意,Foo.java 已经位于我最初问题中的 javax.swing.text 包中。

据我所知,包裹没有“密封”。 rt.jar(包含 javax.swing.text 包)的 MANIFEST 不包含“Sealed”。命令 Package.getPackage("javax.swing.text").isSealed() 返回 false。

因此,我可以针对我自己的内部类进行实例,但不能针对AbstractDocument.UndoRedoDocumentEvent,尽管包中的其他类可以。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独陪着我 2024-11-07 11:58:58

看起来 UndoRedoDocument 是受包保护的,并且 DefaultCaret.HandlerUndoRedoDocument 位于同一个包中(javax.swing.text 如果我没记错的话)。

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

梦回梦里 2024-11-07 11:58:58

第一个想法是 javax.swing.text 包是密封的,并且不允许您向其中添加新类。你自己的包裹也会发生同样的情况吗?


编辑
从我的评论来看:

有点晚了,但我认为原因可能是你的课程和摇摆课程已加载
由不同的类加载器加载,并且从不同的类加载器加载的包不是
即使它们具有相同的名称,也被视为相同的包。

您可以找出 Name.class.getClassLoader()object.getClass().getClassLoader() 使用哪个类加载器。打印这些。 (可能swing类的ClassLoader为null,代表引导类加载器(由VM实现,在ClassClassLoader<之前创建) /code> 类加载器很可能是另一个类加载器。

要使用您自己的类加载器创建类,请使用 URLClassLoader。这应该将您的应用程序的类加载器作为父级(以允许类访问每个类)。其他),但使用另一个 URL(例如加载不在通常类路径上的类)。

您可以执行以下操作:

  • 将您的 Outer 类放入应用程序的类路径
  • 中。创建一个主类(在同一类路径中),如下所示:

    类主要{
        公共静态无效主要(字符串[]被忽略)抛出异常{
            URL url = new URL("文件:///path/to/other/dir");
            ClassLoader cl = new URLClassLoader(url, Main.class.getClassLoader());
            类 testClass = cl.loadClass("package.Test");
            testClass.getMethod("测试").invoke(null);
        }
    }
    
  • 在与 Outer 相同的包中创建一个这样的测试类,但以主类中的 URL 为根:

    类测试{
       公共静态无效测试(){
          对象 o = 新对象();
          if (o 外部实例。内部) {
              System.out.println("耶");
          } 别的 {
              System.out.println("aww");
         }
       }
    }
    

如果我的理论是正确的,这应该会给出相同类型的异常。 (我没有尝试过。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文