title: Jaxb objects with same name
body: <p>It would appear to be possible to unmarshal two <em>different</em> <code>jaxb</code> objects with the same <strong>name</strong>. </p>
  
  <p>There is a <code>Bar</code> class  ...</p>
  
  <pre><code>public abstract Bar {
     private @XmlElement String val;
  }
  </code></pre>
  
  <p>.. with Two implementations (constructors etc. left out):</p>
  
  <pre><code>@XmlRootElement(name="bar")
  public class BarA extends Bar { }
  
  @XmlRootElement(name="bar")
  public class BarB extends Bar {
       private @XmlElement(required=true) String type;
  }
  </code></pre>
  
  <p>Lastly I want to unmarshall a list of <code>Bar</code> documents from XML similar to the following</p>
  
  <pre><code>&lt;bars&gt;
    &lt;bar&gt;
      &lt;val&gt;1&lt;/val&gt;
    &lt;/bar&gt;
    &lt;bar&gt;
      &lt;val&gt;1&lt;/val&gt;
      &lt;type&gt;2&lt;/type&gt;
    &lt;/bar&gt;
  &lt;/bars&gt;
  </code></pre>
  
  <p>The list is wrapped in an class utilizing the <a href="http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlAnyElement.html" rel="nofollow noreferrer"><code>@XmlAnyElement</code></a></p>
  
  <pre><code>@XmlRootElement
  public class Bars {
  
    @XmlMixed
    @XmlAnyElement(lax = true) 
    @XmlElementRefs({@XmlElementRef(BarA.class), @XmlElementRef(BarB.class)})
    private List&lt;Bar&gt; bars;
  }
  </code></pre>
  
  <p>However I seem to <em>only</em> get instances of <em>either</em> <code>BarA</code> or <code>BarB</code>, whichever is the <em>last</em> element in the <code>@XmlElementRefs</code> chain. </p>
  
  <p>Testing code: </p>
  
  <pre><code>String xml = ...
  JAXBContext jc = JAXBContext.newInstance(Bars.class);
  ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
  Bars bars = (Bars) jc.createUnmarshaller().unmarshal(in);
  for (Bar bar : bars.getBars()) {
      System.out.println(bar.getClass());
  }
  </code></pre>
  
  <p>I don't think the <code>XmlAdapter</code> suggested in <a href="https://stackoverflow.com/questions/5007516/jaxb-xmlelements-different-types-but-same-name">JAXB @XmlElements, different types but same name?</a> would neccesarily be the only approach either. </p>