I want to set a Java method so that it can be called by arbitrary scripts. Then the script can do arithmetic with the return value.
The problem is that although the public method returns Java Integer, the script does not actually get a regular number, but an instance org.mozilla.javascript.NativeJavaObject.
Here are some simplified test codes that show the behavior:
public class RhinoTest
{
public static void main(String[] args)
{
String script = "foo.getBar() + 1";
Context context = Context.enter();
ScriptableObject scriptableObject = context.initStandardObjects();
ScriptableObject.putProperty(scriptableObject, "foo", new Foo());
Object result = context.evaluateString(scriptableObject, script, "FooBar", 1, null);
Context.exit();
System.out.println(result);
}
public static class Foo
{
public Integer getBar()
{
return 9;
}
}
}
The expected result 10, but the script returns 91.
So, how can I make the calls getBar()inside the script actually return the regular Javascript data type? Note that I do not want to modify the script code by adding unwrap()calls, parseint()or the like.