问题
I want to generate Javascript code from an existing Java project (original question here).
I use GWT in conjunction with gwt-exporter. After I GWT compile, none of my types appear in any of the generated code.
I have
GameEntryPoint.java
package game.client;
import org.timepedia.exporter.client.ExporterUtil;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
public class GameEntryPoint implements EntryPoint {
@Override
public void onModuleLoad() {
ExporterUtil.exportAll();
}
}
RoadServer.java
package game.client;
import org.timepedia.exporter.client.Exportable;
public class RoadServer implements Exportable {
int _index;
int _id;
public RoadServer(int index,int id){
this._id=id;
this._index=index;
}
}
There is no reference to RoadServer anywhere in my war/ directory (grep -Ri RoadServer war/* matches only the .class files). Also, just to be sure, I opened the war/GameEntryPoint.html file in my browser (Firefox) and Firebug only sees game_GameEntryPoint as something that starts with game.
Any idea why this is not working?
P.S. I also tried specifying @Export and @Export("RoadServer") with @ExportPackage("game"). Nothing works.
回答1:
Aha, so looks like it is some bug in ExporterUtil.exportAll(). If class have non-empty constructor or doesn't have any methods it is not exported. But if you add an empty constructor to the exported class, and annotate non-empty constructor with @Export annotation it starts working
Also you can manually export a class:
public class GameEntryPoint implements EntryPoint {
@Override
public void onModuleLoad() {
ExporterUtil.exportAll();
GWT.create(RoadServer.class); //forcing to export road server
}
}
But be aware that this way can sometimes export a class with errors
来源:https://stackoverflow.com/questions/10057844/gwt-exporter-doesnt-generate-code-java-to-javascript