bouhmid_tun
bouhmid_tun

Reputation: 344

Get returned value from java method with GWT's JSNI

I'm trying to get the returned value from a java method, but it returns something very strange: it returns the method itself written in javascript I think.

Here the code of the java method:

public String getNameToShow() {
        return "Chart number 1";
    }

and the javascript method:

 public native void drawJSChart(String divId, int a, String jsData) /*-{
            try {
                //First create a script where to paste the jsData
                var scriptID = [email protected]::getNameToShow();
                console.log(scriptID);
                //Some code
            } catch (e) {
                console.error(e.message);
            }
        }-*/;

Thank you.

Upvotes: 1

Views: 2148

Answers (2)

Stefan
Stefan

Reputation: 14863

You have to pass the types of your Java function too. Writing it like this works:

package XXXXX.client;

import com.google.gwt.core.client.EntryPoint;

class _24_TestJSNIMethodCallback implements EntryPoint {
    public void onModuleLoad() {
        drawJSChart();
    }

    public String getNameToShow() {
        return "Chart number 1";
    }

    public native void drawJSChart() /*-{
        try {
            //First create a script where to paste the jsData 
            var scriptID = this
                    [email protected]._24_TestJSNIMethodCallback::getNameToShow()();
            $wnd.alert(scriptID);
            //Some code 
        } catch (e) {
            console.error(e.message);
        }
    }-*/;

}

Upvotes: 1

jusio
jusio

Reputation: 9900

It returns js method because, you've asked for js method.

Invocation of java methods from JSNI code should look like this:

var scriptID = [email protected]::getNameToShow(*)(); //notice second pair of braces

Basically to invoke java method from JSNI, you will need to place two pairs of braces. First defines method parameter types (in my example I've used * so it will match any parameter types), second is used to pass parameters into the method.

Upvotes: 5

Related Questions