How to use Java varargs with the GWT Javascript Native Interface? (in other words, "GWT does not have printf ()")

I am trying to quickly learn GWT as part of a new project. I found out that GWT does not implement the Java function String.format (), so there is no printf () function. I knew that there were some implementations of printf () for Javascript, so I decided that I could insert one of them as a GWT Javascript Native Interface function. I ran into problems and decided it was better to make sure that the varargs values ​​were passed correctly. What is where everything has become ugly. Firstly, some sample code:

// From Java, call the JSNI function:
test("sourceString", "params1", "params2", "params3");

....

public static native void test(Object... params) /*-{   
    // PROBLEM: this kills GWT!
    // alert(params.length);  

    // returns "function"
    alert(typeof(params));      

    // returns "[Ljava.lang.Object;@b97ff1"
    alert(params);
}-*/;

GWT , " JavaScript varargs Java , ". , , params.length, JavascriptException, UmbrellaException, . "typeof (params)", "function". , params, , Java.

, , :

1) GWT/JSNI, - ?
2) ? 3) printf() GWT?

+5
1

JSNI , , , . Java (varargs ) JSNI. JsArrayString:

public static native void test(JsArrayString strings) /*-{
  // strings is a normal JavaScript array of strings
}

:

String[] jStrings = {"one", "two", "three"};
JsArrayString jsStrings = (JsArrayString)JsArrayString.createArray();

for (String s : jStrings) {
  jsStrings.push(s);
}

test(jsStrings);
+5

All Articles