简体   繁体   中英

How to call JSNI method from JSNI method in GWT

Is it possible to call native method from another native method in the same class in this way?

public native JavaScriptObject mySECONDJsniMethod(String name) /*-{
        //..
        return secondVar;
}-*/;

public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
        var secondVar = mySECONDJsniMethod(name);
        //..
        return firstVar;
}-*/;

In my Chrome Console it returns: Uncaught TypeError: mySECONDJsniMethod is not a function . Any ideas? Thank you.

Yes. Within the JSNI for myFIRSTJsniMethod(), use the normal JSNI syntax for calling a Java method.

For example, if these two methods are in the class com.mycompany.Test , you could call mySECONDJsniMethod() like so:

public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
    var secondVar = this.@com.mycompany.Test::mySECONDJsniMethod(Ljava/lang/String;)(name);
    //..
    return firstVar;
}-*/;

See the GWT documentation on Accessing Java Methods and Fields from JavaScript for more information.

EDIT: Here is a complete compilable example:

package com.mycompany.client;

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

public class SO26277049 implements EntryPoint {

    @Override
    public void onModuleLoad() {
        final JavaScriptObject firstVar = myFIRSTJsniMethod("hello world!");
    }

    public native JavaScriptObject mySECONDJsniMethod(String name) /*-{
        var secondVar = {
            name: name
        };
        return secondVar;
    }-*/;

    public native JavaScriptObject myFIRSTJsniMethod(String name) /*-{
        var secondVar = this.@com.mycompany.client.SO26277049::mySECONDJsniMethod(Ljava/lang/String;)(name);
        $wnd.alert(secondVar.name);
        var firstVar = secondVar;
        return firstVar;
    }-*/;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM