Code Generation and JDK5 - xdoclet, sgen, generics, and annotions

Published: 11:04 PM GMT+12, Sunday, 10 April 2005 under: technology

At the start of the week discussion at office centered around a desire to make the move to JDK5 for all our apps, after we'd all agreed and convinced the sysadmins "change is good", one of the devs started to experiement with adding generics - blamo - xdoclet explodes on the parsing the source - doh. Jean's now off looking at xdoclet2 code from CVS whilst I took a quick google of the net...

Some time ago I recall reading about Cedric's new SGen code generator which looked promising (even if there were no standard plugins for struts, hibernate, ejb etc. etc. yet ). After downloading it and getting a basic generator working with Javadoc, I discovered it wouldn't run under Tiger either - what documentation there was indicated a tiger specific compile, which unfortunately wasn't in the distribution :(

So I shoot off a short email to Cedric asking about this, or about a newer release of SGen, the response wasn't exactly what I wanted thou - when Cedric left BEA, SGen stayed behind and is (at least to my perception) a dead project. Back to square one.

As I was saying, whilst a coworker was checking out xdoclet2 I googled, and came accross XJD-41 in XDoclet's bug database, after reading through all the comments, I was finally pointed to the attached jars, or the source location of http://www.softmotions.com/xjavadoc/ which contained a "patched" version of xjavadoc to support the JDK5 generics and annotations markup. Replacing my existing xjavadoc.jar with the patched version worked like a charm - with one exception...

Patching xjavadoc only makes xdoclet run with generics and annotations, as none of the xdoclet plugins/templates are changed, generated code doesn't appear to include references to generic types. i.e. a remote EJB method returning List gets generated as only return List. We can probably live with this for now thou.

At the same time I started to look into the new annotation processor in JDK5, and within about an hour of googling, reading a few blog posts, I have a nice annotation based generator for Spring bean definitions:

@SpringBean("mainBean")
public class Main {
    private String name;
    private String company;

    @SpringInject("productName")
    public void setName(String name) {
        this.name = name;
    }

    @SpringInject(ref="companyName")
    public void setCompany(String company) {
        this.company = company;
    }  
}

which after running the command:

[amrk@spawn apttest]$ apt -cp classes:lib/jdom.jar -s gen \
  -factory test.AptTestFactory src/test/Main.java

gives me:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="mainBean" class="test.Main">
    <property name="company">
      <ref bean="companyName" />
    </property>
    <property name="name">
      <value>productName</value>
    </property>
  </bean>
</beans>

APT is starting to look incredibly powerfull, as well as being easy to use, the only downside so far is that everything seems to be under the com.sun.mirror.* package tree, which means it's potentially likely to change.

Comments (2)

Hey-

Sadly, xdoclet and xdoclet2 are hopelessly behind the times (neither can read or generate Java 5).

JSR-269 will standardize a lot of the APT stuff. The downside is that any code written to the com.sun.mirror API will be deprecated with the next version of Java.

APT-Jelly looks promising as a simpler way to use the APT.

Thanks for tracking this information publicly - I wasted a lot of time trying to work with XDoclet 1 and 2 (who would believe they don't support java 5?!?)

--Adam

left by Adam Brod . Saturday, 15 April 2006 7:17 AM

Hi, I'm actually trying to generate Spring XML descriptors using annotations and APT. Would you mind posting the generator you've come up with. That would save me quite some time ;)

PS: Sorry for the weird email, it's to avoid spam.

left by Cédric Vidal . Wednesday, 29 March 2006 4:04 AM
Add Comment