本文整理汇总了Java中javax.script.ScriptEngine.put方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.put方法的具体用法?Java ScriptEngine.put怎么用?Java ScriptEngine.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testJS
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void testJS() {
// https://stackoverflow.com/questions/22492641/java8-js-nashorn-convert-array-to-java-array
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
engine.put("line", "刘长炯 微信号weblogic (10.3.2) [46a5432f8fdea99a6186a927e8da5db7a51854ac]");
// engine.put("regex", )
String regex = "/(.*?) \\((.*?)\\) \\[(.*?)\\]/";
String[] value = (String[])engine.eval("Java.to(line.match(" + regex + "),\"java.lang.String[]\" );");
System.out.println(value.length);
System.out.println(value[1]);
String[] result = {"刘长炯 微信号weblogic (10.3.2) [46a5432f8fdea99a6186a927e8da5db7a51854ac]",
"刘长炯 微信号weblogic", "10.3.2", "46a5432f8fdea99a6186a927e8da5db7a51854ac"};
Assert.assertArrayEquals("result shold match", result, value);
// Collection<Object> val = value.values();
// if(value.isArray()) {
// System.out.println(value.getMember("1"));
// }
} catch (ScriptException e) {
e.printStackTrace();
}
}
示例2: iteratingJSObjectTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// iteration tests
public void iteratingJSObjectTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
final MapWrapperObject obj = new MapWrapperObject();
obj.setMember("foo", "hello");
obj.setMember("bar", "world");
e.put("obj", obj);
// check for..in
Object val = e.eval("var str = ''; for (i in obj) str += i; str");
assertEquals(val.toString(), "foobar");
// check for..each..in
val = e.eval("var str = ''; for each (i in obj) str += i; str");
assertEquals(val.toString(), "helloworld");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例3: functionalInterfaceObjectTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void functionalInterfaceObjectTest() throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine e = manager.getEngineByName("nashorn");
final AtomicBoolean invoked = new AtomicBoolean(false);
e.put("c", new Consumer<Object>() {
@Override
public void accept(final Object t) {
assertTrue(t instanceof ScriptObjectMirror);
assertEquals(((ScriptObjectMirror)t).get("a"), "xyz");
invoked.set(true);
}
});
e.eval("var x = 'xy'; x += 'z';c({a:x})");
assertTrue(invoked.get());
}
示例4: fakeProxySubclassAccessCheckTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void fakeProxySubclassAccessCheckTest() {
if (System.getSecurityManager() == null) {
// pass vacuously
return;
}
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.put("name", ScriptEngineSecurityTest.class.getName());
e.put("cl", ScriptEngineSecurityTest.class.getClassLoader());
e.put("intfs", new Class[] { Runnable.class });
final String getClass = "Java.type(name + '$FakeProxy').getProxyClass(cl, intfs);";
// Should not be able to call static methods of Proxy via fake subclass
try {
e.eval(getClass);
fail("should have thrown SecurityException");
} catch (final Exception exp) {
if (! (exp instanceof SecurityException)) {
fail("SecurityException expected, got " + exp);
}
}
}
示例5: createScriptEngine
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Override
protected ScriptEngine createScriptEngine() {
String scripEngineName = SCRIPT_ENGINE_NAME;
// ScriptEngine result = new ScriptEngineManager().getEngineByName(scripEngineName);
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine result = factory.getScriptEngine("-scripting");
Validate.isInstanceOf(Compilable.class, result, "ScriptingEngine %s doesn't implement Compilable", scripEngineName);
Validate.isInstanceOf(Invocable.class, result, "ScriptingEngine %s doesn't implement Invocable", scripEngineName);
PROCESSOR_CLASSES.forEach((interfaceClass, scriptClass) -> addImport(result, scriptClass, interfaceClass.getSimpleName()));
addImport(result, NashornPlugin.class, Plugin.class.getSimpleName());
getStandardImportClasses().forEach(cls -> addImport(result, cls));
result.put(KnowledgeBaseConstants.VAR_ENGINE_OPERATIONS, getEngineOperations());
eval(result, "load(\"classpath:" + INITIAL_SCRIPT + "\");");
return result;
}
示例6: currentGlobalMissingTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void currentGlobalMissingTest() throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine e = manager.getEngineByName("nashorn");
final Context ctx = new Context();
e.put("ctx", ctx);
e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }");
e.eval("ctx.set(obj)");
final Invocable inv = (Invocable)e;
assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello"));
// try object literal
e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })");
assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO"));
// try array literal
e.eval("var arr = [ 'hello', 'world' ]");
e.eval("ctx.set(arr)");
assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", ""));
}
示例7: windowAlertTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void windowAlertTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Window window = new Window();
try {
e.put("window", window);
e.eval("print(window.alert)");
e.eval("window.alert('calling window.alert...')");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例8: act
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public final void act(final MapleClient c, final MapleReactor reactor) {
try {
final Invocable iv = getInvocable("reactor/" + reactor.getReactorId() + ".js", c);
if (iv == null) {
return;
}
final ScriptEngine scriptengine = (ScriptEngine) iv;
ReactorActionManager rm = new ReactorActionManager(c, reactor);
scriptengine.put("rm", rm);
iv.invokeFunction("act");
} catch (ScriptException | NoSuchMethodException e) {
System.err.println("Error executing reactor script. ReactorID: " + reactor.getReactorId() + ", ReactorName: " + reactor.getName() + ":" + e);
}
}
示例9: consStringTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// ConsString attribute access on a JSObject
public void consStringTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
final MapWrapperObject obj = new MapWrapperObject();
e.put("obj", obj);
e.put("f", "f");
e.eval("obj[f + 'oo'] = 'bar';");
assertEquals(obj.getMap().get("foo"), "bar");
assertEquals(e.eval("obj[f + 'oo']"), "bar");
assertEquals(e.eval("obj['foo']"), "bar");
assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE);
assertEquals(e.eval("'foo' in obj"), Boolean.TRUE);
e.eval("delete obj[f + 'oo']");
assertFalse(obj.getMap().containsKey("foo"));
assertEquals(e.eval("obj[f + 'oo']"), null);
assertEquals(e.eval("obj['foo']"), null);
assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE);
assertEquals(e.eval("'foo' in obj"), Boolean.FALSE);
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例10: engineOverwriteInScriptTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public static void engineOverwriteInScriptTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.put("foo", 32);
assertEquals(((Number)e.eval("foo")).intValue(), 32);
assertEquals(e.eval("engine = 'bar'"), "bar");
assertEquals(((Number)e.eval("foo")).intValue(), 32);
}
示例11: rewriteExceptionNotSerializable
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void rewriteExceptionNotSerializable() throws ScriptException {
// NOTE: we must create a RewriteException in a context of a Nashorn engine, as it uses Global.newIntance()
// internally.
final ScriptEngine e = new NashornScriptEngineFactory().getScriptEngine();
e.put("f", new Runnable() {
@Override
public void run() {
tryToSerialize(RewriteException.create(null, new Object[0], new String[0]));
}
});
e.eval("f()");
}
示例12: testMaxLengthAdapter
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public static void testMaxLengthAdapter() throws ScriptException {
final ScriptEngine e = createEngine();
e.put("MaxParams", MaxParams.class);
final MaxParams s = (MaxParams) e.eval("new MaxParams.static(function(){ return arguments })");
final ScriptObjectMirror m = (ScriptObjectMirror)s.method(true, Byte.MIN_VALUE, Short.MIN_VALUE, 'a', Integer.MAX_VALUE, Float.MAX_VALUE, Long.MAX_VALUE, Double.MAX_VALUE,
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
"45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62",
"63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80",
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98",
"99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114",
"115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130",
"131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146",
"147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162",
"163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178",
"179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194",
"195", "196", "197", "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "213", "214", "215", "216", "217", "218", "219", "220", "221", "222", "223", "224", "225", "226",
"227", "228", "229", "230", "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", "242",
"243", "244", "245", "246", "247", "248", "249", "250", "251");
Assert.assertEquals(true, m.getSlot(0));
Assert.assertEquals(Integer.valueOf(Byte.MIN_VALUE), m.getSlot(1)); // Byte becomes Integer
Assert.assertEquals(Integer.valueOf(Short.MIN_VALUE), m.getSlot(2)); // Short becomes Integer
Assert.assertEquals(Character.valueOf('a'), m.getSlot(3));
Assert.assertEquals(Integer.valueOf(Integer.MAX_VALUE), m.getSlot(4));
Assert.assertEquals(Double.valueOf(Float.MAX_VALUE), m.getSlot(5)); // Float becomes Double
Assert.assertEquals(Long.valueOf(Long.MAX_VALUE), m.getSlot(6)); // Long was untouched
Assert.assertEquals(Double.valueOf(Double.MAX_VALUE), m.getSlot(7));
for (int i = 8; i < 252; ++i) {
Assert.assertEquals(String.valueOf(i), m.getSlot(i));
}
}
示例13: setVariable
import javax.script.ScriptEngine; //导入方法依赖的package包/类
protected void setVariable(ScriptEngine scriptEngine, String name, Object value) {
scriptEngine.put(name, value);
try {
if (value != null) {
scriptEngine.eval("val " + name + " = bindings[\"" + name + "\"] as " + value.getClass().getName());
} else {
scriptEngine.eval("val " + name + " = bindings[\"" + name + "\"]");
}
} catch (ScriptException e) {
throw SpongeUtils.wrapException(e);
}
}
示例14: callableJSObjectTest
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// a callable JSObject
public void callableJSObjectTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.put("sum", new Adder());
// check callability of Adder objects
assertEquals(e.eval("typeof sum"), "function");
assertEquals(((Number)e.eval("sum(1, 2, 3, 4, 5)")).intValue(), 15);
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例15: testReturnAdapter
import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public static void testReturnAdapter() throws ScriptException {
final ScriptEngine e = createEngine();
e.put("SupplierSupplier", SupplierSupplier.class);
final SupplierSupplier s = (SupplierSupplier) e.eval("new SupplierSupplier.static(function(){ return function() { return 'foo' } })");
Assert.assertEquals("foo", s.getSupplier().get());
}