Convert GWT to Javascript

In javascript console, if I do this,

a = [1,2,3] Object.prototype.toString.call(a) // gives me "[object Array]" typeof a // gives me "object" 

If I create an arraylist in GWT and pass it to the native method and do it,

 // JAVA code a = new ArrayList<Integer>(); a.push(1); a.push(2); //JSNI code Object.prototype.toString.call(a) // gives me "[object GWTJavaObject]" typeof a // returns "function" 

What is the difference between the two? Is GWTJavaObject exactly similar to an Array ?

Why typeof returns an “ object ” in pure javascript but a “ function ” in GWT?

Consolidated question: in what exactly are GWT objects converted to Javascript? The full code is here.

  public void onModuleLoad() { List<Integer> list = new ArrayList<Integer>(); list.add( new Integer( 100 ) ); list.add( new Integer( 200 ) ); list.add( new Integer( 300 ) ); Window.alert(nativeMethodCode( list )); Window.alert(nativeMethodCode2( list )); } public static final native Object nativeMethodCode( Object item ) /*-{ return Object.prototype.toString.call(item); }-*/; public static final native Object nativeMethodCode2( Object item ) /*-{ return typeof item; }-*/; 
+7
source share
1 answer

An ArrayList in GWT does not translate into a pure JS array: it is a class that extends AbstractList and implements a bunch of interfaces, and this information should be saved when switching to JS so that instanceof checks (in your Java code, for example instanceof List or instanceof RandomAccess ), still working properly. So ArrayList is implemented as a wrapper around the JS array, see https://code.google.com/p/google-web-toolkit/source/browse/tags/2.5.0/user/super/com/google/gwt /emul/java/util/ArrayList.java .

Note that the Java array translates to the JS array, but be very careful what you do with it in JSNI, as you could interrupt further Java assumptions (for example, that the array has a fixed size).

+3
source

All Articles