I'm using java 6 javax.script feature but I have an issue :
Before I call the ScriptEngine.eval() method I put some attributes to the ScriptContext:
scriptContext.setAttribute("Utils", utils, ScriptContext.ENGINE_SCOPE);
In the script I call :
var s = utils.getMyString()
The Java getMyString() method returns a String (java.lang.String).
The type of 's' in the scriptContext is sun.org.mozilla.javascript.internal.NativeJavaObject that wrap the Java String instance.
When I try to get the attribute from the context in Java with:
(String) scriptContext.getAttribute("s");
I got
java.lang.ClassCastException:
sun.org.mozilla.javascript.internal.NativeJavaObject cannot be cast to java.lang.String
When I write in the script:
var s = "hello world"
or
var s = "" + utils.getMyString()
or
var s = String(utils.getMyString())
all is well because these are javascript Strings that can be get from the scriptContext thanks to an internal conversion.
I think that NativeJavaObjects should be unwrapped (see sun.org.mozilla.javascript.internal.Wrapper.unwrap()) when they are released from the scriptContext.
So, is it a bug ? I have the same issue with java7u5.
I don't beleive that I would have to do :
var s = String(utils.getMyString())
to convert a Java String to a JavaScript String to be able to get it back as a Java String...
Thanks for your point of view.