如何使用多个参数格式化JSNI方法的param-signature?

问题描述 投票:1回答:2

我正在使用GWT并且有一个带有签名的Java方法,该签名需要字符串和布尔参数,如下所示:

private void myMethod(String s, Boolean b) {}

我有一个JSNI方法,在编译后公开这个Java方法:

public class myClass {
    public native void exportMyMethod(myClass c)/*-{
        $wnd.myMethod = $entry(function(s, b) {
            [email protected]::myMethod(Ljava/lang/String;Z);
        });
    }-*/;
}

对于我的生活,当有超过1个参数时,我无法弄清楚如何正确格式化param-signature。

我读过GWT documentation regarding how to do this。我也读过那个文件directs me to how to properly refer to the JNI Type。但我似乎无法找到使用多个参数时如何格式化签名的示例。看起来应该很容易。

那么,我如何正确格式化我的param签名?我试过了:

  • Ç@ com.path.to.myClass :: myMethod的(Ljava /郎/字符串; Z)。
  • Ç@ com.path.to.myClass :: myMethod的。(Ljava /郎/字符串; Ljava /郎/布尔);
  • Ç@ com.path.to.myClass :: myMethod的(Ljava /郎/ StringLjava /郎/布尔)。
  • Ç@ com.path.to.myClass :: myMethod的(Ljava /郎/字符串;,Ljava /郎/布尔)。

我尝试过的每种不同的排列都会导致同样的错误。

“引用方法'com.path.to.myClass.myMethod(Ljava / lang / String; Z)/'无法解析方法。”

gwt jsni
2个回答
5
投票

在Javascript中,与Java不同,您实际上可以传递一个方法,就像它是一个变量一样 - 您可以重新分配它,将其分配给变量等。这意味着对于JSNI引用工作,我们需要的方法不仅仅是打电话给他们,但要引用他们。

然后标准的JSNI模式是[email protected]::method(arg;types;)(actual, params)

在你的情况下,这一行

[email protected]::myMethod(Ljava/lang/String;Z);

应该改成这样的东西

[email protected]::myMethod(Ljava/lang/String;Z)(s, b);

请注意,Z指的是boolean,而不是Boolean,所以你问题中的当前代码是不一致的。如果只有一个具有特定名称的方法,则可以省略类型并只传递*

[email protected]::myMethod(*)(s, b);

2
投票

类引用以L开头,以;结尾,参数类型不分开;所以只有前两个签名格式正确:

第一个采取boolean,第二个采取java.lang.Boolean

© www.soinside.com 2019 - 2024. All rights reserved.