Java调用代码:
import jdk.nashorn.api.scripting.*;
....
myCustomHashMap dataStore = new myCustomHashMap();
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("nashorn");
engine.put("dataStore",dataStore);
engine.eval(new java.io.FileReader("test.js"));
((Invocable)engine).invokeFunction("jsTestFunc", "testStr" );
Javascript:
function jsTestFunc (testParam)
{ dataStore.a = [1,2,3];
dataStore.b = {First:"John",Last:"Doe",age:37}; }
目标:
I need to JSONify the dataStore after the script execution
with no dependence on the script for assistance
dataStore.a -> jdk.nashorn.internal.objects.NativeArray
dataStore.b -> jdk.nashorn.internal.scripts.JO4
对于每个 Map 值,我尝试过但失败了:
- 转换为 ScriptObject 或 ScriptObjectMirror
- 投射到地图或列表
- 直接访问 JO4/NativeArray 方法
- ScriptUtils.wrap() / ScriptUtils.unwrap()
我尝试过覆盖该HashMap.put()
方法,但它似乎没有被转换为 a ScriptObjectMirror
on assignments,仅在显式函数调用上:
dataStore.x = [1,2,3] ; -> jdk.nashorn.internal.objects.NativeArray
javaHost.javaFunc( [1,2,3] ); -> ScriptObjectMirror
我真的需要使用 myCustomHashMap (它时间戳更改并维护更改列表等),所以我不能从根本上改变这种安排。我该怎么做才能恢复这些数据?