本文整理汇总了Java中javax.script.ScriptEngineManager类的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngineManager类的具体用法?Java ScriptEngineManager怎么用?Java ScriptEngineManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ScriptEngineManager类属于javax.script包,在下文中一共展示了ScriptEngineManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeJs
import javax.script.ScriptEngineManager; //导入依赖的package包/类
public Object executeJs(String js,@Nullable String funcName,Object... args){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
Object res=engine.eval(js);
if(StringUtils.isNotBlank(funcName)){
if (engine instanceof Invocable) {
Invocable invoke = (Invocable) engine;
res = invoke.invokeFunction(funcName, args);
}
}
return res;
} catch (Exception e) {
log.error("",e);
}
return null;
}
示例2: main
import javax.script.ScriptEngineManager; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("\nTest4\n");
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = Helper.getJsEngine(m);
if (e == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
e.eval(new FileReader(
new File(System.getProperty("test.src", "."), "Test4.js")));
Invocable inv = (Invocable)e;
Runnable run1 = (Runnable)inv.getInterface(Runnable.class);
run1.run();
// use methods of a specific script object
Object intfObj = e.get("intfObj");
Runnable run2 = (Runnable)inv.getInterface(intfObj, Runnable.class);
run2.run();
}
示例3: ScriptRouter
import javax.script.ScriptEngineManager; //导入依赖的package包/类
public ScriptRouter(URL url) {
this.url = url;
String type = url.getParameter(Constants.TYPE_KEY);
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
if (type == null || type.length() == 0){
type = Constants.DEFAULT_SCRIPT_TYPE_KEY;
}
if (rule == null || rule.length() == 0){
throw new IllegalStateException(new IllegalStateException("route rule can not be empty. rule:" + rule));
}
ScriptEngine engine = engines.get(type);
if (engine == null){
engine = new ScriptEngineManager().getEngineByName(type);
if (engine == null) {
throw new IllegalStateException(new IllegalStateException("Unsupported route rule type: " + type + ", rule: " + rule));
}
engines.put(type, engine);
}
this.engine = engine;
this.rule = rule;
}
示例4: invokeMethodMixEnginesTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
/**
* Check that calling method on mirror created by another engine results in
* IllegalArgumentException.
*/
public void invokeMethodMixEnginesTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine1 = m.getEngineByName("nashorn");
final ScriptEngine engine2 = m.getEngineByName("nashorn");
try {
final Object obj = engine1.eval("({ run: function() {} })");
// pass object from engine1 to engine2 as 'thiz' for invokeMethod
((Invocable) engine2).invokeMethod(obj, "run");
fail("should have thrown IllegalArgumentException");
} catch (final Exception exp) {
if (!(exp instanceof IllegalArgumentException)) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
}
示例5: indexedAccessTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
// array-like indexed access for a JSObject
public void indexedAccessTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
final BufferObject buf = new BufferObject(2);
e.put("buf", buf);
// array-like access on BufferObject objects
assertEquals(e.eval("buf.length"), buf.getBuffer().capacity());
e.eval("buf[0] = 23");
assertEquals(buf.getBuffer().get(0), 23);
assertEquals(e.eval("buf[0]"), 23);
assertEquals(e.eval("buf[1]"), 0);
buf.getBuffer().put(1, 42);
assertEquals(e.eval("buf[1]"), 42);
assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE);
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例6: CustomFrameTitleBuilder
import javax.script.ScriptEngineManager; //导入依赖的package包/类
public CustomFrameTitleBuilder() {
PropertiesComponent prop = PropertiesComponent.getInstance();
projectPattern = prop.getValue(Settings.TEMPLATE_PATTERN_PROJECT, DEFAULT_TEMPLATE_PATTERN_PROJECT);
filePattern = prop.getValue(Settings.TEMPLATE_PATTERN_FILE, DEFAULT_TEMPLATE_PATTERN_FILE);
engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
// evaluate JavaScript Underscore library
engine.eval(new InputStreamReader(getClass().getResourceAsStream("/underscore-min.js")));
// create new JavaScript methods references for templates
engine.eval("var projectTemplate;");
engine.eval("var fileTemplate;");
prepareTemplateSettings();
} catch (Exception e) {
// we took precaution below
}
TitleComponent.addSettingChangeListener(this);
}
示例7: 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 = "var obj = new Object(); obj.run = function() { print('run method called'); }";
// evaluate script
engine.eval(script);
// get script object on which we want to implement the interface with
final Object obj = engine.get("obj");
final Invocable inv = (Invocable) engine;
// get Runnable interface object from engine. This interface methods
// are implemented by script methods of object 'obj'
final Runnable r = inv.getInterface(obj, Runnable.class);
// start a new thread that runs the script implemented
// runnable interface
final Thread th = new Thread(r);
th.start();
th.join();
}
示例8: functionalInterfaceObjectTest
import javax.script.ScriptEngineManager; //导入依赖的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());
}
示例9: factoryJSObjectTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
// a factory JSObject
public void factoryJSObjectTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.put("Factory", new Factory());
// check new on Factory
assertEquals(e.eval("typeof Factory"), "function");
assertEquals(e.eval("typeof new Factory()"), "object");
assertEquals(e.eval("(new Factory()) instanceof java.util.Map"), Boolean.TRUE);
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例10: printManyTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
// check that print prints all arguments (more than one)
public void printManyTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final StringWriter sw = new StringWriter();
e.getContext().setWriter(sw);
try {
e.eval("print(34, true, 'hello')");
} catch (final Throwable t) {
t.printStackTrace();
fail(t.getMessage());
}
assertEquals(sw.toString(), println("34 true hello"));
}
示例11: 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());
}
}
}
示例12: getInterfaceMixEnginesTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
/**
* Check that calling getInterface on mirror created by another engine
* results in IllegalArgumentException.
*/
public void getInterfaceMixEnginesTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine1 = m.getEngineByName("nashorn");
final ScriptEngine engine2 = m.getEngineByName("nashorn");
try {
final Object obj = engine1.eval("({ run: function() {} })");
// pass object from engine1 to engine2 as 'thiz' for getInterface
((Invocable) engine2).getInterface(obj, Runnable.class);
fail("should have thrown IllegalArgumentException");
} catch (final Exception exp) {
if (!(exp instanceof IllegalArgumentException)) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
}
示例13: indexPropertiesExternalBufferTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
public void indexPropertiesExternalBufferTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj");
final ByteBuffer buf = ByteBuffer.allocate(5);
int i;
for (i = 0; i < 5; i++) {
buf.put(i, (byte)(i+10));
}
obj.setIndexedPropertiesToExternalArrayData(buf);
for (i = 0; i < 5; i++) {
assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue());
}
e.eval("for (i = 0; i < 5; i++) obj[i] = 0");
for (i = 0; i < 5; i++) {
assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue());
assertEquals((byte)0, buf.get(i));
}
}
示例14: invokeMethodDifferentContextTest
import javax.script.ScriptEngineManager; //导入依赖的package包/类
@Test
/**
* Check that we can call invokeMethod on an object that we got by
* evaluating script with different Context set.
*/
public void invokeMethodDifferentContextTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
// define an object with method on it
final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");
final ScriptContext ctxt = new SimpleScriptContext();
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
e.setContext(ctxt);
// invoke 'func' on obj - but with current script context changed
final Object res = ((Invocable) e).invokeMethod(obj, "hello");
assertEquals(res, "Hello World!");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例15: getAttributesFromInlineGroovyScript
import javax.script.ScriptEngineManager; //导入依赖的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);
}