Reputation: 1398
The JSNI method does not accept any parameters but return a Java Object type:
public static native String nativeJSFuncGwt() /*-{
$wnd.console.log($wnd.someJSFunc());
return "" + $wnd.someJSFunc() + "" ;
}-*/;
//someJSFunc returns { abc:xcv, def:asd}
I can see the value getting printed in the javascript console but java side is not able to understand the casting.
Is it because the native method does not accept any parameters ?
String tokenFromNativeJS = nativeJSFuncGwt(); // String value is null
The documentation also is not clear enough in GWT.
Upvotes: 0
Views: 1305
Reputation: 1398
I am posting here what finally worked for due to GWT version(2.4) constraint
From GWT Doc:
Outgoing Java type:
Any other Java Object (including arrays)
What must be passed:
Java Object of the correct type that must have originated in Java code; Java objects cannot be constructed from “thin air” in JavaScript
My code with modification would like:
public static native MyObject nativeJSFuncGwt(MyObject obj) /*-{
var xyz = $wnd.someJsFunc();
[email protected]::setter1(Ljava/lang/String;)(xyz);
return obj;
}-*/;
I wish documentation could have been more clear.
Upvotes: 0
Reputation: 18356
Step one, avoid JSNI, you are better off defining a JsInterop method which provides the same API access. JSNI will still work in GWT2 but JsInterop is the way forward for GWT3, and is often much easier to read and write. This would look something like this:
@JsMethod(name = "someJSFunc", namespace = JsPackage.GLOBAL)
public static native String someJSFunc();
Step two, define a Java type which fits your expected JS return value. This will work with either JSNI or JsInterop. In JSNI you would make a JavaScriptObject subclass, and provide methods which access the fields (see http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html and other docs for more detail, but as per step one, I'm not going to go into more depth on this here). For your example object, this would look like this in JsInterop:
@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
public class SomeReturnObject {
public String abc;
public double def;
}
Obviously replace the field names and types with whatever is appropriate in your own project. Give this new type with the placeholder name, here's what your global someJsFunc would look like:
@JsMethod(name = "someJSFunc", namespace = JsPackage.GLOBAL)
public static native SomeReturnObject someJSFunc();
And you would use it like you expect in plain Java - no need to write JSNI any more:
SomeReturnObject object = someJSFunc();
DomGlobal.console.log(object.abc + ": " + object.def);
Upvotes: 5