Thursday, April 2, 2015

JSNI and boilerplate code

Hi everyone,

Trying to work with overlays I often need to write extreme amount of boilerplate code just for transferring data between Java and Javascript.

(a) Take the following simple class as an example. I just want to run the JS function stored in the private variable setFunc for different object types. I understand that the Java objects (Double, Date, Integer etc.) could not have to be appropriately converted, but in the cases of Strings and the primitives it seems rather odd to have to repeat exactly the same code over and over again.

(b) In addition, even the Java Objects are actually used by the application as simple busses for transferring data from a JavaScript part to another through the Java code (e.g. JavaScript nullable number to Double and then back to JavaScript Number).

I suspect that the in both above cases (multiple identical methods and Java Objects as busses) the compiler is not able to optimize the code, so affecting the performance. One possible solution would be to use JavaScriptObject as a bus, but, unfortunately, it seems that it cannot store JS primitives.

I wonder if there is a better way for treating data transfers between JS and Java or if the upcoming next generation JS interoperability solves the problem. I'd appreciate any idea for better treating.

public class ViewPropWritable extends ViewProp {

  private JavaScriptObject setFunc;

  private JavaScriptObject data;

 

  public ViewPropWritable(Binder view, ValueChangeListener uiElement,
      String getExpr) {

    super(view, uiElement, getExpr);

  }

 

  public ViewPropWritable(Binder view, ValueEditor uiElement,
      String
getExpr, String setExpr, String validExpr) {

    super(view, uiElement, getExpr);

    setFunc = getSetFunc(setExpr, validExpr);

    ((ValueEditor)uiElement).bindChangeEvent(this);

  }

 

  public void setScope(Scope scope) {

    this.data = scope.getData();

  }

 

  public boolean setValue(Double value) {

    return value == null ? setNull() : set(value.doubleValue());

  }

 

  public boolean setValue(Boolean value) {

    return value == null ? setNull() : set(value.booleanValue());

  }

      

  public boolean setValue(Date value) {

    return value == null ? setNull() : set(value.getTime());

  }

      

  public native boolean setValue(String value) /*-{

    var f = this.@net.qark.core.ViewPropWritable::setFunc;

    return f(this.data, value);

  }-*/;

 

  public native boolean setValue(JavaScriptObject value) /*-{

    var f = this.@net.qark.core.ViewPropWritable::setFunc;

   return f(this.data, value);

  }-*/;

 

  // Private methods

 

  private native boolean set(double value) /*-{

    var f = this.@net.qark.core.ViewPropWritable::setFunc;

    return f(this.data, value);

  }-*/;

 

  private native boolean set(boolean value) /*-{

    var f = this.@net.qark.core.ViewPropWritable::setFunc;

    return f(this.data, value);

  }-*/;

 

  private native boolean setNull() /*-{

    var f = this.@net.qark.core.ViewPropWritable::setFunc;

    return f(this.data, value);

  }-*/;

 

  private native JavaScriptObject getSetFunc(String setExpr, String validExpr) /*-{

    return function() {

      var sf = new Function("$", "v", setExpr + "=v;");

      if (validExpr) {

        var vf = new Function("$", "v", validExpr);

        return function(d, v) {

          var b = v ? v(d, v) : true;

          if (b) sf(d, v);

          return b;

        }

      } else {

        return function(d, v) {

          return sf(d, v);

        }

      }

    }();

  }-*/;

}


BTW, the documentation of GWT 2.7 suggests that fully qualified class names are not necessary anymore, but Eclipse complains if I set this.@ViewPropWritable::setFunc for example.

Thanks,
Elias

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment