Suppress compiler warnings on JAXB generated classes

我的未来我决定 提交于 2019-12-01 15:59:39
ToYonos

You have to use this plugin : jaxb2-annotate-plugin

2 solutions :

1 - Modify your xsd and add this kind of block

<xsd:element name="foobar" type="xsd:string">
    <xsd:annotation>
        <xsd:appinfo>
            <annox:annotate>@java.lang.SuppressWarnings("all")</annox:annotate>
        </xsd:appinfo>
    </xsd:annotation>
</xsd:element>

2 - Create a custom binding

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1">
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <annox:annotate target="class">@java.lang.SuppressWarnings("all")</annox:annotate>
    </jaxb:bindings>
</jaxb:bindings>

Disclaimer: I am the author of the jaxb2-annotate-plugin mentioned in @ToYonos answer.

This is not a standalone answer but rather an addition to @ToYonos answer.

I would like to address the following point from the question.

  1. Annox plugin from Atlassian's JAXB2 Basics framework/project (according to examples, seems to do the job but to add a whole framework and hundreds of kB of code just to add simple annotation to a bunch of classes? Really??),

I just need to add a few notes:

  • First of all, this is not "Atlassian's" framework/project. I am neither employee nor affiliated with Atlassian or Sun or Oracle (although I'm an official Sun/Oracle contributor). This is an indipendent open-source project not driven by any company.
  • Next, jaxb2-annotate-plugin was moved to a standalone project, it is not longer part of JAXB2 Basics. It just got too large and deserved to be a standalone thing.
  • Concerning "a whole framework and hundreds of kB of code".
    • jaxb2-annotate-plugin does not require or add ANY runtime dependencies to your code. If you annotations have runtime retention, you'll need the classes of your annotations in runtime, but it should be obvious.
    • jaxb2-annotate-plugin does indeed have a number of compile-time dependencies like which is used to parse annotations from Java syntax. Well, this is due to the fact the plugin has to do a lot of moderately advanced stuff like parse annotations from Java syntax and transform them to to add to the generated code. I personally don't think that a few hundred kBs in the compile time is a problem at all.

So, from my point of view, @ToYonos offered a reasonable workaround for the moment.


Now, how would a correct fix look like?

  • The correct fix would be to fix XJC. Everything else is just a workaround.
  • There is the JAXB-1053 issue for this problem. Iaroslav correctly points out that as long as 1.6 has to be supported, the diamond fix can't be applied.
  • The CodeModel will have to be updated as well to support Java 7+ constructs.
  • You could help by forking and providing a PR to XJC code on GitHub. However I could not say that PRs really do get applied. There are three of my PRs in the pipeline, for a month or so already.
  • It is possible to write an XJC plugin which would override code generation. This is not easy but possible.

The correct fix does not look like an easy task, I'd opt to a workaround and wait for the vendor fix at the moment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!