Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 277
alex Avatar asked Nov 07 '14 08:11

alex


People also ask

Do extended classes inherit annotations?

Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it. The annotation can be overridden in case the child class has the annotation.

Can classes be annotated in Java?

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line. As of the Java SE 8 release, annotations can also be applied to the use of types.

Can we extend annotation?

In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.


1 Answers

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 jaxb2-maven-plugin.

You can use jaxb2-annotate-plugin with jaxb2-maven-plugin 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 jaxb2-maven-plugin.

Configuration with my maven-jaxb2-plugin 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 maven-jaxb2-plugin/jaxb2-annotate-plugin combo.

like image 119
lexicore Avatar answered Sep 24 '22 23:09

lexicore