[英]How to use GWT $entry Function in JSNI?
The $entry function in GWT is used to get uncaught exceptions reported from JavaScript to GWT. GWT中的$ entry函数用于获取从JavaScript报告给GWT的未捕获异常。
What is the difference between the following $entry calls and which one is the correct one? 以下$ entry调用和正确的调用有什么区别?
The following versions call instance Java functions. 以下版本调用实例Java函数。
Version 1: 版本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: 版本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? 我使用静态或非静态Java函数有其他用途吗?
Update: 更新:
The following versions call static Java functions. 以下版本调用静态Java函数。
Version 1: 版本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: 版本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. $entry
将一个函数包装在另一个使用try / catch的函数中。
Question: 题:
Is there a different use whether I use static or non static java functions? 我使用静态或非静态Java函数有其他用途吗?
Answer: 回答:
There is a error while accessing static
java function javaFunction
using instance
as follow instance.@com.example.MyClass::javaFunction(D)(arg);
使用instance
遵循instance.@com.example.MyClass::javaFunction(D)(arg);
访问static
Java函数javaFunction
出错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: 两者都适用于静态java函数,如下所示:
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: 两者对于非静态java函数也都可以正常工作,如下所示:
Note: just single change in version 2 where var instance = this;
注意:版本2中只有一个更改,其中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. 在这两种情况下,我都倾向于使用版本2。
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;
}-*/;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.