7

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 a native method and do this,

// 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 exactly is the difference between the both? Is GWTJavaObject exactly similar to Array?

Why do typeof return "object" in pure javascript but "function" in GWT?

Summary question is, what exactly are the GWT objects converted to in Javascript? 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;
        }-*/;
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
LPD
  • 2,833
  • 2
  • 29
  • 48
  • You're essentially asking how GWT is implemented. Is that what you intend, or is there something more specific? – Potatoswatter Jan 11 '13 at 07:56
  • Thanks for quick response. Am trying to use JSNI inside GWT, thats where all these questions are popping up, because methods I write in JSNI works in browser console but not in GWT. – LPD Jan 11 '13 at 08:03
  • according to [this](https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#passing-java), Java arrays cannot be accessed from JSNI code. Sorry… hope that page and helps you implement a workaround (surely it's possible). – Potatoswatter Jan 11 '13 at 08:06
  • Ah, looks like you need to mangle the type name `ArrayList` and then you can call the `get` method from inside JSNI using some ugly syntax. – Potatoswatter Jan 11 '13 at 08:12
  • @Potatoswatter, not exactly. I was trying to implement a generic clone functionality using the suggestions given here. http://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript and use it in GWT – LPD Jan 11 '13 at 08:37
  • You want to clone Java objects using JSNI code? Nesting objects across a language boundary (*any* inter-language interface) essentially locks you into reference semantics; generically cloning objects as values will be a world of pain. – Potatoswatter Jan 11 '13 at 08:41
  • Yes sir, I want to clone GWT Java objects using JSNI code. Or else every business pojo object should implement clone() method which I am desperately trying to avoid. – LPD Jan 11 '13 at 08:55

1 Answers1

3

An ArrayList in GWT is not translated to a pure JS array: it's a class extending AbstractList and implementing a bunch of interfaces, and this information should be kept when translated to JS so that instanceof checks (in your Java code; e.g. instanceof List or instanceof RandomAccess) still work as expected. An ArrayList is thus implemented as a wrapper around a 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 a Java array is translated to a JS array, but be very careful about what you do to it in JSNI as you could break further Java assumptions (e.g. that an array has a fixed size).

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • Thanks for your reply. Does that mean, GWT translation doesn't necessarily provide all the functionalities given by JS arrays? Am asking this because am trying to have a deep clone feature in my GWT application using the JSNI code as explained in this link. http://stackoverflow.com/questions/14273509/javascript-generic-clone-method-used-in-gwt-application. Should this attempt to find a generic "deep clone" method be debunked all together? – LPD Jan 11 '13 at 10:00
  • Like the example I have given, does the translated GWT object support the JS object features like obj.prototype or obj.constructor? – LPD Jan 11 '13 at 10:24
  • GWT-translated objects are JS objects, so they indeed have a prototype and a constructor. – Thomas Broyer Jan 11 '13 at 11:04
  • But when am trying to get item.prototype or item.__proto__ in my above example its giving me com.google.gwt.core.client.JavaScriptException: (null) @daridra.client.Attempt::nativeMethodCode(Ljava/lang/Object;)([Java object: java.util.ArrayList@17122202]): nul – LPD Jan 11 '13 at 15:03
  • 1
    Oh, these objects are not _standard JS objects_ when in Dev Mode, only in prod mode (or SuperDevMode) – Thomas Broyer Jan 12 '13 at 12:47