如何在 Rhino 中子类化内部(静态)类?

发布于 2024-10-10 01:57:43 字数 1043 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(2

初与友歌 2024-10-17 01:57:43

内部类必须是公共的。

package org.example;

public class Foo
{
    public abstract static class MyInnerClass
    {
        abstract void print(String s);
    }

    public void pr()
    {
        System.out.println("foo");
    }
}

第一次测试:子类化并实例化外部 Foo 类:

$ java -classpath $PWD:/usr/share/java/js.jar:/usr/share/java/jline.jar:. org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 2 2010 01 20
js> var a = new JavaAdapter(Packages.org.example.Foo, {
  >   pr: function() { print("jsfoo"); },
  > });
js> a.pr();
jsfoo

第二次测试:子类化并实例化内部类:

js> var b = new JavaAdapter(Packages.org.example.Foo.MyInnerClass, {
  >   print: function(s) { print("Inner: " + s); },
  > });
js> b.print("one");
Inner: one
undefined

注意 :如果我将内部类更改为不公开,我会得到与您报告的完全相同的错误。

The inner class must be public.

package org.example;

public class Foo
{
    public abstract static class MyInnerClass
    {
        abstract void print(String s);
    }

    public void pr()
    {
        System.out.println("foo");
    }
}

First test: Subclass and instantiate the outer Foo class:

$ java -classpath $PWD:/usr/share/java/js.jar:/usr/share/java/jline.jar:. org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 2 2010 01 20
js> var a = new JavaAdapter(Packages.org.example.Foo, {
  >   pr: function() { print("jsfoo"); },
  > });
js> a.pr();
jsfoo

Second test: Subclass and instantiate the inner class:

js> var b = new JavaAdapter(Packages.org.example.Foo.MyInnerClass, {
  >   print: function(s) { print("Inner: " + s); },
  > });
js> b.print("one");
Inner: one
undefined

Note: If I change the inner class not to be public, I get the exact same error you reported.

小帐篷 2024-10-17 01:57:43

由于它的可见性是包私有的,您只能在同一个包中创建它的子类,也许这就是问题所在?

Since it's visibility is package private you may only create sub class of it in same package, maybe that's the problem?

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