本文整理匯總了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");
}