本文整理汇总了Java中javax.script.CompiledScript.getEngine方法的典型用法代码示例。如果您正苦于以下问题:Java CompiledScript.getEngine方法的具体用法?Java CompiledScript.getEngine怎么用?Java CompiledScript.getEngine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.CompiledScript
的用法示例。
在下文中一共展示了CompiledScript.getEngine方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeScript
import javax.script.CompiledScript; //导入方法依赖的package包/类
/**
* Executes a compiled script and returns the result.
*
* @param script Compiled script.
* @param functionName Optional function or method to invoke.
* @param scriptContext Script execution context.
* @return The script result.
* @throws IllegalArgumentException
*/
public static Object executeScript(CompiledScript script, String functionName, ScriptContext scriptContext, Object[] args) throws ScriptException, NoSuchMethodException {
Assert.notNull("script", script, "scriptContext", scriptContext);
Object result = script.eval(scriptContext);
if (UtilValidate.isNotEmpty(functionName)) {
if (Debug.verboseOn()) {
Debug.logVerbose("Invoking function/method " + functionName, module);
}
ScriptEngine engine = script.getEngine();
try {
Invocable invocableEngine = (Invocable) engine;
result = invocableEngine.invokeFunction(functionName, args == null ? EMPTY_ARGS : args);
} catch (ClassCastException e) {
throw new ScriptException("Script engine " + engine.getClass().getName() + " does not support function/method invocations");
}
}
return result;
}
示例2: main
import javax.script.CompiledScript; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval(new FileReader("src/main/javascript/Example.js"));
Compilable compilingEngine = (Compilable) engine;
CompiledScript script = compilingEngine.compile(new FileReader(
"src/main/javascript/Function.js"));
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("date", new Date());
bindings.put("out", System.out);
for (Map.Entry me : bindings.entrySet()) {
System.out.printf("%s: %s\n", me.getKey(),
String.valueOf(me.getValue()));
}
script.eval(bindings);
Invocable invocable = (Invocable) script.getEngine();
invocable.invokeFunction("sayhello", "Jose");
}