I pass a function from JavaScript file into Java code. JavaScript function looks like:
entity.handler = function(arg1, arg2) {
//do something
};
In Java code the class implements the Scriptable
interface. And when I invoke the JavaScript, actually the following method is invoked in Java:
Scriptable.put(java.lang.String s, org.mozilla.javascript.Scriptable scriptable, java.lang.Object o)
where for my case:
s = 'handler';
scriptable - object whose type is 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.
In the Scriptable.put()
method implementation I want to delegate action to the 'o' object:
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. 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 } );
}
});