Call Java method from GWT JSNI

Firstly, yes, I already searched and already found this answer:

GWT JSNI - string passing problem

I am trying to call a java method from a JSNI method, but they will not go anywhere. I tried the advice above, but it still won't work.

Here is the code:

public native void initCustomListeners(MapmakerMapViewPresenter.MyView view) /*-{ //public native void initCustomListeners() /*-{ $wnd.getLocationDescriptions = function(lng, lat) { $entry( view.@org.jason.mapmaker.client.view.MapmakerMapViewImpl ::getLocationDescriptions(DD)(lng, lat)); } $wnd.google.maps.event.addListener(map, 'click', function(event) { var lat = event.latLng.lat().toFixed(3); var lng = event.latLng.lng().toFixed(3); alert("(" + lat + ", " + lng + ")"); $wnd.getLocationDescriptions(lng, lat); alert("Test!"); }); }-*/; @Override public void getLocationDescriptions(double lng, double lat) { getUiHandlers().doGetLocationDescriptions(lng, lat); } 

Can anyone help me out?

Jason

+4
source share
3 answers

I don’t know if this problem was (you don’t even say how this code behaves versus how you expect it to behave), but there are several errors in your code:

  • $entry wraps the function, so you need to call the returned function, and not (uselessly) wrap the function result after calling it! That is, $entry(function(lat,lng) { foo.@bar.Baz ::quux(DD)(a, b); } , not $entry( foo.@bar.Baz ::quux(DD)(a, b))

  • you addListener on map , but this variable is never defined.

+3
source

I am still missing something, and I have been looking at this code for quite some time.

Here's the current version of the code:

 public native void initCustomListeners(JavaScriptObject map) /*-{ var that = this; $wnd.getLocationDescriptions = function(lng, lat) { $entry( that.@org.jason.mapmaker.client.presenter.MapmakerMapViewPresent er::doGetLocationDescriptions(DD))(lng,lat); } $wnd.google.maps.event.addListener(map, 'click', function(event) { var lat = event.latLng.lat(); var lng = event.latLng.lng(); alert("(" + lat + ", " + lng + ")"); @com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("calling $wnd.getLocationDescriptions()"); $wnd.getLocationDescriptions(lng, lat); @com.google.gwt.core.client.GWT::log(Ljava/lang/String;)("called $wnd.getLocationDescriptions()"); }); }-*/; 

As a result of the call, a ClassCastException is thrown. Sorry if I missed something that should be obvious.

0
source

One error that I see is that you should:

 $wnd.getLocationDescriptions = $entry(@org.jason.mapmaker.client.view.MapmakerMapViewImpl::getLocationDescriptions(DD)(lng, lat)); 

(not with the wrapper function used), and then call the function from javascript via:

 $wnd.getLocationDescriptions(lng, lat); 

Also, the variable 'this' is not needed (or 'this'), before @. I'm also not sure about $ wnd.google.maps.event.addListener (thing, are you sure that such an object is assigned to $ wnd?

Finally, look again at this:

https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

0
source

All Articles