I’ve been working somewhat intensively with XDoclet over the past week, and, as it has proven to be quite a valuable tool, I thought I’d share my experience.
The specific reason for my foray into this particular technology was to automate the creation of JMX MBean descriptor metadata for Commons Modeler. Commons Modeler consumes an XML file describing the exposed operations and attributes of managed components. Having to maintain this XML document separately from the code is very burdensome and, of course, and easily lead to disconnects between the actual code and what is described within the XML doc.
Therefore, as a means to resolve this issue, I decided to look into XDoclet. I’d briefly investigated it in the past when doing some EJB development, but did not have time to pursue it further. So, this was an educational exercise that had long been overdue. XDoclet enables Attribute-Oriented Programming. Basically this means leveraging metadata within existing source files to automatically generate code. This metadata can either be the actual characteristics of Java classes and their intrinsic methods, parameters and attributes, or annotations in the form of JavaDoc tags. The beauty of it is that it allows you to specify and extract metadata from within the code, rather than having to be separately maintained.
XDoclet contains two core components. First, an optimized Java source parser, capable of exposing JavaDoc tags and class metadata through an object model. Second, a template engine, similar in ways to other popular Java-based template technology. XDoclet uses template (.xdt) files while iterating over Java source files in order to generate new content – in my case, the mbeans-descriptors.xml
Overall I found using XDoclet a positive experience. There’s an immediate ROI in the form of saved time and reduced scope for error just from not having to separately maintain metadata from the actual code. Creating a custom template was pretty easy, and even creating a custom tag handler was a relatively intuitive process, especially if you have previously written JSP custom tags. That said, I have to give thanks to the authors at Vecna for the Customizing XDoclet page, which provided a great jumpstart on the process.
If you’re looking for ways to exploit the metadata richness available from your Java application code – whether for automatically generating descriptors for JMX or EJB deployment, creating Struts configuration files, or producing other types of content – you should spend some time checking out XDoclet. They’re actually in the midst of working on version 2 (XDoclet v2 Wiki), which now uses QDox as the Java source parser engine and supports both Jelly and Velocity as templating engines.