我有一个用 Java 编写的类库,并希望将其转换为 Javascript。所有方法都非常简单,主要与操作集合有关。我有一个类 GameControl,我可以对其进行实例化,并且我希望它的方法暴露给页面上的其他 Javascript 代码。
我想使用GWT。我在 GWT 中有一个正在运行的项目,它可以编译,但我不知道如何公开我的 GameControl 类的实例(+功能)。
我认为使用 JSNI 公开我的对象应该可以工作,但它没有。这是它现在的样子的简短版本:
游戏入口点.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;
}-*/;
}
游戏控制.java
package game.client;
public class GameControl {
public boolean isEmpty(int id){
// does stuff...
return true;
}
}
所以,GWT 确实编译了代码,我看到有一个GameControl_0
对象正在构建并设置到$wnd.game
中,但没有isEmpty()
找到方法。
我预期的最终结果是window.game
作为GameControl
所有公共方法GameControl
公开的实例。
我怎样才能做到这一点?
编辑
根据@jusio
的回复,使用 JSNI 显式公开window
属性有效,但它太冗长了。我正在尝试 gwt-exporter 解决方案。我现在有
游戏入口点.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;
}
}
但仍然没有导出任何代码(特别是没有RoadServer
)。