Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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?

share|improve this question
    
Why not just evaluate list = function(){return new java.util.ArrayList()}? –  Saposhiente Feb 9 at 7:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.