javascript-Java Nashorn存储功能

我有一个自定义的功能界面

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

我目前有一种存储它们以备后用的方法

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);
    }

    ...
}

我正在尝试通过JS API(使用Nashorn引擎)公开这些功能.基本上,我希望用户能够编写像function(thing,flags){…}这样的javascript函数,并将其作为ThingFunction存储在函数映射中.

我知道我可以将引擎写成Invocable并使用Invocable :: getInteface(Class)从javascript代码创建ThingFunction

...
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");
...

但是,这种方法意味着我在引擎中只能有一个apply方法.有没有办法我可以执行许多功能并将它们存储在如上所示的地图中?

解决方法:

Nashorn允许将脚本函数作为参数传递给需要单抽象方法(SAM)接口类型对象的任何Java方法.由于ThingFunction是SAM接口,因此您可以执行以下操作:

文件: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");
    }
}

文件: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);
});

要编译和运行,可以使用以下命令:

javac *.java
java Main main.js

另一种方法(与Nashorn无关,并且也可以与较早的Rhino jsr-223引擎一起使用)是使用Invocable.getInterface(Object,Class)[http://docs.oracle.com/javase/7/docs/api/javax/script/Invocable.html#getInterface%28java.lang.Object,%20java.lang.Class%29]

在脚本中,您将定义多个对象-每个对象都有一个名为“ apply”的脚本函数属性.您可以在每个此类脚本对象的顶部创建一个ThingFunction实例.经评估的脚本看起来像

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

从Java代码中,您将执行以下操作:

 Object obj1 = e.get("obj1");
 Object obj2 = e.get("obj2");
 ThingFunction tf1 = invocable.getInterface(obj1, ThingFunction.class);
 ThingFunction tf2 = invocable.getInterface(obj2, ThingFunction.class);
上一篇:javascript-如何直接初始化Nashorn上下文和引擎?


下一篇:无法使用NashornscriptEngine在Java 8上执行es6