static void nashornInvokeMethod(String code) throws ScriptException, NoSuchMethodException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("nashorn"); engine.eval(code); Invocable inv = (Invocable) engine; JSObject propertiesDict = (JSObject) engine.get("properties"); Object result = null; Object property; long total = 0; for (int i = 0; i < RUNS; ++i) { long start = System.nanoTime(); for (int j = 0; j < BATCH; ++j) { property = propertiesDict.getMember("ssn"); result = inv.invokeMethod(property, "clean", "12345678"); } long stop = System.nanoTime(); System.out.println( "Run " + (i * BATCH + 1) + "-" + ((i + 1) * BATCH) + ": " + Math.round((stop - start) / BATCH / 1000) + " us"); total += (stop - start); } System.out.println("Average run: " + Math.round(total / RUNS / BATCH / 1000) + " us"); System.out.println( "Data is " + ((Invocable) engine).invokeMethod(result, "toString").toString()); }
@Test public void invokeMethod() throws ScriptException, NoSuchMethodException { Object obj = engine.eval("list(execute=sqrt)"); DoubleVector result = (DoubleVector) invocableEngine.invokeMethod(obj, "execute", 16); assertThat(result.length(), equalTo(1)); assertThat(result.get(0), equalTo(4d)); }