Search code examples
javajaxbxjclombokjaxb2-annotate-plugin

Modify java classes to include specific annotations at compile time


I have many classes generated by JAXB's xsd2java. I need all these classes to be annotated with specific annotations at compile time (for example with lombok annotations). Is there any way to do this, with some code generation tool for example?


Solution

  • Disclaimer: I am the author of JAXB2 Annotate Plugin which allows you adding arbitrary annotations to the schema-derived classes.

    Short example:

    <xsd:complexType name="FooType">
        <xsd:annotation>
            <xsd:appinfo>
                <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
                <annox:annotate target="package">@javax.annotation.Generated({"XJC","JAXB2 Annotate Plugin"})</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>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
                        <annox:annotate target="setter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="setter-parameter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="getter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="field">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="class">@java.lang.Deprecated</annox:annotate>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    

    Works in external binding files as well.

    Limitations:

    • Annotations are provided in Java syntax, but you have to use fully qualified names.
    • You have to include annotations classes (lombok, for instance) into the XJC classpath as these classes must be available during the schema compilation time.

    ps. I assume that when you said xsd2java you probably meant XJC.

    Update

    The OP asked about in comments how to configure it with the .

    You can use jaxb2-annotate-plugin with as well. I just have never tried it.

    • You can include additional JARs into classpath by using the dependencies/depenency in pom.xml.
    • Then you'll need to add arguments into the configuration.

    See this answer (and question) for examples:

    https://stackoverflow.com/a/12804497/303810

    It is about other plugins but you'll get a clue on how to configure it with Codehaus .

    Configuration with my would be as follows:

    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <configuration>
            <extension>true</extension>
            <args>
                <arg>-Xannotate</arg>
            </args>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
                </plugin>
            </plugins>
        </configuration>
    </plugin>
    

    This part:

    <plugin>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
    </plugin>
    

    refers to the artifact containing the annotation classes.

    Here's a sample pom.xml for / combo.