Java call code:
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}; }
Goal:
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
For each map value, I tried and failed with:
- Cast to ScriptObject or ScriptObjectMirror
- Listing for a card or list
- Direct access to JO4 / NativeArray methods.
- ScriptUtils.wrap () / ScriptUtils.unwrap ()
I tried to override the HashMap.put() method, but it does not translate to ScriptObjectMirror when assigned only to explicit function calls:
dataStore.x = [1,2,3] ; -> jdk.nashorn.internal.objects.NativeArray javaHost.javaFunc( [1,2,3] ); -> ScriptObjectMirror
I really need to use myCustomHashMap (these are temporary changes and maintains a list of changes, etc.), so I cannot radically change this scheme. What can I do to return this data?
source share