本文整理汇总了Java中javax.script.Bindings.put方法的典型用法代码示例。如果您正苦于以下问题:Java Bindings.put方法的具体用法?Java Bindings.put怎么用?Java Bindings.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.Bindings
的用法示例。
在下文中一共展示了Bindings.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAttributesFromInlineGroovyScript
import javax.script.Bindings; //导入方法依赖的package包/类
private static Map<String, Object> getAttributesFromInlineGroovyScript(final Map<String, Object> attributes,
final Matcher matcherInline) throws ScriptException {
final String script = matcherInline.group(1).trim();
final ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
if (engine == null) {
LOGGER.warn("Script engine is not available for Groovy");
return new HashMap<>();
}
final Object[] args = {attributes, LOGGER};
LOGGER.debug("Executing script, with parameters [{}]", args);
final Bindings binding = new SimpleBindings();
binding.put("attributes", attributes);
binding.put("logger", LOGGER);
return (Map<String, Object>) engine.eval(script, binding);
}
示例2: appendTo
import javax.script.Bindings; //导入方法依赖的package包/类
public static void appendTo(Bindings bindings) {
// standard java collections
bindings.put("newArrayList", ARRAY_LIST);
bindings.put("newLinkedList", LINKED_LIST);
bindings.put("newHashSet", HASH_SET);
bindings.put("newHashMap", HASH_MAP);
bindings.put("newCopyOnWriteArrayList", COPY_ON_WRITE_ARRAY_LIST);
bindings.put("newConcurrentHashSet", CONCURRENT_HASH_SET);
bindings.put("newConcurrentHashMap", CONCURRENT_HASH_MAP);
bindings.put("listOf", LIST_OF);
bindings.put("setOf", SET_OF);
// guava immutables
bindings.put("immutableListOf", IMMUTABLE_LIST_OF);
bindings.put("immutableSetOf", IMMUTABLE_SET_OF);
// misc
bindings.put("parseUuid", PARSE_UUID);
}
示例3: megamorphicPropertyReadTest
import javax.script.Bindings; //导入方法依赖的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);
}
}
示例4: main
import javax.script.Bindings; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("\nTest3\n");
final Reader reader = new FileReader(
new File(System.getProperty("test.src", "."), "Test3.js"));
ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine = Helper.getJsEngine(m);
if (engine == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
Bindings en = new SimpleBindings();
engine.setBindings(en, ScriptContext.ENGINE_SCOPE);
en.put("key", "engine value");
Bindings gn = new SimpleBindings();
engine.setBindings(gn, ScriptContext.GLOBAL_SCOPE);
gn.put("key", "global value");
engine.eval(reader);
}
示例5: write
import javax.script.Bindings; //导入方法依赖的package包/类
static <E extends IJsonIO> void write( String tag, E obj, Bindings bindings )
{
if( obj == null )
{
if( tag != null && !tag.isEmpty() )
{
bindings.put( tag, obj );
}
}
else
{
if( tag != null && !tag.isEmpty() )
{
Bindings objBindings = ManifoldHost.createBindings();
obj.save( objBindings );
bindings.put( tag, objBindings );
}
else
{
obj.save( bindings );
}
}
}
示例6: invokeJS
import javax.script.Bindings; //导入方法依赖的package包/类
private List<String> invokeJS(String js, int y, int ch) {
ArrayList<String> pagsList = new ArrayList<String>();
String str = js.substring(0, js.indexOf("var pt="));
str = str.replace("ge('TheImg').src", "var src");
String unuseScript = StringUtility.substring(str, "\'.jpg\';", "break;");
str = str.replace(unuseScript, "");
String varSrc = null;
if(str.indexOf("ci = i;") != -1){
varSrc = StringUtility.substring(str, "ci = i;", "break;");
}else if(str.indexOf("ci=i;") != -1){
varSrc = StringUtility.substring(str, "ci=i;", "break;");
}
String getPageJS = String.format(buildGetPagesJS(), varSrc);
str = str.replace(varSrc, "");
str = str.replace("break;", getPageJS);
String script = "function sp2(ch, y){" + str + "} " + buildNviewJS();
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Bindings bind = engine.createBindings();
bind.put("pagsList", pagsList);
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("sp2", ch, y);
} catch (Exception e) {
e.printStackTrace();
}
return pagsList;
}
示例7: run
import javax.script.Bindings; //导入方法依赖的package包/类
@Override
public void run() {
ScriptEngine engine = loader.getScriptEngine();
try {
// create bindings
Bindings bindings = engine.createBindings();
// provide an export for this scripts attributes
bindings.put("loader", loader);
bindings.put("registry", terminableRegistry);
bindings.put("logger", logger);
// the path of the script file (current working directory)
bindings.put("cwd", path.normalize().toString().replace("\\", "/"));
// the root scripts directory
bindings.put("rsd", loader.getDirectory().normalize().toString().replace("\\", "/") + "/");
// function to depend on another script
bindings.put("depend", (Consumer<String>) this::depend);
// append the global helper bindings to this instance
systemBindings.appendTo(bindings);
// create a new script context, and attach our bindings
ScriptContext context = new SimpleScriptContext();
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
// evaluate the header
engine.eval(systemBindings.getPlugin().getScriptHeader(), context);
// resolve the load path, relative to the loader directory.
Path loadPath = loader.getDirectory().normalize().resolve(path);
engine.eval("__load(\"" + loadPath.toString().replace("\\", "/") + "\");", context);
} catch (Throwable t) {
t.printStackTrace();
}
}
示例8: perform
import javax.script.Bindings; //导入方法依赖的package包/类
private Node perform(HashMap<String, java.lang.Object> a6y) throws Exception {
Bindings bindings = engine.createBindings();
bindings.put("a6y", a6y);
try {
engine.eval(script, bindings);
return jsToNode(a6y.get("response"));
} catch (ScriptException e) {
throw new Exception(e);
}
}
示例9: invokeJS
import javax.script.Bindings; //导入方法依赖的package包/类
public static List<String> invokeJS(String js, int y, int ch) {
ArrayList<String> pagsList = new ArrayList<String>();
System.out.println(StringUtility.substring(js, ";for(var i=0;i<", ";i++"));
String str = js.substring(0, js.indexOf("var pt="));
str = str.replace("ge('TheImg').src", "var src");
String unuseScript = StringUtility.substring(str, "\'.jpg\';", "break;");
System.out.println(str);
System.out.println("unuseScript==" + unuseScript);
str = str.replace(unuseScript, "");
System.out.println("str-->" + str);
String varSrc = StringUtility.substring(str, "ci = i; ", "break;");
System.out.println("varSrc-->" + varSrc);
String getPageJS = String.format(buildGetPagesJS(), varSrc);
str = str.replace(varSrc, "");
str = str.replace("break;", getPageJS);
String script = "function sp2(ch, y){" + str + "} " + buildNviewJS();
System.out.println("script-->" + script);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Bindings bind = engine.createBindings();
bind.put("pagsList", pagsList);
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("sp2", ch, y);
} catch (Exception e) {
e.printStackTrace();
}
return pagsList;
}
示例10: program
import javax.script.Bindings; //导入方法依赖的package包/类
public void program() throws ScriptException {
final ScriptContext sc = new SimpleScriptContext();
final Bindings global = new SimpleBindings();
sc.setBindings(global, ScriptContext.GLOBAL_SCOPE);
sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
global.put("text", "programText");
String value = engine.eval("text", sc).toString();
Assert.assertEquals(value, "programText");
engine.put("program", this);
engine.eval("program.method()");
// eval again from here!
value = engine.eval("text", sc).toString();
Assert.assertEquals(value, "programText");
}
示例11: engineOverwriteTest
import javax.script.Bindings; //导入方法依赖的package包/类
@Test
public static void engineOverwriteTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = new SimpleBindings();
b.put("engine", "hello");
b.put("foo", 32);
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
e.setContext(newCtxt);
assertEquals(e.eval("engine"), "hello");
assertEquals(((Number)e.eval("foo")).intValue(), 32);
}
示例12: EngineMap
import javax.script.Bindings; //导入方法依赖的package包/类
public EngineMap()
{
this.map = Collections.synchronizedMap(new EnumMap<>(Engine.class));
final ScriptEngine js = Engine.JAVASCRIPT.newScriptEngine();
this.map.put(Engine.JAVASCRIPT, js);
try
{
js.eval("engines = {}");
}
catch (final ScriptException e)
{
throw new RuntimeException(e); // should never happen
}
final Bindings engines = (Bindings) js.get("engines");
engines.put("js", js);
this.context = js.getContext();
engines.put("context", this.context);
for (final Engine engine : Engine.values())
this.map.computeIfAbsent(engine, e ->
{
final ScriptEngine scriptEngine = e.newScriptEngine(this.context);
((Bindings) scriptEngine.get("engines")).put(e.getName(), scriptEngine);
return scriptEngine;
});
}
示例13: route
import javax.script.Bindings; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
try {
List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings();
bindings.put("invokers", invokersCopy);
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
CompiledScript function = compilable.compile(rule);
Object obj = function.eval(bindings);
if (obj instanceof Invoker[]) {
invokersCopy = Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
invokersCopy = new ArrayList<Invoker<T>>();
for (Object inv : (Object[]) obj) {
invokersCopy.add((Invoker<T>) inv);
}
} else {
invokersCopy = (List<Invoker<T>>) obj;
}
return invokersCopy;
} catch (ScriptException e) {
//fail then ignore rule .invokers.
logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
return invokers;
}
}
示例14: testMegamorphicGetInGlobal
import javax.script.Bindings; //导入方法依赖的package包/类
@Test
public static void testMegamorphicGetInGlobal() throws Exception {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine = m.getEngineByName("nashorn");
final String script = "foo";
// "foo" is megamorphic because of different global scopes.
// Make sure ScriptContext variable search works even after
// it becomes megamorphic.
for (int index = 0; index < 25; index++) {
final Bindings bindings = new SimpleBindings();
bindings.put("foo", index);
final Number value = (Number)engine.eval(script, bindings);
assertEquals(index, value.intValue());
}
}
示例15: route
import javax.script.Bindings; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
try {
List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings();
bindings.put("invokers", invokersCopy);
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
CompiledScript function = compilable.compile(rule);
Object obj = function.eval(bindings);
if (obj instanceof Invoker[]) {
invokersCopy = Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
invokersCopy = new ArrayList<Invoker<T>>();
for (Object inv : (Object[]) obj) {
invokersCopy.add((Invoker<T>)inv);
}
} else {
invokersCopy = (List<Invoker<T>>) obj;
}
return invokersCopy;
} catch (ScriptException e) {
//fail then ignore rule .invokers.
logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
return invokers;
}
}