我正在使用 Mozilla 的 Rhino 实现(不是与 JDK 捆绑的那个)。阅读此主题后:如何在 Rhino 中子类化内部(静态)类?- 我认为我在正确的轨道上,但以下实现失败:
Exception in thread "main" org.mozilla.javascript.EvaluatorException: Access to Java class "adapter1" is prohibited. (test#2)
at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:913)
at org.mozilla.javascript.Context.reportRuntimeError(Context.java:969)
at org.mozilla.javascript.Context.reportRuntimeError1(Context.java:932)
at org.mozilla.javascript.JavaMembers.<init>(JavaMembers.java:35)
at org.mozilla.javascript.JavaMembers.lookupClass(JavaMembers.java:807)
at org.mozilla.javascript.NativeJavaObject.initMembers(NativeJavaObject.java:54)
at org.mozilla.javascript.NativeJavaObject.<init>(NativeJavaObject.java:44)
at org.mozilla.javascript.JavaAdapter.createAdapterWrapper(JavaAdapter.java:107)
at adapter1.<init>(<adapter>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.mozilla.javascript.JavaAdapter.js_createAdapter(JavaAdapter.java:205)
at org.mozilla.javascript.JavaAdapter.execIdCall(JavaAdapter.java:86)
at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
at org.mozilla.javascript.BaseFunction.construct(BaseFunction.java:343)
at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:2349)
at org.mozilla.javascript.gen.test_1._c_script_0(test:2)
at org.mozilla.javascript.gen.test_1.call(test)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen.test_1.call(test)
at org.mozilla.javascript.gen.test_1.exec(test)
爪哇:
package com.rebelstudios.scripting;
public abstract class Test
{
public abstract void x();
public static void main(String[] _) throws Throwable
{
Context cx = Context.enter();
ScriptableObject prototype = cx.initStandardObjects();
Scriptable topLevel = new ImporterTopLevel(cx);
prototype.setParentScope(topLevel);
Scriptable scope = cx.newObject(prototype);
scope.setPrototype(prototype);
Reader reader = new FileReader("test.js");
org.mozilla.javascript.Script script = cx.compileReader(reader, "test", 0, null);
script.exec(cx, scope);
}
}
JS:
var derived = new JavaAdapter(
Packages.com.rebelstudios.scripting.Test,
{
x: function() { }
}
);
已经很晚了,我可能会遗漏一些明显的东西,但是有人可以提供一些提示吗?谢谢!
PS 我也非常感谢任何关于使用 Mozilla Rhino 的优秀文档、示例和教程的链接。从我从搜索中收集到的信息中,似乎没有太多连贯的信息。