Catersa
Catersa

Reputation: 125

GWT JSNI split method bug

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,

Upvotes: 0

Views: 602

Answers (2)

Catersa
Catersa

Reputation: 125

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 :).

Upvotes: 0

Colin Alworth
Colin Alworth

Reputation: 18331

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.

Upvotes: 1

Related Questions