@Override public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable)engine).invokeFunction(name, args); }
@Override public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable)engine).invokeFunction(name, args); }
@Override public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable) engine).invokeFunction(name, args); }
@Override public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable) engine).invokeFunction(name, args); }
private void loadCustomModule(Invocable invocable, String path, Charset charset) throws ScriptException, NoSuchMethodException { invocable.invokeFunction("__greys_load", path, charset.name()); }
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); // read script file engine.eval(Files.newBufferedReader(Paths.get("C:/Scripts/Jsfunctions.js"), StandardCharsets.UTF_8)); Invocable inv = (Invocable) engine; // call function from script file inv.invokeFunction("yourFunction", "param");
public AsciiMathOld(String form) throws IOException, ScriptException, ParserConfigurationException, NoSuchMethodException { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.eval(JAVASCRIPT_CODE); final Invocable inv = (Invocable) engine; final Document dom = createDocument(); mathML = (Node) inv.invokeFunction("plantuml", dom, form); }
public AsciiMath(String form) throws ScriptException, NoSuchMethodException { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.eval(JAVASCRIPT_CODE); final Invocable inv = (Invocable) engine; this.tex = patchColor((String) inv.invokeFunction("plantuml", form)); this.builder = new LatexBuilder(tex); }
html = ((Invocable) engine).invokeFunction(this.renderFunction, template, model, context);
html = ((Invocable) engine).invokeFunction(this.renderFunction, template, model, context);
@Test public void testInvocableFunction() throws ScriptException, NoSuchMethodException { // tag::jsr223_invocable[] ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("groovy"); String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }"; engine.eval(fact); Invocable inv = (Invocable) engine; Object[] params = {5}; Object result = inv.invokeFunction("factorial", params); assertEquals(new Integer(120), result); // end::jsr223_invocable[] } }
@Override public <T> T executeTemplate(String template, HttpRequest request, Class<? extends DTO<T>> dtoClass) { T result = null; String script = "function handle(request) {" + indentAndToString(template)[0] + "}"; try { if (engine != null) { engine.eval(script + " function serialise(request) { return JSON.stringify(handle(JSON.parse(request)), null, 2); }"); // HttpResponse handle(HttpRequest httpRequest) - ES5 Object stringifiedResponse = ((Invocable) engine).invokeFunction("serialise", new HttpRequestTemplateObject(request)); logFormatter.info(TEMPLATE_GENERATED, request, "generated output:{}from template:{}for request:{}", stringifiedResponse, script, request); result = httpTemplateOutputDeserializer.deserializer(request, (String) stringifiedResponse, dtoClass); } else { logFormatter.error(request, "JavaScript based templating is only available in a JVM with the \"nashorn\" JavaScript engine, " + "please use a JVM with the \"nashorn\" JavaScript engine, such as Oracle Java 8+", new RuntimeException("\"nashorn\" JavaScript engine not available")); } } catch (Exception e) { throw new RuntimeException(formatLogMessage("Exception transforming template:{}for request:{}", script, request), e); } return result; } }
html = ((Invocable) engine).invokeFunction(this.renderFunction, template, model, context);
public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader("java8-nashorn/src/main/resources/nashorn1.js")); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("fun1", "Peter Parker"); System.out.println(result); System.out.println(result.getClass()); invocable.invokeFunction("fun2", new Date()); invocable.invokeFunction("fun2", LocalDateTime.now()); invocable.invokeFunction("fun2", new Person()); }
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("ECMAScript"); try { String jsPath = ScriptEngineDemo.class.getResource("/test.js").getPath(); engine.eval(new FileReader(jsPath)); Invocable invokableEngine = (Invocable) engine; Object ret = invokableEngine.invokeFunction("test", null); System.out.println("The result is : " + ret); } catch (Exception e) { e.printStackTrace(); } } }
public static void main(String[] args) throws ScriptException, NoSuchMethodException { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("function foo(predicate, obj) { return !!(eval(predicate)); };"); Invocable invocable = (Invocable) engine; Person person = new Person(); person.setName("Hans"); String predicate = "obj.getLengthOfName() >= 4"; Object result = invocable.invokeFunction("foo", predicate, person); System.out.println(result); }
public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('java8-nashorn/src/main/resources/nashorn5.js')"); Invocable invocable = (Invocable) engine; Product product = new Product(); product.setName("Rubber"); product.setPrice(1.99); product.setStock(1037); Object result = invocable.invokeFunction("getValueOfGoods", product); System.out.println(result); }
import java.util.*; import javax.script.*; public class TestBindings { public static void main(String args[]) throws Exception { String script = "function doSomething() {var d = date}"; ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); Compilable compilingEngine = (Compilable) engine; CompiledScript cscript = compilingEngine.compile(script); //Bindings bindings = cscript.getEngine().createBindings(); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for(Map.Entry me : bindings.entrySet()) { System.out.printf("%s: %s\n",me.getKey(),String.valueOf(me.getValue())); } bindings.put("date", new Date()); //cscript.eval(); cscript.eval(bindings); Invocable invocable = (Invocable) cscript.getEngine(); invocable.invokeFunction("doSomething"); } }
public static void main(String[] args) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("load('java8-nashorn/src/main/resources/nashorn6.js')"); Invocable invocable = (Invocable) engine; Product product = new Product(); product.setName("Rubber"); product.setPrice(1.99); product.setStock(1337); ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("calculate", product); System.out.println(result.get("name") + ": " + result.get("valueOfGoods")); }
public static void main1(String[] args) throws IOException, ScriptException, NoSuchMethodException { JSCodeCompiler jsCodeCompiler = new JSCodeCompiler(null, null, new JSLoggerService() { @Override public ILogger createLogger(String project, String prefix) { return new JSCodeCompiler.TestLogger(); } @Override public ILogger createLogger(String project, String prefix, String identifier) { return new JSCodeCompiler.TestLogger(); } }, new JavascriptConfig()); Invocable engine = jsCodeCompiler.createEngine("test", new String(ByteStreams.toByteArray(Test.class.getResourceAsStream("example.js"))), "test"); HashMap<Object, Object> map = new HashMap<>(); HashMap<Object, Object> inline = new HashMap<>(); inline.put("param", 1); map.put("test", inline); Object main = engine.invokeFunction("main", map); System.out.println(main); }