2

我正在尝试根据自己的需要测试 ScriptEngine。在我的 Java 程序中,我有一个变量:

HashMap<String,HashMap<String,String[]>> mymap = new HashMap<String,HashMap<String,String[]>>();

例如,它包含{Source, {service = ["TCP"]}} 现在,我希望能够将此映射传递给 ScriptEngine,以便在读取我的映射内容时评估表达式。我尝试执行以下操作:

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);

然而它不起作用。我得到:

Hello from inside scripting!
mymap = {Source={service=[Ljava.lang.String;@4e2e29c}}

我都试过了,map.Source or map['Source']但它们都没有奏效。我如何让它工作?这样字符串就可以通过并评估它?更重要的是,因为它可以解决我的任何问题,我可以调试 javascript 运行时吗?所以我可以看到那里发生了什么。

4

1 回答 1

1

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?

于 2013-10-30T12:16:49.740 回答