Java Nashorn store feature

I have a custom functional interface

public interface ThingFunction {
    Object apply(Thing thing, String... flags);
}

      

I currently have a way to store them for future use

public class Thing {
    private static Map<String, ThingFunction> functions = new HashMap<>();

    ...

    public static void addFunction(String key, ThingFunction function) {
        functions.put(key, function);
    }

    ...

    public Object executeFunction(String key, String... flags) {
        return functions.get(key).accept(this, flags);
    }

    ...
}

      

I am trying to expose these functionality via JS API (using Nashorn engine). Basically, I want the user to be able to write a javascript function like function(thing, flags) {...}

and store it as a ThingFunction in the function map.

I know that I can use engine for Invocable

and use Invocable::getInteface(Class)

to create ThingFunction from javascript code

...
engine.eval("function apply(actor, flags) {return 'There are ' + flags.length + ' arguments';}");
Invocable invocable = (Invocable) enging;
ThingFunction function = invocable.getInterface(ThingFunction.class);
function.apply(thing, "this", "is", "a", "test");
...

      

However, this approach means that I can only have one method apply

in the engine. Is there a way to make a lot of functions and store them in the map like above?

+3


source to share


1 answer


Nashorn allows you to pass a script function as an argument to any Java method that requires an interface type object with a single abstract method (SAM). Since your ThingFunction is a SAM interface, you can do something like this:

File: Main.java

import javax.script.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine e = m.getEngineByName("nashorn");
        e.eval(new FileReader(args[0])); 
        Thing th = new Thing();
        // script evaluated is expected to 'register' func, foo
        // call those functions from java
        th.executeFunction("func", "arg1", "arg2");
        th.executeFunction("foo", "bar", "j");
    }
}

      

File: main.js

var Thing = Java.type("Thing");

// register two functions with Thing.
// Nashorn auto-converts a script function to an object
// implementing any SAM interface (ThingFunction in this case)

Thing.addFunction("func", function(thing, args) {
    print("in func");
    for each (var i in args) print(i);
});

Thing.addFunction("foo", function(thing, args) {
    print("in foo");
    for each (var i in args) print(i);
});

      

To compile and run, you can use the following commands:



javac *.java
java Main main.js

      

Another approach (independent from Nashorn and working with the old Rhino jsr-223 engine) is to use Invocable.getInterface (Object, Class) [ http://docs.oracle.com/javase/7/docs/api/javax/script/Invocable .html # getInterface% 28java.lang.Object,% 20java.lang.Class% 29 ]

In a script, you must define several objects - each of which has a property on the script function called "apply". And you can create one ThingFunction instance on top of each such script object. The evaluated script will look like

var obj1 = { apply: function(thing, args)  { ... } };
var obj2 = { apply: function(thing, args)  { ....} };

      

From Java code, you would do something like:

 Object obj1 = e.get("obj1");
 Object obj2 = e.get("obj2");
 ThingFunction tf1 = invocable.getInterface(obj1, ThingFunction.class);
 ThingFunction tf2 = invocable.getInterface(obj2, ThingFunction.class);

      

+4


source







All Articles