How to expose class functionality in GWT

code-generation, gwt, javascript

Solution

You have exposed only instance of the `GameControl`. If you want to expose other methods, you'll have to expose them as well. For example:

 public native void expose()/*-{
        var control = this.@game.client.GameEntryPoint::_gameControl;   
        var gameInstance = {
            gameControl: control,
            isEmpty:function(param){
              control.@game.client.GameEntryPoint::isEmpty(*)(param);   
            }  

        }


        $wnd.game = gameInstance;
    }-*/;

Also there is a framework called gwt-exporter, it might make things easier for you

Problem

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. 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. 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: GameEntryPoint.java ``` 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; }-*/; } ``` GameControl.java ``` package game.client; public class GameControl { public boolean isEmpty(int id){ // does stuff... return true; } } ``` So, GWT indeed compiles the code, and I see that there is a `GameControl_0` object being built and set into `$wnd.game`, but no `isEmpty()` method to be found. My expected end result is to have a `window.game` as an instance of `GameControl` with all public methods `GameControl` exposes. How can I do this? Edit As per `@jusio`'s reply, using JSNI to expose `window` properties explicitly worked, but it was too verbose. I'm trying the gwt-exporter solution. Now I have GameEntryPoint.java ``` 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(); } } ``` RoadServer.java ``` 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; } } ``` but still none of the code is exported (specifically not `RoadServer`).

Original source