tweej
tweej

Reputation: 842

XJC Generated Java Classes Missing Import Statement When Using Binding Declaration

I am trying to use XJC to generate Java classes from a XML Schema and a bindings file. The bindings file is intended to generate a member of type InetAddress instead of String in the generated class.

I have the following:

config.xsd

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="config">
    <xsd:complexType>
        <xsd:all>
            <xsd:element name="ip" type="IPv4Address" />
        </xsd:all>
    </xsd:complexType>
</xsd:element>

<xsd:simpleType name="IPv4Address">
    <xsd:annotation>
        <xsd:documentation>
            IPv4 address in the dotted-decimal notation.
        </xsd:documentation>
    </xsd:annotation>

    <xsd:restriction base="xsd:string">
        <xsd:pattern value="((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"/>
    </xsd:restriction>
</xsd:simpleType>

configBindings.xml

<?xml version="1.0"?>
<jxb:bindings version="2.0"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jxb:bindings schemaLocation="config.xsd" node="/xsd:schema" >
    <jxb:globalBindings>
        <jxb:javaType name="InetAddress" xmlType="IPv4Address" parseMethod="InetAddress.getByName" printMethod="getHostAddress" />
    </jxb:globalBindings>
</jxb:bindings>

Then, I run XJC with xjc -b configBindings.xml config.xsd which generates java code in the directory "generated". However, while the type of member ip has been correctly bound to InetAddress, the file Config.java is missing an import statement for java.net.InetAddress. So when I try to compile Config.java or the adapter Adapter1.java I get several errors of this sort:

$ javac generated/Adapter1.java 
generated/Adapter1.java:14: error: cannot find symbol
    extends XmlAdapter<String, InetAddress>
                               ^
  symbol: class InetAddress

Am I required to manually insert the needed import statements after running XJC? Is there no way to get XJC to do this for me automatically, even if I were to specify the needed packages in the binding file somehow?

NOTE: I actually want to use my own adapter class to do this so that I can deal with the InetAddress exceptions properly, but this example makes the question a little easier to ask even if the generated code won't compile after manually adding the import statement because UnknownHostException is not dealt with.

Upvotes: 1

Views: 1815

Answers (1)

lexicore
lexicore

Reputation: 43651

Just use the fully qualified class name. You can't expect XJC to guess that you meant java.net.InetAddress.

Upvotes: 2

Related Questions