本文整理汇总了Java中javax.script.ScriptEngineManager.getEngineByName方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngineManager.getEngineByName方法的具体用法?Java ScriptEngineManager.getEngineByName怎么用?Java ScriptEngineManager.getEngineByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngineManager
的用法示例。
在下文中一共展示了ScriptEngineManager.getEngineByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: windowLocationTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void windowLocationTest() {
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.location)");
final Object locationValue = e.eval("window.getLocation()");
assertEquals(locationValue, "http://localhost:8080/window");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例2: windowEventTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void windowEventTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Window window = new Window();
try {
e.put("window", window);
e.eval("window.onload = function() { print('window load event fired'); return true }");
assertTrue((Boolean)e.eval("window.onload.loaded()"));
final WindowEventHandler handler = window.getOnload();
assertNotNull(handler);
assertTrue(handler.loaded());
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例3: main
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
final ScriptEngineManager manager = new ScriptEngineManager();
final ScriptEngine engine = manager.getEngineByName("nashorn");
// JavaScript code in a String
final String script = "function run() { print('run called'); }";
// evaluate script
engine.eval(script);
final Invocable inv = (Invocable) engine;
// get Runnable interface object from engine. This interface methods
// are implemented by script functions with the matching name.
final Runnable r = inv.getInterface(Runnable.class);
// start a new thread that runs the script implemented
// runnable interface
final Thread th = new Thread(r);
th.start();
th.join();
}
示例4: getInterfaceNonScriptObjectThizTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
/**
* Check that getInterface on non-script object 'thiz' results in
* IllegalArgumentException.
*/
public void getInterfaceNonScriptObjectThizTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
((Invocable) e).getInterface(new Object(), Runnable.class);
fail("should have thrown IllegalArgumentException");
} catch (final Exception exp) {
if (!(exp instanceof IllegalArgumentException)) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
}
示例5: invokeMethodNonScriptObjectThizTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
/**
* Check that calling method on non-script object 'thiz' results in
* IllegalArgumentException.
*/
public void invokeMethodNonScriptObjectThizTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
((Invocable) e).invokeMethod(new Object(), "toString");
fail("should have thrown IllegalArgumentException");
} catch (final Exception exp) {
if (!(exp instanceof IllegalArgumentException)) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
}
示例6: getInterfaceDifferentContext
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
/**
* Check that we can get interface out of a script object even after
* switching to use different ScriptContext.
*/
public void getInterfaceDifferentContext() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
final Object obj = e.eval("({ run: function() { } })");
// change script context
final ScriptContext ctxt = new SimpleScriptContext();
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
e.setContext(ctxt);
final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class);
r.run();
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例7: testJS
import javax.script.ScriptEngineManager; //导入方法依赖的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();
}
}
示例8: testScriptEngine
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
public static void testScriptEngine() throws Exception {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine js = sem.getEngineByName("JavaScript");
js.eval("blubb = 1\n" +
"muh = blubb + 2\n" +
"// just a comment...\n" +
"crap = \"asd\"");
System.out.println(js.get("muh").getClass());
System.out.println("Testing ScriptEngine Performance...");
long start = System.currentTimeMillis();
Config c = Config.getInstance(new FileInputStream("res/customConfig.js"));
double[] d = new double[]{1.0, 7.5, 1.3+2.5+6.3+3.1, 6.0};;
for (int i = 0; i < 100000; i++) {
//c.pReassignVnf(i);
//d = new double[]{d[0] + 1.0, d[1] - 0.5, d[0] + d[1], d[3] * 1.1};
c.objectiveVector(new double[Solution.Vals.values().length]);
}
System.out.println(Arrays.toString(d));
long dur = System.currentTimeMillis() - start;
System.out.println("Duration: " + dur + "ms");
System.out.println(c.topologyFile.toAbsolutePath().toString());
}
示例9: windowAlertTest
import javax.script.ScriptEngineManager; //导入方法依赖的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());
}
}
示例10: putGlobalFunctionTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void putGlobalFunctionTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.put("callable", new Callable<String>() {
@Override
public String call() throws Exception {
return "callable was called";
}
});
try {
e.eval("print(callable.call())");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例11: engineOverwriteInScriptTest
import javax.script.ScriptEngineManager; //导入方法依赖的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);
}
示例12: buildInvocable
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
/**
* Builds the script invocable
*/
private void buildInvocable() {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
try {
if (hasScript()) {
engine.eval(script.getValue());
inv = (Invocable) engine;
}
} catch (ScriptException e) {
ExceptionHandler.get().handle(e);
}
}
示例13: main
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
public static void main(String args[]) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
try {
nashorn.eval("var aaa = 'www'; print(aaa);");
Integer result = (Integer) nashorn.eval("10 + 2");
System.out.println(result.toString());
} catch (ScriptException e) {
System.out.println("执行脚本错误: " + e.getMessage());
}
}
示例14: engineOverwriteTest
import javax.script.ScriptEngineManager; //导入方法依赖的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);
}
示例15: exposeGlobalTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void exposeGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.put("y", "foo");
e.eval("print(y)");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}