本文整理汇总了Java中javax.script.CompiledScript.eval方法的典型用法代码示例。如果您正苦于以下问题:Java CompiledScript.eval方法的具体用法?Java CompiledScript.eval怎么用?Java CompiledScript.eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.CompiledScript
的用法示例。
在下文中一共展示了CompiledScript.eval方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: invokeFunctionWithCustomScriptContextTest
import javax.script.CompiledScript; //导入方法依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// create an engine and a ScriptContext, but don't set it as default
final ScriptContext scriptContext = new SimpleScriptContext();
// Set some value in the context
scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);
// Evaluate script with custom context and get back a function
final String script = "function (c) { return myString.indexOf(c); }";
final CompiledScript compiledScript = ((Compilable)engine).compile(script);
final Object func = compiledScript.eval(scriptContext);
// Invoked function should be able to see context it was evaluated with
final Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
assertTrue(((Number)result).intValue() == 1);
}
示例2: init
import javax.script.CompiledScript; //导入方法依赖的package包/类
private void init(Snapshot snapshot) throws RuntimeException {
this.snapshot = snapshot;
try {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByName("JavaScript"); // NOI18N
InputStream strm = getInitStream();
CompiledScript cs = ((Compilable)engine).compile(new InputStreamReader(strm));
cs.eval();
Object heap = ((Invocable)engine).invokeFunction("wrapHeapSnapshot", snapshot); // NOI18N
engine.put("heap", heap); // NOI18N
engine.put("cancelled", cancelled); // NOI18N
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Error initializing snapshot", ex); // NOI18N
throw new RuntimeException(ex);
}
}
示例3: 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;
}
示例4: test
import javax.script.CompiledScript; //导入方法依赖的package包/类
@Test
public final void test() throws Exception {
String csvMapperScript = "mapFunction = function(inputHeaders, inputField, inputValue, outputField, line) return inputValue end";
String simpleScript = "return inputValue";
CompiledScript compiledScript = (CompiledScript) ((Compilable) scriptEngine).compile(simpleScript);
Bindings bindings = scriptEngine.createBindings();
// inputHeaders, inputField, inputValue, outputField, line
bindings.put("inputHeaders", "");
bindings.put("inputField", "");
bindings.put("inputValue", "testreturnvalue");
bindings.put("outputField", "");
bindings.put("line", "");
String result = (String) compiledScript.eval(bindings);
System.out.println(result);
assertEquals("testreturnvalue", result);
}
示例5: runMap
import javax.script.CompiledScript; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public List<Object> runMap(CallingContext context, RaptureScript script, RaptureDataContext data, Map<String, Object> parameters) {
try {
ScriptEngine engine = engineRef.get();
CompiledScript cScript = getMapScript(engine, script);
addStandardContext(context, engine);
engine.put(PARAMS, parameters);
engine.put(DATA, JacksonUtil.getHashFromObject(data));
Kernel.getKernel().getStat().registerRunScript();
return (List<Object>) cScript.eval();
} catch (ScriptException e) {
Kernel.writeAuditEntry(EXCEPTION, 2, e.getMessage());
throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error running script " + script.getName(), e);
}
}
示例6: invokeFunctionWithCustomScriptContextTest
import javax.script.CompiledScript; //导入方法依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// create an engine and a ScriptContext, but don't set it as default
ScriptContext scriptContext = new SimpleScriptContext();
// Set some value in the context
scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);
// Evaluate script with custom context and get back a function
final String script = "function (c) { return myString.indexOf(c); }";
CompiledScript compiledScript = ((Compilable)engine).compile(script);
Object func = compiledScript.eval(scriptContext);
// Invoked function should be able to see context it was evaluated with
Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
assertTrue(((Number)result).intValue() == 1);
}
示例7: evaluateActivationExpression
import javax.script.CompiledScript; //导入方法依赖的package包/类
public static Boolean evaluateActivationExpression(Bindings bindings, Expression activationExpression) {
Boolean expressionResult;
if(activationExpression!=null) {
CompiledScript script = activationExpression.compiledScript;
if(script!=null) {
try {
Object evaluationResult = script.eval(bindings);
if(evaluationResult instanceof Boolean) {
expressionResult = (Boolean) evaluationResult;
} else {
expressionResult = false;
}
} catch (ScriptException e) {
expressionResult = false;
}
} else {
expressionResult = true;
}
} else {
expressionResult = true;
}
return expressionResult;
}
示例8: evaluate
import javax.script.CompiledScript; //导入方法依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JavaScript expression: {1}", expression);
try {
final Bindings scope = engine.createBindings();
for (final Entry<String, ?> entry : values.entrySet()) {
scope.put(entry.getKey(), entry.getValue());
}
if (compilable != null) {
CompiledScript compiled = compiledCache.get(expression);
if (compiled == null) {
compiled = compilable.compile(expression);
compiledCache.put(expression, compiled);
}
return compiled.eval(scope);
}
return engine.eval(expression, scope);
} catch (final ScriptException ex) {
throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex);
}
}
示例9: evaluateScript
import javax.script.CompiledScript; //导入方法依赖的package包/类
/**
* Evaluates the compiled script of a entry.
* @param entry The configuration entry to evaluate
* @param scopeValues All known values for script scope
* @return The computed value
* @throws ScriptException
*/
private Object evaluateScript(Entry<String, Map<String, Object>> entry, Map<String, Object> scopeValues) throws ScriptException {
Object value = null;
// executes compiled script
if(entry.getValue().containsKey("cscript")) {
CompiledScript cscript = (CompiledScript) entry.getValue().get("cscript");
// Add global variables thisValue and keyName to JavaScript context
Bindings bindings = cscript.getEngine().createBindings();
bindings.putAll(scopeValues);
value = cscript.eval(bindings);
}
// try to convert the returned value to BigDecimal
value = ObjectUtils.defaultIfNull(
NumberUtils.toBigDecimal(value), value);
// round to two digits, maybe not optimal for any result
if(value instanceof BigDecimal) {
((BigDecimal)value).setScale(2, BigDecimal.ROUND_HALF_UP);
}
return value;
}
示例10: getElValue
import javax.script.CompiledScript; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> T getElValue(String exp, Object target, Object[] arguments, Object retVal, boolean hasRetVal, Class<T> valueType) throws Exception {
Bindings bindings=new SimpleBindings();
bindings.put(TARGET, target);
bindings.put(ARGS, arguments);
if(hasRetVal) {
bindings.put(RET_VAL, retVal);
}
CompiledScript script=expCache.get(exp);
if(null != script) {
return (T)script.eval(bindings);
}
if(engine instanceof Compilable) {
Compilable compEngine=(Compilable)engine;
script=compEngine.compile(funcs + exp);
expCache.put(exp, script);
return (T)script.eval(bindings);
} else {
return (T)engine.eval(funcs + exp, bindings);
}
}
示例11: intArrayLengthTest
import javax.script.CompiledScript; //导入方法依赖的package包/类
@Test
public void intArrayLengthTest() throws ScriptException {
Map<String, Object> vars = new HashMap<String, Object>();
Integer[] l = new Integer[] { 1,2,3 };
vars.put("l", l);
String script = "l.length";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
ScriptContext context = engine.getContext();
Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.putAll(vars);
Compilable compilable = (Compilable)engine;
CompiledScript compiledScript = compilable.compile(script);
Object o = compiledScript.eval();
assertThat(((Number) o).intValue(), equalTo(3));
}
示例12: objectArrayTest
import javax.script.CompiledScript; //导入方法依赖的package包/类
@Test
public void objectArrayTest() throws ScriptException {
Map<String, Object> vars = new HashMap<String, Object>();
final Map<String, Object> obj2 = new HashMap<String,Object>() {{
put("prop2", "value2");
}};
final Map<String, Object> obj1 = new HashMap<String,Object>() {{
put("prop1", "value1");
put("obj2", obj2);
}};
vars.put("l", new Object[] { "1", "2", "3", obj1 });
String script = "l.length";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.putAll(vars);
Compilable compilable = (Compilable)engine;
CompiledScript compiledScript = compilable.compile(script);
Object o = compiledScript.eval(bindings);
assertThat(((Number) o).intValue(), equalTo(4));
}
示例13: testInvoke
import javax.script.CompiledScript; //导入方法依赖的package包/类
public void testInvoke() throws Exception {
eng = new ScriptEngineManager().getEngineByName("jav8");
Compilable compiler = (Compilable) this.eng;
CompiledScript script = compiler.compile(calcFunction);
int max = 100000;
Bindings binding = this.eng.getBindings(ScriptContext.GLOBAL_SCOPE);
binding.put("num", 3);
Object r = script.eval(binding);
System.out.println(r);
long startM = System.currentTimeMillis();
for ( int i=0; i<max; i++ ) {
script.eval(binding);
}
long endM = System.currentTimeMillis();
System.out.println(" V8 engine loop " + max + ":" + (endM-startM));
}
示例14: 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");
}
示例15: evaluateScript
import javax.script.CompiledScript; //导入方法依赖的package包/类
/**
* Evaluates the compiled script of a entry.
*
* @param entry The configuration entry to evaluate
* @param scopeValues All known values for script scope
* @return The computed value
* @throws ScriptException
*/
private Object evaluateScript(Entry<String, TelegramValue> entry, Map<String, Object> scopeValues)
throws ScriptException {
Object value = null;
// executes compiled script
if (entry.getValue().getCsript() != null) {
CompiledScript cscript = entry.getValue().getCsript();
// Add global variables thisValue and keyName to JavaScript context
Bindings bindings = cscript.getEngine().createBindings();
bindings.putAll(scopeValues);
value = cscript.eval(bindings);
}
// try to convert the returned value to BigDecimal
value = ObjectUtils.defaultIfNull(NumberUtils.toBigDecimal(value), value);
// round to two digits, maybe not optimal for any result
if (value instanceof BigDecimal) {
((BigDecimal) value).setScale(2, BigDecimal.ROUND_HALF_UP);
}
return value;
}