users@jaxb.java.net

Re: generating Annotated classes with JAXB ?

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Wed, 28 Nov 2007 11:55:11 +0100

Hi.

> How can I generate annotated classes with JAXB ?
>
> I want to generate a class like that:
>
> @Deprecated
> public void MyClass {}
>
> @Deprecated is my primary goal, but I am also interested in figure out
> how to add any annotation to classes ? How to define it in the Schem
> or using customization...
>
> The reason I am looking for that: I am using JAXB as configuration
> framework of my application, and some of the configuration values
> should not be used anymore. In order to keep the compatibility with
> old configuration files, I don't want to rid off the declaration of
> the obsolete fields, but rather annotate it as deprecated.

Use the annotate plugin.

Here's an example:

<xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        jaxb:version="2.1"
        xmlns:annox="http://annox.dev.java.net"
        jaxb:extensionBindingPrefixes="annox"
        xmlns:jl="http://annox.dev.java.net/java.lang">

        <xsd:element name="foo" type="FooType"/>
        <xsd:complexType name="FooType">
                <xsd:annotation>
                        <xsd:appinfo>
                                <annox:annotate>
                                        <jl:SuppressWarnings/>
                                </annox:annotate>
                        </xsd:appinfo>
                </xsd:annotation>
                <xsd:sequence>
                        <xsd:element name="bar" type="xsd:string"/>
                        <xsd:element name="foobar" type="xsd:string">
                                <xsd:annotation>
                                        <xsd:appinfo>
                                                <annox:annotate>
                                                        <jl:Deprecated/>
                                                </annox:annotate>
                                        </xsd:appinfo>
                                </xsd:annotation>
                        </xsd:element>
                </xsd:sequence>
        </xsd:complexType>
</xsd:schema>

Note the jl prefix mapped to the java.lang package. Element names are
then annotation class names, attributes - fields and so on.

The result:

package generated;

...
@SuppressWarnings({

})
public class FooType {

...

    @Deprecated
    public String getFoobar() {
        return foobar;
    }
...
}

I've attached the pom.xml. The example will be available under
jaxb2-commons/tests/annotate in half an hour.

Bye.
/lexi