Java Nashorn 商店功能
Java Nashorn store Function
我有自定义功能界面
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);
我有自定义功能界面
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 代码
...
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);