Well, you can try and get the bindings that the script creates:
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine se = mgr.getEngineByName("JavaScript"); try { se.eval("var x;var a,b,c,d;var y = \"wow\";var z = y+'x';"); Bindings bindings = se.getBindings(ScriptContext.ENGINE_SCOPE); System.out.println(bindings.keySet()); } catch (ScriptException e) {
this prints [d, b, c, println, a, context, z, y, print, x]
as you can see, some additional bindings are defined: context, print and println
and here we filter them
Set<String> keySet = bindings.keySet(); keySet.removeAll(Arrays.asList("context", "print", "println")); System.out.println(keySet);
it prints [d, b, c, a, z, y, x]
source share