Created
September 21, 2016 14:46
-
-
Save joaovarandas/57ff0b539e9fbf82974959dfb168b468 to your computer and use it in GitHub Desktop.
Nested Evaluation Test in Nashorn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
System.out.println(o); | |
o = scriptEngine.eval("this[\"obj-java\"].key1.key2.key3", b); | |
System.out.println(o); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment