Autogenerated Java Beans, anyone?

What's the best way to generate Java-Bean source code? Castor? JAXB? Annotations? XSLT?

The problem ...

I don't really like creating all those setters and getters that fire property changes whenever something changes.

I know this is a must for many situations, mainly if you want your Java Beans to be conformant with the Java Beans specification.

Or if you want to use them to represent the Domain Object Model (DOM) of your applications, and want to use for that.

But I hate creating a PropertyChangeSupport and then firing PropertyChangeEvents from there.

This is, I hate writing something like:

 * Sets this album's title and notifies observers 
 * if the title changed.
 * @param title   The title to set.
public void setTitle(String title) {
    Object oldValue = getTitle();
    this.title = title;
    firePropertyChange(PROPERTYNAME_TITLE, oldValue, title);

I know many tools (including NetBeans) make this easy for you, but still being so, I prefer something easier.

In many situations, mainly when I have complex DOMs, I prefer describing the DOM in a file and then generating all that stuff for me automagically. If a part of the DOM contains 5 properties then it's probably good enough to use NetBeans for that (one by one, because the "Refactor/Encapsulate fields" feature does not generate bound properties), but what about if you have several dozen properties?

(You may have noticed that I was satisfied with NetBeans a while back but it's time for a change now)

And if you use a binding framework, such as you, of course, need to create a DOM, a DOM that fires property changes!

... and some solutions ...

So the idea is to generate a file that is then passed through a tool to generate all that Java code for me. This gives me some flexibility and ease of mainteinance: if I change the DOM then I just have to update my file, and all the DOM Java Code is automagically generated.


I could use Python, but, wait: I don't know Python yet. Mainteinance is gonna be a nightmare. No way.


One first approach is to use an XSLT stylesheet to generate all the Java code. That is probably simple enough for me, and requires little-to-none extra libraries. NetBeans is able to run an XSLT transformation within the IDE, so a small IDE support may be included with this solution.

I would have to further investigate if the XSLT is able to generate different output files. I know this is possible (DocBook stylesheets do it, for instance), but I'll have to investigate.


Castor has a code generator that is able to build bound properties too. The drawbacks of this solutions are that some extra libraries may be needed. Advantages are that the DOM may be transformed in a set of XML documents. And that's good for quick and dirty persistence. Another drawback is that I don't know Castor very well (and, well, this may be a benefit, after all)


JAXB doesn't generate bound properties. I have done a small plug-in for JAXB that creates bound properties, though. This is an interesting solution, because JAXB reads stuff from XML Schema files, and XML Schema (though difficult to use) is probably a good way to define models. With XML Schema you can define constraints on data, and that's something I find useful. Furthermore, with JAXB I can also persist stuff nicely to XML.

Drawbacks are that some extra-libraries are needed, and that JAXB plugin development is still stuck at P-mode, this is, there's little documentation on how to write a JAXB plugin.


Another option would be to use annotations. Something similar to these at the Tapestry project or these xdoclet tags. Say a @JavaBean, @Property(Vetoable) or something like that.

The advantages of this solution is that the initial file is probably a Java interface defining the JavaBean, with a list of fields each of one with its corresponding annotation.

The drawbacks are that building annotation processors (using the APT tool) seems also to be stuck in "P" mode because there's little documentation yet.

NetBeans module?

Finally I could build a NetBeans module. A module that let's me choose properties and property types (name:String, date:Date, and so forth) and that then generates the JavaBean for me.

The drawback is that I won't be able to use that feature

Food for thought

So I'm still deciding which solution I'll go after. Any ideas, suggestions or experience would be greatly appreciated!

Cheers, Antonio

blog comments powered by Disqus