Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expose class functionality in GWT
    text
    copied!<p>I have a class library written in Java and want to convert it to Javascript. All methods are pretty simple and mostly have to do with manipulating collections. I have this one class, GameControl, which I could instantiate and I want its methods exposed to other Javascript code on the page.</p> <p>I thought to use GWT. I have a running project in GWT which compiles, but I can't figure out how to expose my instance (+functionality) of the GameControl class. </p> <p>I thought using JSNI to expose my object should work, but it didn't. This is the short version of how it look like right now:</p> <p><strong>GameEntryPoint.java</strong></p> <pre><code>import com.google.gwt.core.client.EntryPoint; public class GameEntryPoint implements EntryPoint { private GameControl _gameControl; @Override public void onModuleLoad() { _gameControl = new GameControl(); expose(); } public native void expose()/*-{ $wnd.game = this.@game.client.GameEntryPoint::_gameControl; }-*/; } </code></pre> <p><strong>GameControl.java</strong></p> <pre><code>package game.client; public class GameControl { public boolean isEmpty(int id){ // does stuff... return true; } } </code></pre> <p>So, GWT indeed compiles the code, and I see that there is a <code>GameControl_0</code> object being built and set into <code>$wnd.game</code>, but no <code>isEmpty()</code> method to be found.</p> <p>My expected end result is to have a <code>window.game</code> as an instance of <code>GameControl</code> with all public methods <code>GameControl</code> exposes.</p> <p>How can I do this?</p> <p><strong>Edit</strong> As per <code>@jusio</code>'s reply, using JSNI to expose <code>window</code> properties explicitly worked, but it was too verbose. I'm trying the gwt-exporter solution. Now I have</p> <p><strong>GameEntryPoint.java</strong></p> <pre><code>package game.client; import org.timepedia.exporter.client.ExporterUtil; import com.google.gwt.core.client.EntryPoint; public class GameEntryPoint implements EntryPoint { @Override public void onModuleLoad() { ExporterUtil.exportAll(); } } </code></pre> <p><strong>RoadServer.java</strong></p> <pre><code>package game.client; import org.timepedia.exporter.client.Export; import org.timepedia.exporter.client.ExportPackage; import org.timepedia.exporter.client.Exportable; @ExportPackage("game") @Export("RoadServer") public class RoadServer implements Exportable { int _index; int _id; public RoadServer(int index,int id){ this._id=id; this._index=index; } } </code></pre> <p>but still none of the code is exported (specifically not <code>RoadServer</code>).</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