Search code examples
gwtoverlaypojo

Mixing Java POJOs & GWT Overlay types


In my app I use JsArray extensively to store my overlay types. I use java.util.List to store my client-side Java POJOs.

For performance reasons and to unify the way I access my model I planned to eliminate the Lists and use only JSO wrappers. Given a wrapper around a native array that can store any Java Object:

 public class JsArrayObject<T> extends JavaScriptObject {

        protected JsArrayObject() {}

        public final native T get(int index) /*-{
            return this[index];
        }-*/;

        public final native void push(T value) /*-{
            this[this.length] = value;
        }-*/;
    }

Is it safe to store Java Objects this way? The doc says that when you pass a Java Object into JavaScript the result is "an opaque value accessible through special syntax". This sounds confussing to me. For instance if I push an Integer and try to get it an exception will be thrown because something different than an Object was found (at least in dev mode). The same happens with the rest of Java primitive Wrappers. Apart from the problems with Java primitive Wrappers, are there other concerns to be aware?

Many thanks


Solution

  • Which doc are you referring to? The one on this page?

    They're talking in terms of passing Java objects into JavaScript with the intent of having JavaScript code use the methods or fields in the object. It is possible to do that, but the syntax you have to use on the JavaScript side is a bit awkward. If you've done any JSNI, you've seen it.

    If you don't intend to access the Java objects from the JavaScript side you can ignore the business about the special syntax. So yes, it is safe. I'd be interested to know if it actually turns out to help performance.