Rhino: How to return a string from Java to Javascript?
Asked Answered
I

4

11

How do I use Rhino return a string from Java to Javascript, all I get is org.mozilla.javascript.JavaNativeObject when I use

var jsString = new java.lang.String("test");

inside my js file.

Is this the right way to do it?

var jsString = String(new java.lang.String("test"));

The goal is to have a Java method to return the String object instead of creating it on the fly like above.

Incandescence answered 29/10, 2010 at 9:15 Comment(2)
Isn't it possible to use a simple string literal? var jsString = "test";?Dorsy
if var1 is java.lang.String, simpile to javascript String is : ""+var1Hessite
T
3

In general, you would call Context.javaToJS which converts a Java object to its closest representation in Javascript. However, for String objects, that function returns the string itself without needing to wrap it. So if you're always returning a string, you don't need to do anything special.

Trousers answered 12/11, 2010 at 10:21 Comment(2)
This doesn't solve the problem. For the right solution, see my answer.Pierides
Link is broken when I tried. Had trouble finding alternative link, but the source code is here.Riggs
P
4

Turn off the wrapping of Primitives and then the value returned in your expression will be a JS string:

Context cx = Context.enter();
cx.getWrapFactory().setJavaPrimitiveWrap(false);
Pierides answered 13/5, 2020 at 14:44 Comment(0)
T
3

In general, you would call Context.javaToJS which converts a Java object to its closest representation in Javascript. However, for String objects, that function returns the string itself without needing to wrap it. So if you're always returning a string, you don't need to do anything special.

Trousers answered 12/11, 2010 at 10:21 Comment(2)
This doesn't solve the problem. For the right solution, see my answer.Pierides
Link is broken when I tried. Had trouble finding alternative link, but the source code is here.Riggs
S
3

Although in most cases the returned Java String type can be used just like the JS String type within the JS code, it does not have the same methods!

In particular I found it cannot be used in a JS object passed to 'stringify()' as it does not have the toJSON() method.

The only solution I found is to explicitly do the addition of "" in the JS, to convert the Java String to a JS String. I found no way to code the java method to return a good JS string directly... (as Context.javaToJS() doesn't convert a Java String) Eg:

var jstr = MyJavaObj.methodReturningAString();
JSON.stringify({ "toto":jstr});   // Fails
JSON.stringify({ "toto": ""+jstr});  // OK
Staurolite answered 12/1, 2018 at 11:17 Comment(0)
E
1

For me this is a Rhino bug. The s+"" trick inside JavaScript works, but here's a quick patch to fix it Java-side - after this line in NativeJavaMethod.call()

Object retval = meth.invoke(javaObject, args);

add this check to convert it to a native JavaScript string (ie typeof returns "string" not "object")

if (retval instanceof String) {
    return NativeJavaObject.coerceTypeImpl(String.class, retval);
}

This is important otherwise s.replace() calls the Java version so is wrong for eg "h e l l o".replace(" ", "")

https://github.com/mozilla/rhino/issues/638

Exanimate answered 2/1, 2020 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.