本文整理汇总了Java中javax.script.ScriptEngine.getBindings方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.getBindings方法的具体用法?Java ScriptEngine.getBindings怎么用?Java ScriptEngine.getBindings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.getBindings方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeJsExpress
import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
* 执行js表达式并返回执行后的结果
* @param express
* 表达式
* @param value
* 原值
* @return
* 返回新值或返回原值(执行失败时)
*/
public static Object executeJsExpress(String express, Object value){
Object newValue = value;
if(!StringUtils.isEmpty(express)){
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.put("value", value);
engine.put("newValue", "");
engine.getBindings(ScriptContext.ENGINE_SCOPE);
engine.eval(express);
newValue = engine.get("newValue");
} catch (ScriptException e) {
log.error("执行js表达式错误",e);
}
}
return newValue;
}
示例2: megamorphicPropertyReadTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void megamorphicPropertyReadTest() throws ScriptException {
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
final ScriptEngine engine = factory.getScriptEngine();
final Bindings scope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
boolean ret;
// Why 16 is the upper limit of this loop? The default nashorn dynalink megamorphic threshold is 16.
// See jdk.nashorn.internal.runtime.linker.Bootstrap.NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD
// We do, 'eval' of the same in this loop twice. So, 16*2 = 32 times that callsite in the script
// is exercised - much beyond the default megamorphic threshold.
for (int i = 0; i < 16; i++) {
scope.remove(VAR_NAME);
ret = lookupVar(engine, VAR_NAME);
assertFalse(ret, "Expected false in iteration " + i);
scope.put(VAR_NAME, "foo");
ret = lookupVar(engine, VAR_NAME);
assertTrue(ret, "Expected true in iteration " + i);
}
}
示例3: syncBindings
import javax.script.ScriptEngine; //导入方法依赖的package包/类
private void syncBindings(ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {
Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
this.scriptEngines.forEach((String name, ScriptEngine engine) -> {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
currentBindings.keySet().forEach((String key) -> {
bindings.put(key, scriptLanguage.decode(currentBindings.get(key)));
});
});
}
示例4: initBindings
import javax.script.ScriptEngine; //导入方法依赖的package包/类
private void initBindings(Bindings bindings, ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) {
Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.keySet().forEach((String key) -> {
currentBindings.put(key, scriptLanguage.decode(bindings.get(key)));
});
}