|
GettingStarted
Module ConfigurationApart of adding gwtexporter-x.x.x.jar to your project, you have to configure your gwt module in order to use it. The features marked with (*) in this page are only available if you use latest code (2.4.0-SNAPSHOT). You have to tell the gwt compiler whether you want to export your project. The export flag in the module file, is useful when sharing your module/library, marked with gwt-exporter annotations, with other gwt code which does not need anything to be exported. <module> ... <inherits name='org.timepedia.exporter.Exporter'/> <set-property name="export" value="yes"/> </module> Exportable typesYou have to be aware that only certain types and classes can be used as arguments or return types of methods and constructors:
InterfacesAll classes which you wanted to use in javascript must implement the Exportable interface, or must be exported via the ExportOverlay technique. Of course primitives and classes which extends the JavaScriptObject like Element, JsArray, etc can be used directly. Although you can return other gwt objects, those objects in javascript only can be used as a references because their gwt methods are not available. ExportableIt is the marker interface to export a class to javascript. Note that the class should be public. public class MyClass implements Exportable {
}ExportOverlayImplements a decoupled Exportable interface or class. In this example MyOtherClass will be exported. MyOtherClassdoes not need to implement Exportable. It is a technique to export code without modifying the original class nor adding gwt-exporter dependency to the original package. If you use a class instead of an interface, you should stub all methods you want to export. public class MyClass implements ExportOverlay<MyOtherClass> {
public static void method1() {}
public String method2 {return null;}
}
public class MyOtherClass {
public static void method1() {
}
public String method2() {
return "hello";
}
}
AnnotationsAre used to mark/unmark the classes and methods which should be exported, and to to rename the exported package, class, or method names. @ExportMarks a method, field, constructor, or class as exportable and allows its Javascript name to be overriden. When a class is marked, all public methods will be exported unless them are marked with !NoExport. It accepts two parameters: value used to define the final name of the method or class in javascript, and all(*) which says to the compiler that it has to export methods in parent classes. If you wanted a static method being available in the window object you can mark the method with a name starting with "$wnd."(*). // Exported all public methods in MyClass:
// mypackage.mclass.methodOne()
// mypackage.mclass.methodTwo()
// And exported all public methods in MyOtherClass:
// mypackage.mclass.methodThree()
@Export(value="mclass", all=true)
public class MyClass extends MyOtherClass implements Exportable {
public void methodOne() {
}
public void methodTwo() {
}
}
public class MyOtherClass implements Exportable {
public void methodThree() {
}
}
// Exported only one method
// mypackage.OtherClass.method()
public class OtherClass implements Exportable {
public void methodOne() {
}
@Export("method")
public void methodTwo() {
}
@Export("$wnd.myFunction");
public static void staticMethod(String parameter) {
}
}@NoExportIf a class is marked for export, use this method to selectively disable certain methods or fields from being exported. // method1, method4, method5 will be exported
@Export
public class MyClass implements Exportable {
public void method1() {
}
@NoExport
public void method2() {
}
public void method3() {
}
@Export
public void method4() {
}
public void method5() {
}
}@ExportPackageUsed to rename the package name. If you wanted the class available in the window object use an empty string (*). package mypackage;
// The exported method will be available as:
// p.c.m() instead of mypackage.MyClass.method()
@ExportPackage("p")
@Export("c");
public class MyClass implements Exportable {
@Export("m");
public void method() {
}
}@ExportClosureIndicates that an interface allows JS functions to be automatically promoted to its type when it appears as an argument to an exported function. For example: @Export
@ExportClosure
public interface JsClosure extends Exportable {
public void execute(String par1, String par2);
}
@Export("dpicker");
@ExportPackage("jsc")
public class DatePicker implements Exportable {
public executeJsClosure(JsClosure closure){
closure.execute("Hello", "Friend");
} var picker = new jsc.dpicker();
picker.executeJsClosure(function(a, b){
alert a + " " + b;
});You can export closures with more than one executable method(*), but you have to be aware of which method is being called each time to provide enough arguments to the javascript function. Also it is possible to export a class via ExportClosure if you combine it with the ExportOverlay interface (*): public class Function() {
public String method1(String a) {
return null;
}
public boolean method2(int a) {
return false;
}
}
@ExportOverlay
public interface MyFunction extends ExportOverlay<Function> {
String method1(String a);
boolean method2(int a);
}
@ExportPackage("js")
@Export(value="mclass")
public class MyClass extends MyOtherClass implements Exportable {
public void runMethodOne(Function f) {
String a = f.method1("hello");
}
public void runMethodTwo(Function f) {
boolean b = f.method2(4);
}
} var c = new js.mclass();
alert(c.runMethodOne(function(a) {
return "-> " + a;
});
alert(c.runMethodTwo(function(a) {
return a > 10 ? true : false;
});@ExportConstructor (*)Marks a static method which returns a class instance to be used as a constructor in javascript. It is useful to maintain just one instance of the class like is shown in the next example: @ExportPackage("gwt")
@Export("c")
public static class TestConstructors implements Exportable {
private static TestConstructors instance;
private String msg;
@ExportConstructor
public static TestConstructors constructor(String msg) {
if (instance == null) {
instance = new TestConstructors();
instance.msg = msg;
}
return instance;
}
// Constructor is private
private TestConstructors() {
}
public String echo() {
return msg;
}
}@ExportStaticMethod (*)Marks a static method in an ExportOverlay class to be exported as an static method of the original class. @ExportInstanceMethod (*)Marks a static method in an ExportOverlay class to be exported as an instance method of the original class. Because this method must be static, the first argument must be the instance of the original class. @ExportJsInitMethod (*)Marks an instance method in the Exportable class to return the JavascriptObject which will be used when wrapping the gwt object. The returned object will be extended with all the exported methods. @ExportAfterCreateMethod (*)Marks a static method in an Exportable or an ExportOverLay class so as it will run after the class has been exported. It is run just once and it is useful to redefine name-spaces, run jsni code, or whatever you needed to do after the class has been exported. Generator of Javascript API DocumentationGwtexporter comes with the class JsDoclet able to generate the javascript api documentation for your project. The class inspects your sources and documents exported methods and classes.
|
Great library!
I got a question here.
Can I export a class like this without changing the code, only by implementing ExportOverlay?<UrlParameters> ?
public class UrlParameters? {
.... }
Thank you very much and looking forward to hearing from you soon.
Please ask questions in the mailing list since we do not read usually this page.
Thanks - Manolo
To make this guide working I had to run ExporterUtil?.exportAll(); during module load, this is not visible instantly, maybe should be mentioned somewhere
Some tips after having problems using this: Indeed, ExporterUtil?.exportAll() should be called in onModuleLoad(), together with specifying a default constructor on each type you want to export, an @Export attribute on the default constructor and on the class itself. And if you're getting "Unresolvable native reference to method 'wrap'" errors, you need to check your class doesn't return arrays of custom types.
edit "unless them are marked with !NoExport" to "unless they are marked with @NoExport". use * (^*^) instead of (*). (*) is really confusing.