Skip to Content »

Tech Life of Recht » XDoclet module for GWT

 XDoclet module for GWT

  • August 5th, 2006
  • 3:33 pm

Having used Google Web Toolkit to build an application, I found out that I had to write a good portion of code, which might as well be generated automatically. I then decided to mess around with XDoclet a little to see if it was possible to make a new module. It was, and now you can use it too.

Basically, it can generate service interfaces, async services interface, module definitions (.xml.gwt), and a basic web.xml file. Finally, it can create a service factory for creating new Async-objects.

Service interfaces
When creating a remote service, just annotate the class like this:

CODE:
  1. package dk.contix.gwt.service;
  2.  
  3. /**
  4. * @gwt.interface service="dk.contix.gwt.shared.RemoteService" path="/remoteService"
  5. */
  6. public class RemoteServiceImpl implements RemoteService {
  7.  
  8.   /**
  9.    * @gwt.serviceMethod
  10.    */
  11.   public String getStuff(String what) {
  12.      // code here
  13.   }
  14. }

This will create the interface dk.contix.gwt.shared.RemoteService containing all methods which are annotated with @gwt.serviceMethod. It will also generate the async interface in dk.contix.gwt.shared.RemoteServiceAsync

Modules
To generate module definitions (.gwt.xml files), annotate the EntryPoint classes, like this:

CODE:
  1. package dk.contix.gwt.client;
  2.  
  3. /**
  4. * @gwt.module package="dk.contix.gwt" include="client,shared"
  5. */
  6. public class Client implements EntryPint {
  7.   // code here
  8. }

This will generate a file in dk/contix/gwt calles Client.gwt.xml. The file will contain the entry-point declaration, all remote servlets, and additional source paths (those specified in the include parameter).

ServiceFactory
If a service factory is generated, a class with static methods for creating instances of the Async interfaces will be created. The factory class will automatically detect url prefixes (if the application is deployed in another context than ROOT).

Generating the files
Create a build.xml file for Ant, if you don't already have one. Include the following target:

CODE:
  1. <path id="classpath">
  2.   <pathelement location="src"/>
  3.   <pathelement location="${build.dir}/classes"/>
  4.         <fileset dir="lib">
  5.             <include name="**/*.jar"/>
  6.         </fileset>
  7. </path>
  8.  
  9. <target name="generate">
  10.   <taskdef resource="xdoclet/modules/gwt/doclet.xml" classpathref="classpath"/>
  11.  
  12.   <gwtdoclet destdir="src">
  13.     <fileset dir="src">
  14.       <include name="**/*.java"/>
  15.     </fileset>
  16.    
  17.     <interface/>
  18.     <async/>
  19.     <module/>
  20.     <webxml output="web.xml"/>
  21.     <servicefactory class="dk.contix.gwt.client.ServiceFactory"/>
  22.   </gwtdoclet>
  23. </target>

Finallt, place xdoclet-gwt-module-0.1.jar in lib/ so that it becomes a part of the classpath. xdoclet-*.jar and xjavadoc-*.jar is also required. They can be downloaded from xdoclet.sourceforge.net.

Bootstrapping
When a new remote service is created, there's no interface to implement. I work around this simply by annotating the service implementation, generating the code, and then adding the implements declaration.

Want your say?

* Required fields. Your e-mail address will not be published on this site

You can use the following XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>