尝试针对 javax.swing.text 中的 AbstractDocument.UndoRedoDocumentEvent 进行实例化时出现 IllegalAccessError
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
UndoRedoDocument
是受包保护的,并且DefaultCaret.Handler
和UndoRedoDocument
位于同一个包中(javax.swing.text
如果我没记错的话)。It looks like
UndoRedoDocument
is package-protected, andDefaultCaret.Handler
andUndoRedoDocument
are in the same package (javax.swing.text
if I remember correctly).第一个想法是 javax.swing.text 包是密封的,并且不允许您向其中添加新类。你自己的包裹也会发生同样的情况吗?
编辑:
从我的评论来看:
您可以找出
Name.class.getClassLoader()
或object.getClass().getClassLoader()
使用哪个类加载器。打印这些。 (可能swing类的ClassLoader为null
,代表引导类加载器(由VM实现,在Class
和ClassLoader<之前创建) /code> 类加载器很可能是另一个类加载器。
要使用您自己的类加载器创建类,请使用 URLClassLoader。这应该将您的应用程序的类加载器作为父级(以允许类访问每个类)。其他),但使用另一个 URL(例如加载不在通常类路径上的类)。
您可以执行以下操作:
Outer
类放入应用程序的类路径中。创建一个主类(在同一类路径中),如下所示:
在与 Outer 相同的包中创建一个这样的测试类,但以主类中的 URL 为根:
如果我的理论是正确的,这应该会给出相同类型的异常。 (我没有尝试过。)
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:
You can find out which class loader is used by
Name.class.getClassLoader()
, orobject.getClass().getClassLoader()
. Print these. (It may be that the ClassLoader of the swing class isnull
, which represents the bootstrap class loader (implemented by the VM, created before theClass
andClassLoader
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:
Outer
class in the application's class path.create a main class (in the same class path) like this:
create a test class like this in the same package as Outer, but rooted at the URL in the main class:
If my theory is correct, this should give the same type of exception. (I did not try it.)