View Javadoc

1   package org.codehaus.xfire.jaxb;
2   
3   import com.sun.xml.bind.RIElement;
4   import org.codehaus.xfire.XFireRuntimeException;
5   
6   import javax.xml.bind.JAXBContext;
7   import javax.xml.namespace.QName;
8   import java.lang.reflect.Method;
9   
10  public class JaxbIntrospector
11  {
12      private JAXBContext jaxbContext;
13  
14      public JaxbIntrospector(JAXBContext jaxbContext)
15      {
16          this.jaxbContext = jaxbContext;
17      }
18  
19      public QName introspect(Class clazz)
20      {
21          String typeName = clazz.getName();
22          typeName = typeName.substring(typeName.lastIndexOf('.') + 1);
23  
24          Method creator;
25          // we need to create an instance of the jaxb bean to find out the qname
26          RIElement instance;
27          try
28          {
29              // use reflection to call create<TypeName>
30              creator = jaxbContext.getClass().getMethod("create" + typeName, null);
31              instance = (RIElement) creator.invoke(jaxbContext, null);
32  
33          } catch (Exception e)
34          {
35              throw new XFireRuntimeException("error getting qname of jaxb type", e);
36          }
37          return new QName(instance.____jaxb_ri____getNamespaceURI(), instance.____jaxb_ri____getLocalName());
38  
39      }
40  }