我使用Mozilla的Rhino实现(不是与JDK捆绑的实现)。阅读了这个帖子:How to subclass an inner (static) class in 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() { }
}
);
现在已经很晚了,我可能会遗漏一些明显的东西,但有人可以提供一些提示吗?谢谢!
P.S。我也非常感谢有关使用Mozilla Rhino的良好文档,示例和教程的任何链接。从我的搜索中收集到的信息似乎并不是很多连贯的信息。
答案 0 :(得分:0)
不需要使用JavaAdaper。您可以显式声明一个您要实例化的类,但是这个类必须能够通过反射进行实例化,因为Rhino不会像代码编织那样产生任何魔法。
您可以在此处阅读有关Rhino中的子类/扩展的相关文章:example using rhino's JavaAdapter
您也可以考虑将您的Scriptable对象(在js脚本执行期间创建)包装到Java对象中,如 BSON.from 和 BSON.to 来自MongoDB的以下Rhino适配器:http://code.google.com/p/mongodb-rhino/