Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java enums can't be handled by JSNI, but you can pass enum strings to the JSNI layer, as well as convert strings returned from JSNI methods back to enums. </p> <p>You'd also be better off representing the <code>Giant</code> JavaScript object separately, and delegate to the JSNI layer from the <code>Giant</code> Java type. That way you can maintain separation of concerns and loosely couple the implementation and the abstraction:</p> <pre class="lang-java prettyprint-override"><code>public class GiantJso extends JavaScriptObject { protected GiantJso() { } public static final native GiantJso create(String wordA, String wordB, String wordC) /*-{ return { vocabulary : [ wordA, wordB, wordC ], said : '' }; }-*/; public final native void sayGiantWord(String word) /*-{ if (this.vocabulary.indexOf(word) != -1) { console.log("i'm a giant, here me roar: ", word, "!"); this.said = word; } }-*/; public final native JsArrayString vocabulary() /*-{ return this.vocabulary || null; }-*/; public final native String said() /*-{ return this.said; }-*/; } </code></pre> <p>Then wrap that in the Java type:</p> <pre class="lang-java prettyprint-override"><code>public class Giant { private GiantJso jso; public Giant() { jso = GiantJso.create(GiantWord.FEE.name(), GiantWord.FEE.name(), GiantWord.FEE.name()); } public void sayGiantWord(GiantWord word) { jso.sayGiantWord(word.name()); } public GiantWord getSaidWord() { return GiantWord.valueOf(jso.said()); } } </code></pre> <p><code>vocabulary</code> and <code>said</code> are just examples of additional properties that can be assigned to the JSO, you're free to implement any structure you wish, of course.</p> <h3>Further reading</h3> <ul> <li><em><a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.html" rel="nofollow">JSNI overlay types</a></em> on the GWT docs dev guide.</li> </ul> <p><em><strong>Note:</strong> If what you're going for is representing Java classes as JavaScript constructs, check out <a href="http://code.google.com/p/gwt-exporter/" rel="nofollow">GWT exporter</a>.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload