如何在 Rhino 中子类化内部(静态)类?
我正在尝试在 Rhino 中对内部类(在 Java 中定义)进行子类化,但我似乎无法使其工作。
我有一些已编译的Java代码(我基本上无法更改),它有一个内部抽象类:
package mypackage;
class MyClass {
abstract static class MyInnerClass {
abstract void print(String s);
}
}
从Rhino,我可以很好地看到它:
js> Packages.mypackage.MyClass.MyInnerClass
[JavaClass mypackage.MyClass$MyInnerClass]
但我不知道如何子类化它。我认为这样的事情会起作用,因为我是为非内部类这样做的:
var a = new JavaAdapter(Packages.mypackage.MyClass.MyInnerClass, {
print: function(s) { print("s=" + s); },
});
它不仅不起作用,Rhino 本身也会退出,并给我相当多的堆栈跟踪:
Exception in thread "main" java.lang.IllegalAccessError: class adapter1 cannot access its superclass mypackage.MyClass$MyInnerClass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:62)
...
我做错了吗?有什么想法吗?
编辑:哦,我刚刚意识到 MyClass 没有声明为“公共”。会是这样吗?
I'm trying to subclass an inner class (defined in Java) in Rhino, and I can't seem to make it work.
I've got some compiled Java code (which I essentially can't change) that has an inner abstract class:
package mypackage;
class MyClass {
abstract static class MyInnerClass {
abstract void print(String s);
}
}
From Rhino, I can see it just fine:
js> Packages.mypackage.MyClass.MyInnerClass
[JavaClass mypackage.MyClass$MyInnerClass]
But I can't figure out how to subclass it. I figured something like this would work, since I do it for non-inner classes:
var a = new JavaAdapter(Packages.mypackage.MyClass.MyInnerClass, {
print: function(s) { print("s=" + s); },
});
Not only does it not work, Rhino itself quits, and gives me quite the stack trace:
Exception in thread "main" java.lang.IllegalAccessError: class adapter1 cannot access its superclass mypackage.MyClass$MyInnerClass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:62)
...
Am I doing it wrong? Any ideas?
EDIT: Oh, I just realized that MyClass isn't declared "public". Could that be it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内部类必须是公共的。
第一次测试:子类化并实例化外部
Foo
类:第二次测试:子类化并实例化内部类:
注意 :如果我将内部类更改为不公开,我会得到与您报告的完全相同的错误。
The inner class must be public.
First test: Subclass and instantiate the outer
Foo
class:Second test: Subclass and instantiate the inner class:
Note: If I change the inner class not to be public, I get the exact same error you reported.
由于它的可见性是包私有的,您只能在同一个包中创建它的子类,也许这就是问题所在?
Since it's visibility is package private you may only create sub class of it in same package, maybe that's the problem?