Note that there are some explanatory texts on larger screens.

plurals
  1. POCan JSNI Marshal Enums as strings?
    primarykey
    data
    text
    <p>I'm trying to use GWT's JSNI to call a Java function through native code. The Java function has an enum in it, and I was curious to know if the enum will marshall in the way I want. I couldn't find anything useful on Google or SO, and <a href="https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI" rel="nofollow">the Google basic docs</a> are not very specific. I'm sure I'll find out as I compile and run, but thought I might as well ask.</p> <p>Given vastly simplified code like this:</p> <pre class="lang-java prettyprint-override"><code>package my.example.package; public class Giant { public enum GiantWord { FEE, FIE, FO, FUM }; public void sayGiantWord(GiantWord word) { /* ... */ } public native JavaScriptObject toJS() /*-{ var Giant = function() { this.sayGiantWord = function(word) { this.@my.example.package::sayGiantWord(Lmy/example/package/Giant$GiantWord;)(word); }; }; return new Giant(); }-*/; } </code></pre> <p><strong>EDIT</strong> - Based on comments, let me give an alternative version of the toJS function, and avoid confusion between <code>this</code> and <code>this.</code></p> <pre class="lang-java prettyprint-override"><code> public static native JavaScriptObject toJS(final Giant g) /*-{ var Giant = function() { this.sayGiantWord = function(word) { g.@my.example.package::sayGiantWord(Lmy/example/package/Giant$GiantWord;)(word); }; }; return new Giant(); }-*/; </code></pre> <p>will calling <code>sayGiantWord("FEE")</code> from within JavaScript (on the appropriately acquired var from <code>toJS()</code>) work correctly? In other words, will the JSNI marshaller properly convert a String to its matching Java enum?</p> <p>I expect calling <code>sayGiantWord(1)</code> will be more prone to marshall correctly, since an int can be converted to an enum easily.</p> <p>Other notes:</p> <ul> <li>The GWT Eclipse plugin is what gave me the syntax for accessing the class member's enum. At least that far, GWT is working with me.</li> <li>I don't want to pass a number, and if necessary I know I can handle the string with a conversion function in the Java class as follows; I'd just rather not do so.</li> </ul> <pre class="lang-java prettyprint-override"><code>public void sayGiantWordJS(String word) { // convert the string to an enum // call sayGiantWord } </code></pre> <p>Thanks for any advice!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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