简体   繁体   English

修改java类以在编译时包含特定注释

[英]Modify java classes to include specific annotations at compile time

I have many classes generated by JAXB's xsd2java. 我有许多由JAXB的xsd2java生成的类。 I need all these classes to be annotated with specific annotations at compile time (for example with lombok annotations). 我需要在编译时使用特定注释对所有这些类进行注释(例如使用lombok注释)。 Is there any way to do this, with some code generation tool for example? 有没有办法做到这一点,例如一些代码生成工具?

Disclaimer: I am the author of JAXB2 Annotate Plugin which allows you adding arbitrary annotations to the schema-derived classes. 免责声明:我是JAXB2 Annotate Plugin的作者,它允许您向模式派生类添加任意注释。

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. 注释以Java语法提供,但您必须使用完全限定名称。
  • You have to include annotations classes (lombok, for instance) into the XJC classpath as these classes must be available during the schema compilation time. 您必须在XJC类路径中包含注释类(例如lombok),因为这些类在模式编译期间必须可用。

ps. PS。 I assume that when you said xsd2java you probably meant XJC . 我假设当你说xsd2java你可能意味着XJC

Update 更新

The OP asked about in comments how to configure it with the . OP在评论中询问如何使用配置它。

You can use jaxb2-annotate-plugin with as well. 您也可以将jaxb2-annotate-plugin I just have never tried it. 我从来没有尝试过。

  • You can include additional JARs into classpath by using the dependencies/depenency in pom.xml . 您可以使用pom.xmldependencies/depenency将其他JAR包含到类路径中。
  • Then you'll need to add arguments into the configuration. 然后,您需要在配置中添加arguments

See this answer (and question) for examples: 有关示例,请参阅此答案(和问题):

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

It is about other plugins but you'll get a clue on how to configure it with Codehaus . 它是关于其他插件,但你会得到一个如何使用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. 这是 / 组合的示例pom.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM