[英]JavaScript function invocation in Java Code
I pass a function from JavaScript file into Java code. 我将功能从JavaScript文件传递到Java代码。 JavaScript function looks like: JavaScript函数如下所示:
entity.handler = function(arg1, arg2) {
//do something
};
In Java code the class implements the Scriptable
interface. 在Java代码中,该类实现Scriptable
接口。 And when I invoke the JavaScript, actually the following method is invoked in Java: 当我调用JavaScript时,实际上是在Java中调用了以下方法:
Scriptable.put(java.lang.String s, org.mozilla.javascript.Scriptable scriptable, java.lang.Object o)
where for my case: 我的情况在哪里:
s = 'handler'; s ='处理程序';
scriptable - object whose type is com.beanexplorer.enterprise.operations.js.ScriptableEntity
可脚本化-类型为com.beanexplorer.enterprise.operations.js.ScriptableEntity
对象
o - actually is a function, its type is org.mozilla.javascript.gen.c15
, ( o instanceof Scriptable )
returns true
in debugger. o-实际上是一个函数,其类型为org.mozilla.javascript.gen.c15
( o instanceof Scriptable )
在调试器中返回true
。
In the Scriptable.put()
method implementation I want to delegate action to the 'o' object: 在Scriptable.put()
方法实现中,我想将操作委派给'o'对象:
SomeClass.invoke( new SomeListener(){
@override
public void someAction(int arg1, float arg2) {
//here I need to delegate to the 'o' object.
//do something looked like:
o.call(arg1, arg2); // have no idea how to do it, if it's possible.
}
}
How can I do it? 我该怎么做? I cannot find any example needed for my case. 我找不到我的案子需要的任何例子。
Thanks. 谢谢。
EDIT, solution: Actully o - could be cast to Function. 编辑,解决方案:强制o-可以强制转换为Function。 As a result the following solution helped: 结果,以下解决方案有所帮助:
@Override
put(java.lang.String s, org.mozilla.javascript.Scriptable scriptable, java.lang.Object o) {
....
final Function f = ( Function )o;
final SomeInterface obj = new ...;
obj.someJavaMethod( Object someParams, new SomeJavaListener() {
@Override
public void use(Object par1, Object par2) throws Exception {
Context ctx = Context.getCurrentContext();
Scriptable rec = new SomeJavaScriptableWrapperForObject( par1);
f.call( ctx, scriptable, scriptable, new Object[] { rec, par2 } );
}
});
I managed to run your Javascript via the following code: 我设法通过以下代码运行您的Javascript:
public class Main {
public static void main(String[] args) {
new ContextFactory().call(new ContextAction(){
@Override
public Object run(Context ctx) {
Scriptable scope = ctx.initStandardObjects();
try {
Scriptable entity = ctx.newObject(scope);
scope.put("console", scope, Context.javaToJS(System.out, scope));
scope.put("entity", scope, entity);
ctx.evaluateReader(
scope,
new InputStreamReader(Main.class.getResourceAsStream("/handler.js")),
"handler.js", 1, null);
Function handler = (Function) entity.get("handler", entity);
Object result = handler.call(ctx, scope, scope, new Object[] {"Foo", 1234});
System.out.println("Handler returned " + result);
} catch (Exception e) {
e.printStackTrace(System.err);
}
return null;
}
});
}
}
The following script must be available in a handler.js
on your CLASSPATH: 以下脚本必须在CLASSPATH上的handler.js
可用:
entity.handler = function(arg1, arg2) {
console.println("Hello world from the JS handler");
return 42;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.