users@jaxb.java.net

Re: Adding functionality to JAXB-generated classes

From: Benoit Callebaut <benoit.callebaut_at_euphonynet.be>
Date: Mon, 21 Jan 2008 21:03:32 +0100

Yes,
you can use JAXB plugins to inject code in the generated classes.
a plugin can manipulate the code model of the generated classes and add
functionalities to them.
The inject sample plugin does something like that but very limited way.
It is up to you to expand it.
I am currently writing a plugin to add the visitor pattern to the
generated classes.
Take a look on the jaxb-commons project? You will find sourcecode of
some plugins

Carlos Alegría a écrit :

> Hi all,
>
> I'm trying to add some functionality to JAXB-generated classes in
> order to integrate them with my project's classes and avoid having two
> classes that refeer to the same thing (the ones that contains the
> operations, and the ones - JAXB generated - that manage the state).
> By reading JAXB documentation and (this) user mail list I found that
> there are three approaches to accomplish this:
>
> - Customize schema compilation to tell ObjectFactory to create a
> subclass of the JAXB generated classes when unmarshalling
> - Customize schema compilation to generate JAXB classes that inherit
> from a class that contains extra functionality
> - Create the classes and add JAXB annotations manually (or use
> something like JAXB Introductions*)
>
> I read that the first one is not a good idea, as the original purpose
> of the attribute "implClass" of the class customization element is to
> be able to add portability between different JAXB implementations. For
> me, the third one is wasting a lot of the benefits of using JAXB, as
> you'd have to modify by hand your classes when the schema changes.
>
> I heard that JAXB 2.0 will provide a better mechanism to solve the
> problem of adding functionality to generated classes, but I have found
> no documentation about this (even in the JAXB specification document).
>
> Does anyone knows if there is a way to do this on JAXB 2.*?
>
> Best regards.
> Carlos Alegria
>
> * JAXB Introductions page:
> http://wiki.jboss.org/wiki/Wiki.jsp?page=JAXBIntroductions
> <http://wiki.jboss.org/wiki/Wiki.jsp?page=JAXBIntroductions>