简体   繁体   English

GWT JSNI拆分方法错误

[英]GWT JSNI split method bug

I am developing a GWT application and I am obtaining a List containing the result of a select query. 我正在开发GWT应用程序,并且正在获取包含选择查询结果的列表。 This select query has rows. 该选择查询具有行。 Each row, has each element separated from the previous and the next by "::". 每行的每个元素都用“ ::”与上一个和下一个分隔。

I am trying to split it using String.split, but it is taking ages to perform. 我正在尝试使用String.split拆分它,但是要花很多时间才能执行。 I have read that currently (i am using GWT 2.5.1), the String.split method its quite bugged, sometimes almost taking x1000 times more than the JSNI method to perform; 我已经读过当前的内容(我正在使用GWT 2.5.1),它的String.split方法有很多bug,有时执行的时间是JSNI方法的x1000倍; so i took that approach. 所以我采取了这种方法。

The JSNI method that i am using is the following (which i picked up from this same site): 我正在使用的JSNI方法如下(我是从同一网站上获得的):

public static final native String[] split(String string, String separator) /*-{
   return string.split(separator);
   }-*/;

But now, i am getting this error : 但是现在,我收到此错误:

java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject$ cannot be cast to [Ljava.lang.String; java.lang.ClassCastException:com.google.gwt.core.client.JavaScriptObject $无法转换为[Ljava.lang.String;

And even if I write a .toString() at the end, the error becomes the following: 即使我在末尾编写了一个.toString(),错误也会变成以下内容:

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String; java.lang.ClassCastException:无法将java.lang.String强制转换为[Ljava.lang.String;

I am calling this method like this : 我这样调用此方法:

String[] temp = split(str, "::");

In order to get the results from the split inside temp, for later usage. 为了从temp内部拆分中获取结果,以供以后使用。

str it is a String containing an iterator.next(). str是一个包含iterator.next()的String。

Could you please tell me what could i be missing or misunderstanding?. 您能告诉我我可能会缺少什么或误会吗?

Thank you in advance for your time, 预先感谢您的宝贵时间,

Kind regards, 亲切的问候,

A JavaScript list is not a Java array. JavaScript列表不是 Java数组。 While GWT uses JavaScript lists to emulate Java arrays, that doesn't mean that they are the same thing. 尽管GWT使用JavaScript列表来模拟Java数组,但这并不意味着它们是同一回事。

Instead you should return JsArrayString from your method, and use it that way, or just use the Java version of String.split which returns a real Java array. 相反,您应该从您的方法中返回JsArrayString ,并以这种方式使用它,或者仅使用Java版本的String.split返回真实的Java数组。

Thank you for the response, Colin Alworth. 谢谢您的回应,Colin Alworth。

With your answer, what i did it is the following: 通过您的回答,我做了以下工作:

public static final native JsArrayString split(String string, String separator) /*-{
    return string.split(separator);
    }-*/;

And in the java code: 并在Java代码中:

JsArrayString temp = split(str, "::");

String agentCode = temp.get(1); (an so forth).

Thanks a lot for the help, it works like a charm :). 非常感谢您的帮助,它的作用就像是魅力:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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