gpt4 book ai didi

java - Mozilla Rhino 1_7R4 扩展抽象类 ( JS )

转载 作者:行者123 更新时间:2023-12-01 04:48:02 25 4
gpt4 key购买 nike

我正在使用 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)

Java:

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() { }
}
);

现在已经很晚了,我可能会错过一些明显的东西,但是有人可以提供一些提示吗?谢谢!

附注我也非常感谢任何关于使用 Mozilla Rhino 的优秀文档、示例和教程的链接。从我从搜索中收集到的信息来看,似乎没有太多连贯的信息。

最佳答案

不需要使用JavaAdaper。您可以显式声明一个要实例化的类,但该类必须能够通过反射实例化,因为 Rhino 不会产生任何像代码编织之类的魔法。

您可以在这里阅读有关Rhino子类化/扩展的相关文章:example using rhino's JavaAdapter

您还可以考虑以 BSON.fromBSON.to 等方式将 Scriptable 对象(在 js 脚本执行期间创建)包装在 Java 对象之间。 > 在以下来自 MongoDB 的 Rhino 适配器中:http://code.google.com/p/mongodb-rhino/

关于java - Mozilla Rhino 1_7R4 扩展抽象类 ( JS ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15491642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com