简体   繁体   中英

How to use GWT $entry Function in JSNI?

The $entry function in GWT is used to get uncaught exceptions reported from JavaScript to GWT.

What is the difference between the following $entry calls and which one is the correct one?

The following versions call instance Java functions.

Version 1:

public final native String test(double arg) /*-{
    var instance = this;

    var callExternal = $entry(function(arg) {
       return instance.@com.example.MyClass::javaFunction(D)(arg);
    });

    var x = callExternal(arg);
}-*/;

Version 2:

public final native String test(double arg) /*-{
    var x = $entry(instance.@com.example.MyClass::javaFunction(D)(arg));
}-*/;

Is there a different use whether I use static or non static java functions?

Update:

The following versions call static Java functions.

Version 1:

public final native String test(double arg) /*-{

    var callExternal = $entry(function(arg) {
       return@com.example.MyClass::javaFunction(D)(arg);
    });

    var x = callExternal(arg);
}-*/;

Version 2:

public final native String test(double arg) /*-{
    var x = $entry(@com.example.MyClass::staticJavaFunction(D)(arg));
}-*/; 

The second one is wrong: it calls the method then wraps the result. It's useless, possibly even broken.

$entry wraps a function in another function that uses a try/catch.

Question:

Is there a different use whether I use static or non static java functions?

Answer:

There is a error while accessing static java function javaFunction using instance as follow instance.@com.example.MyClass::javaFunction(D)(arg);

Error:

Unnecessary qualifier on static method 'com.example.MyClass.javaFunction'

--EDIT--

Both are working fine for static java function as shown below:

public static void staticJavaFunction(double d){
    System.out.println(d);
}

public final native String test(double arg) /*-{
    var x = $entry(@com.gwt.test.client.GWTTestProject::staticJavaFunction(D)(arg));
}-*/; 

public final native String test(double arg) /*-{
    var callExternal = $entry(function(arg) {
       return @com.gwt.test.client.GWTTestProject::staticJavaFunction(D)(arg);
    });

    var x = callExternal(arg);
}-*/;

Both are also working fine for non static java function as shown below:

Note: just single change in version 2 where var instance = this; was missing.

public void javaFunction(double d){
    System.out.println(d);
}

public final native String test(double arg) /*-{
    var instance = this;
    var x = $entry(instance.@com.gwt.test.client.GWTTestProject::javaFunction(D)(arg));
}-*/;

public final native String test(double arg) /*-{
    var instance = this;

    var callExternal = $entry(function(arg) {
       return instance.@com.gwt.test.client.GWTTestProject::javaFunction(D)(arg);
    });

    var x = callExternal(arg);
}-*/;

I prefer version 2 in both the cases.

You could try it:

public final native String test1(double arg) /*-{
    var instance = this;
    var x = $entry(function(a) {
        return instance.@com.example.MyClass::javaFunction(D)(a);
    })(arg);
    return x;
}-*/;

For static invocation you can use:

public final native String test2(double arg) /*-{
    var x = $entry(@com.example.MyClass::staticJavaFunction(D))(arg);
    return x;
}-*/; 

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