本文整理汇总了Java中javax.script.Invocable.invokeMethod方法的典型用法代码示例。如果您正苦于以下问题:Java Invocable.invokeMethod方法的具体用法?Java Invocable.invokeMethod怎么用?Java Invocable.invokeMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.Invocable
的用法示例。
在下文中一共展示了Invocable.invokeMethod方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.script.Invocable; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("nashorn");
// JavaScript code in a String. This code defines a script object 'obj'
// with one method called 'hello'.
final String script = "var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);
// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
final Invocable inv = (Invocable) engine;
// get script object on which we want to call the method
final Object obj = engine.get("obj");
// invoke the method named "hello" on the script object "obj"
inv.invokeMethod(obj, "hello", "Script Method !!" );
}
示例2: main
import javax.script.Invocable; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("\nTest8\n");
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = Helper.getJsEngine(m);
if (e == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
e.eval(new FileReader(
new File(System.getProperty("test.src", "."), "Test8.js")));
Invocable inv = (Invocable)e;
inv.invokeFunction("main", "Mustang");
// use method of a specific script object
Object scriptObj = e.get("scriptObj");
inv.invokeMethod(scriptObj, "main", "Mustang");
}