use of jdk.nashorn.api.scripting.ClassFilter in project vorto by eclipse.
the class JavascriptEvalFunction method invoke.
@Override
@SuppressWarnings({ "rawtypes" })
public Object invoke(ExpressionContext context, Object[] parameters) {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new ClassFilter() {
@Override
public boolean exposeToScripts(String s) {
return false;
}
});
try {
final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.remove("print");
bindings.remove("load");
bindings.remove("loadWithNewGlobal");
bindings.remove("exit");
bindings.remove("quit");
engine.eval(functionBody);
} catch (ScriptException e) {
throw new JXPathException("Problem evaluating " + functionName, e);
}
Invocable inv = (Invocable) engine;
Object[] args;
int pi = 0;
Class[] types = toTypes(parameters);
if (types.length >= 1 && ExpressionContext.class.isAssignableFrom(types[0])) {
pi = 1;
}
args = new Object[parameters.length + pi];
if (pi == 1) {
args[0] = context;
}
for (int i = 0; i < parameters.length; i++) {
args[i + pi] = TypeUtils.convert(parameters[i], types[i + pi]);
}
try {
return inv.invokeFunction(functionName, unwrap(args));
} catch (NoSuchMethodException e) {
throw new JXPathInvalidAccessException("Cannot find function with the list of parameters", e);
} catch (ScriptException e) {
throw new JXPathInvalidAccessException("Problem executing javascript", e);
}
}
Aggregations