title: GWT, JSNI: Implementing an overlay object with fully nullable methods?
body: <p>I'm struggling with JSNI method return types to ensure that one of my DTO object can really have nullable properties. This was the very first and obviously unsuccessful try for the overlay DTO:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Dto extends JavaScriptObject {
  
      protected Dto() {
      }
  
      private native Integer getValue1() /*-{ return this.value1; }-*/;
  
      // no longs are allowed
      private native Double getValue2() /*-{ return this.value2; }-*/;
  
      // TODO migrate to Java 8
      public static final Function&lt;Dto, DomainObject&gt; dtoToDomainObject = new Function&lt;Dto, DomainObject&gt;() {
          @Override
          public DomainObject apply(Dto dto) {
              return new DomainObject(
                      dto.getValue1(),
                      (long) dto.getValue2()
              );
          }
      };
  
  }
  </code></pre>
  
  <p>As per the documentation, the outgoing types are just <code>String</code>, <code>boolean</code>, Java numeric primitive (with certain restrictions), <code>JavaScriptObject</code>, <code>Object</code>, and arrays. And this restriction does not seem to fit the nullable semantics, because of various runtime exceptions that prevent passing JavaScript "primitive" via a <code>Number</code> box or vice versa. This is somewhat strange, but I believe there were solid technical arguments for it. Anyway. So far after getting a lot of exceptions during my experiments, I've gained the following way:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Dto extends JavaScriptObject {
  
      protected Dto() {
      }
  
      private native boolean hasValue1() /*-{ return typeof(this.value1) != "undefined" &amp;&amp; this.value1 != null; }-*/;
  
      private native int getValue1() /*-{ return this.value1; }-*/;
  
      private native boolean hasValue2() /*-{ return typeof(this.value2) != "undefined" &amp;&amp; this.value2 != null; }-*/;
  
      // no longs are allowed
      private native double getValue2() /*-{ return this.value2; }-*/;
  
      // TODO migrate to Java 8
      public static final Function&lt;Dto, DomainObject&gt; dtoToDomainObject = new Function&lt;Dto, DomainObject&gt;() {
          @Override
          public DomainObject apply(Dto dto) {
              return new DomainObject(
                      dto.hasValue1() ? dto.getValue1() : null,
                      dto.hasValue2() ? (long) dto.getValue2() : null
              );
          }
      };
  
  }
  </code></pre>
  
  <p>The second implementation finally works as I expect letting me distinguish between real values and nulls. But this is a very huge boilerplate with at least the following disadvantages:</p>
  
  <ul>
  <li>satellite <code>has</code>-methods;</li>
  <li>the implementation of the satellite methods, at least two checks for nullability relying on JavaScript type checks (it can be extracted into a single JavaScript method, but that is not always easy), not typo-bulletproof;</li>
  <li>ternary operator while accessing the nullable methods, not typo-bulletproof allowing to compose an "alien" satellite method that belongs to another accessor;</li>
  <li>maybe something else that I don't see so far.</li>
  </ul>
  
  <p>I'm wondering: how can I use/implement complete nullable semantics for JSNI methods in a more elegant way without that huge boilerplate?</p>