Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nested Evaluation Test in Nashorn
import java.util.LinkedHashMap;
import java.util.Map;
import javax.script.Bindings;
import javax.script.ScriptException;
public class ScriptEngineTest {
public static void main(String[] args) throws ScriptException {
javax.script.ScriptEngineManager engineManager = new javax.script.ScriptEngineManager();
javax.script.ScriptEngine scriptEngine = engineManager.getEngineByName("nashorn");
Bindings b = scriptEngine.createBindings();
scriptEngine.eval("this[\"obj-js\"] = { \"key1\": { \"key2\" : { \"key3\": \"value-from-js-1-2-3\" } } }", b);
Map<String, Object> m1 = new LinkedHashMap<>();
b.put("obj-java", m1);
Map<String, Object> m2 = new LinkedHashMap<>();
m1.put("key1", m2);
Map<String, Object> m3 = new LinkedHashMap<>();
m2.put("key2", m3);
Object val = "value-from-java-1-2-3";
m3.put("key3", val);
Object o;
o = scriptEngine.eval("this[\"obj-js\"].key1.key2.key3", b);
o = scriptEngine.eval("this[\"obj-java\"].key1.key2.key3", b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.