title: Two classes have the same XML type name "objectFactory"
body: <p>We have been using JAXB 2.1 for a long time in our system. We have a platform that is built with Ant and generates a bunch of bundles that are deployed in an OSGi runtime. We use Java SE 6.</p>
  
  <p>We use JAXB during the build process to generate datatypes from different schemas. Those classes are packaged in the bundles and used in runtime to serialize/deserialize content. In addition we use JAXB in our platform in runtime to generate datatypes from other schemas provided by the user (it a sort of MDA platform).</p>
  
  <p>In the OSGi runtime we have a bundle that has the JAXB jars and exports the necessary packages. We create a JAXBContext instance with the context path of all the object factories generated, so we can marshall/unmarshall all our datatypes. </p>
  
  <p>That has been working so far but right now we are trying to upgrade to the latest stable version of JAXB <strong>(2.2.4)</strong> and we are having problems trying to create the context in runtime. We get the following exception:</p>
  
  <pre><code>Two classes have the same XML type name "objectFactory". Use @XmlType.name and @XmlType.namespace to assign different names to them.
      this problem is related to the following location:
          at some.package.ObjectFactory
      this problem is related to the following location:
          at some.other.package.ObjectFactory
  
      at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:436)
      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.&lt;init&gt;(JAXBContextImpl.java:277)
      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
      at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
      at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
      at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:191)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:187)
      ... 76 more
  </code></pre>
  
  <p>The error <em>Two classes have the same XML type name "objectFactory"</em> is printed for each of the object factories generated during the build process.</p>
  
  <p>We have seen several posts in SO with the same error but applying to the generated types, not to the object factory. We think that JAXB may not be identifying the ObjectFactory class as an object factory but as a datatype.</p>
  
  <p>One possibility was that we were using the internal version of JAXB in Java 6, so we decided to use the System Property <strong>-Djava.endorsed.dirs</strong> and put the three jars (<strong>jaxb-api-2.2.4.jar, jaxb-impl-2.2.4.jar and jaxb-xjc-2.2.4.jar</strong>) in that path, but still not working.</p>
  
  <p>We think that the problem might be that we are using a different version of JAXB in the OSGi runtime and in the build process, so the generated code is not compatible. But maybe we are wrong and there is another problem.</p>
  
  <p>Do you have any ideas?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>(Edit: more details on this)</strong></p>
  
  <p>We create the JAXBContext in this way:</p>
  
  <pre><code>    ClassLoader classLoader = new JAXBServiceClassLoader(getParentClassLoader(),
                                                           Collections.unmodifiableMap(objectFactories));
      context = JAXBContext.newInstance(contextPath.toString(), classLoader);
  </code></pre>
  
  <p>where contextPath is a String that contains all our object factories separated by ':', and the JAXBServiceClassLoader is:</p>
  
  <pre><code>  private static final class JAXBServiceClassLoader extends ClassLoader
    {
      @NotNull
      private final Map&lt;String, Object&gt; objectFactories;
  
      private JAXBServiceClassLoader(@NotNull ClassLoader parent, @NotNull Map&lt;String, Object&gt; objectFactories)
      {
        super(parent);
        this.objectFactories = objectFactories;
      }
  
      @Override
      public Class&lt;?&gt; loadClass(String name) throws ClassNotFoundException
      {
        Class&lt;?&gt; ret;
        try
        {
          ret = super.loadClass(name);
        }
        catch (ClassNotFoundException e)
        {
          Object objectFactory = objectFactories.get(name);
          if (objectFactory != null)
          {
            ret = objectFactory.getClass();
          }
          else
          {
            throw new ClassNotFoundException(name + " class not found");
          }
        }
        return ret;
      }
    }
  </code></pre>
  
  <p><strong>(Edit: after Aaron's post)</strong></p>
  
  <p>I've been debugging all the internals of JAXBContextImpl and the thing is that the JAXBContextImpl is trying to get the type info from our ObjectFactory classes, which is wrong. In fact, in com.sun.xml.internal.bind.v2.model.impl.ModelBuilder:314, the getClassAnnotation() call returns null but when I see the instance I can see the annotation XmlRegistry.</p>
  
  <p>The thing is that, at that point XmlRegistry.class.getClassLoader() returns null, but if I run ((Class)c).getAnnotations()[0].annotationType().getClassLoader() it returns the classLoader of the OSGi bundle "lib.jaxb" that contains my JAXB jars, which is correct.</p>
  
  <p>So, I guess that we are loading at the same time two different versions of XmlRegistry, one from the JDK and the other one from JAXB 2.2.4 jars. The question is: why?</p>
  
  <p>And, even more, instead of loading all those com.sun.xml.internal.* classes (like JAXBContextImpl), shouldn't be loading and executing com.sun.xml.bind.v2.runtime.JAXBContextImpl from JAXB jars? During the debug process I can see that it's doing some stuff with reflection but I don't understand why is doing that.</p>