GWT JSNI split method bug

感情迁移 提交于 2019-12-11 14:52:44

问题


I am developing a GWT application and I am obtaining a List containing the result of a select query. 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. 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; so i took that approach.

The JSNI method that i am using is the following (which i picked up from this same site):

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;

And even if I write a .toString() at the end, the error becomes the following:

java.lang.ClassCastException: java.lang.String cannot be cast to [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.

str it is a String containing an iterator.next().

Could you please tell me what could i be missing or misunderstanding?.

Thank you in advance for your time,

Kind regards,


回答1:


A JavaScript list is not a Java array. While GWT uses JavaScript lists to emulate Java arrays, that doesn't mean that they are the same thing.

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.




回答2:


Thank you for the response, 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:

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

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

Thanks a lot for the help, it works like a charm :).



来源:https://stackoverflow.com/questions/20230300/gwt-jsni-split-method-bug

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!