Cutting down GWT's verbosity with gwt-mpv-apt

I am strong believer in GWT’s potential as a client-side web development solution. Its value proposition is quite exciting: write all your web code in Java, using tools of your choice, and the GWT compiler will output highly tuned, browser-compatible JavaScript. I use it whenever possible and never look back.

Still, there are some times when I feel that GWT’s programming style is too verbose ( see Reducing GWT custom widget verbosity ) to be really productive. I obviously save a lot of time, compared to what I would need to write a comparable interface using “plain” Javascript. But that does not mean that I’m happy to spend time writing code which in “real” Java would be much simplified using reflection. As there is no reflection ( java.lang.reflect ) support in GWT, I was stuck writing verbose code for event handling and DTOs.

That’s why I was thrilled to discover the gwt-mpv-apt project, which is a Java 6 annotation processor to help generate some of the boilerplate code involved in GWT projects. The setup is rather simple:

To get a sense of what this can do for you, I’ll take the example of a GwtEvent which signals that a group of people arrived. The PeopleArrivedEvent should contain the count of people which arrived in this group, and their origin. In the screenshot below, the top editor shows what I wrote, and the bottom editor shows what was generated.  I collapsed the methods to make them all fit into one screen, but they are implemented.

gwt-mpv-apt has generated all the boilerplate code for us, and for an application which makes heavy use of events, this is a major time-saver.

Before going further, I must point out that this is based on an accepted Java standard - the Pluggable Annotation Processing API (JSR 269) - which is in my opinion one of the hidden gems in the Java Development Kit. The IDE support comes in for free, and as far as I know both Eclipse and NetBeans have good annotation processing support. In Eclipse, the annotation processor kicks in as soon as you save the file you’re working on and incrementally changes only the needed files. The integration with javac is also seamless - drop in the jar and it works.

Another good feature of this library is its error reporting: error markers are generated in the Eclipse editor just as they are reported to the compiler. This screenshot is an example of a wrongly named class, where the error marker is correctly placed on the class name:

The annotation processor currently has support for generating DTOs for gwt-dispatch and gwt-platform, and the savings are even more impressive. The left editor shows the code I wrote, while the right editors show the code which was generated. Even with folding enabled, I still can’t capture all the code.

I have shown how gwt-mpv-apt can help you reduce the amount of boilerplate code you write when working with GWT.  I encourage you to take it for a spin and let us know what you think about it. The project is still young, and it welcomes any contributions: feedback after usage, bug reports, testing, code contributions and spreading word are all appreciated. You can find more information at the GitHub project page, the GitHub issue tracker and the project mailing list.