public class JSBindings extends SimpleBindings{
public void bind(String key, Object value){
this.put(key, value);
}
}
I pass the instance of JSBindings to ScriptEngine.eval(script, bindings)
in my code. However, I am only able to bind (put) java objects to SimpleBindings. How can I bind global javascript functions from java code?
i.e
bind("UTILS", new SomeJavaObject()) //works fine..
But I want to bind javascript functions. Perhaps something like this :
bind("list", "function(){return new java.util.ArrayList()}")
//doesn't work.. list is just a string
so that when I say list()
in my javascript, it must invoke this bind function and create an instance of ArrayList. Is it possible to bind javascript functions this way from an instance of SimpleBindings?
list = function(){return new java.util.ArrayList()}
? – Saposhiente Feb 9 at 7:28