use of javax.script.Invocable in project selenium_java by sergueik.
the class ScraperVkClient method decrypt.
@SneakyThrows
private String decrypt(Long vkId, String url) {
// TODO: replace with bindings
String script = this.script.replace("${vkId}", vkId.toString());
scriptEngine.eval(script);
Invocable inv = (Invocable) scriptEngine;
return (String) inv.invokeFunction("decode", url);
}
use of javax.script.Invocable in project groovy-core by groovy.
the class JSR223SpecTest method testInvocableFunction.
@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[]
}
use of javax.script.Invocable in project typescript-generator by vojtechhabarta.
the class SymbolTable method getCustomTypeNamingFunction.
private CustomTypeNamingFunction getCustomTypeNamingFunction() throws ScriptException {
if (customTypeNamingFunction == null) {
final String engineMimeType = "application/javascript";
final ScriptEngineManager manager = new ScriptEngineManager();
// getting ScriptEngine from manager doesn't work in Maven plugin on Java 9
// final ScriptEngine engine = manager.getEngineByMimeType(engineMimeType);
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
if (engine == null) {
System.out.println(String.format("Error: Script engine for '%s' MIME type not found. Available engines: %s", engineMimeType, manager.getEngineFactories().size()));
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
System.out.println(String.format(" %s %s - MIME types: %s", factory.getEngineName(), factory.getEngineVersion(), factory.getMimeTypes()));
}
throw new RuntimeException("Cannot evaluate function specified using 'customTypeNamingFunction' parameter. See log for details.");
}
engine.eval("var getName = " + settings.customTypeNamingFunction);
final Invocable invocable = (Invocable) engine;
customTypeNamingFunction = invocable.getInterface(CustomTypeNamingFunction.class);
}
return customTypeNamingFunction;
}
use of javax.script.Invocable in project java8-tutorial by winterbe.
the class Nashorn1 method main.
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("res/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());
}
use of javax.script.Invocable in project java8-tutorial by winterbe.
the class Nashorn7 method main.
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);
}
Aggregations