Reputation: 6317
Im trying to test ScriptEngine for my own needs. In my Java program I have a variable:
HashMap<String,HashMap<String,String[]>> mymap = new HashMap<String,HashMap<String,String[]>>();
for the example, it contains {Source, {service = ["TCP"]}}
Now, I want to be able to pass this map to the ScriptEngine in order to evaluate expression while reading the content of my map.
I tried to do the following:
HashMap<String,String[]> Source = new HashMap<String,String[]>();
Source.put("service", new String[]{"TCP"});
mymap.put("Source", Source);
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
Bindings bindings = new SimpleBindings();
bindings.put("map", mymap);
String exp = "println(\"Hello from inside scripting!\");"
+ "println(\"map = \" + map)";
engine.eval(exp,bindings);
Yet it doesnt work. I get:
Hello from inside scripting!
mymap = {Source={service=[Ljava.lang.String;@4e2e29c}}
I tried both map.Source or map['Source']
yet neither of them worked.
How do i get it to work? So that the string would pass OK and evaluate it?
And much more importantly, since it would solve any of my issues, Can I debug the javascript runtime? so I could see what happens there.
Upvotes: 2
Views: 2380
Reputation: 6317
I managed to solve this by realizing from other examples, that the syntax inside the evaluation string should be the syntax of a Java application, therefore, as a map, I did:
map.get('Source').get('service')
The first .get() returns the value of the map Source key, then the second .get() returns the value of service. I really wonder what is the performance of that type of evaluation process. and moreover, if its possible to debug it?
Upvotes: 1