title: How to use XmlAdapter to map XML complex types to Java objects in schema-generated classes?
body: <p>Using this (demo) schema, I am generating Java objects with JAXB:</p>
  
  <pre><code>&lt;xsd:complexType name="someType"&gt;
      &lt;xsd:sequence&gt;
          &lt;xsd:element name="myOtherType" type="otherType" maxOccurs="unbounded" /&gt;
      &lt;/xsd:sequence&gt;
  &lt;/xsd:complexType&gt;
  
  &lt;xsd:complexType name="otherType"&gt;
      &lt;!-- ... --&gt;
  &lt;/xsd:complexType&gt;
  </code></pre>
  
  <p>This class gets generated:</p>
  
  <pre><code>@XmlType
  public class SomeType {
  
      @XmlElement(name = "myOtherType")
      OtherType myOtherType;
  
  }
  </code></pre>
  
  <p>But I would like to use interfaces instead of implementations in my JAXB-generated objects.</p>
  
  <p>So I write this interface:</p>
  
  <pre><code>public interface OtherTypeInterface {
      // ....
  }
  </code></pre>
  
  <p>And I let the generated OtherType class implement it, with the help of a binding file:</p>
  
  <pre><code>&lt;jxb:bindings node="//xs:complexType[@name='otherType']"&gt;
      &lt;inheritance:implements&gt;com.example.OtherTypeInterface&lt;/inheritance:implements&gt;
  &lt;/jxb:bindings&gt; 
  </code></pre>
  
  <p>So far, so good:</p>
  
  <pre><code>public class OtherType implements OtherTypeInterface {
  
      // ...
  
  }
  </code></pre>
  
  <p>But I need the SomeType object to use this interface too, instead of the OtherType implementation. As suggested <a href="https://jaxb.java.net/guide/Mapping_interfaces.html" rel="nofollow noreferrer">in the unofficial JAXB guide</a> in section <em>3.2.2. Use @XmlJavaTypeAdapter</em>, I would like to use a homemade XML adapter to map OtherType to its interface, and vice-versa:</p>
  
  <pre><code>public class HcpartyTypeAdapter extends XmlAdapter&lt;OtherType, OtherTypeInterface&gt; {
  
      @Override
      public OtherTypeInterface unmarshal(OtherType v) throws Exception {
          return v;
      }
  
      @Override
      public OtherType marshal(OtherTypeInterface v) throws Exception {
          return (OtherType) v;
      }
  
  }
  </code></pre>
  
  <p>But it looks like mapping an XML complex type in my binding file with the following configuration is a big no-no:</p>
  
  <pre><code>&lt;jxb:globalBindings&gt;
      &lt;xjc:javaType name="com.example.OtherTypeInterface" xmlType="ex:otherType" adapter="com.example.OtherTypeAdapter"/&gt;
  &lt;/jxb:globalBindings&gt;
  </code></pre>
  
  <p>The generation fails with this error:</p>
  
  <blockquote>
    <p>com.sun.istack.SAXParseException2; systemId: file:/.../bindings.xjb;
    lineNumber: 8; columnNumber: 22; undefined simple type
    "{<a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a>}otherType".</p>
  </blockquote>
  
  <p>With <a href="https://java.net/jira/browse/JAXB-111?focusedCommentId=64447&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-64447" rel="nofollow noreferrer">a bit of googling</a>, I've found out that it is apparently impossible to use XML adapters for complex types in schema-generated classes. However, if I'm manually editing the files to use my adapter, it works perfectly:</p>
  
  <pre><code>public class SomeType {
  
      @XmlElement(name = "myOtherType")
      @XmlJavaTypeAdapter(OtherTypeAdapter.class)
      @XmlSchemaType(name = "otherType")   
      OtherTypeInterface myOtherType;
  
  }
  </code></pre>
  
  <p>I can marshal and unmarshal it flawlessly; but it seems to me that editing the generated classes defeats the whole purpose of the automatic processing. I'm dealing with multiple schemas defining many, many types.</p>
  
  <p>So my question is: does anyone know a workaround to use XML adapters to map XML complex types to Java objects in schema-generated classes without resorting to manually edit the code?</p>
  
  <hr>
  
  <p>Potential answer here: <a href="https://stackoverflow.com/a/1889584/946800">https://stackoverflow.com/a/1889584/946800</a>. I'm hoping that since 2009, someone may have found some way to solve this issue...</p>