@Test public void testCreateElement() throws JAXBException { JsonXMLRootProvider provider = new JsonXMLRootProvider(); JAXBElement<?> sampleRootElement = provider.createElement(SampleRootElement.class, new SampleRootElement()); Assert.assertEquals(new QName("sampleRootElement"), sampleRootElement.getName()); Assert.assertEquals(SampleRootElement.class, sampleRootElement.getDeclaredType()); JAXBElement<?> sampleType = provider.createElement(SampleType.class, new SampleType()); Assert.assertEquals(new QName("sampleType"), sampleType.getName()); Assert.assertEquals(SampleType.class, sampleType.getDeclaredType()); JAXBElement<?> sampleTypeWithNamespace = provider.createElement(SampleTypeWithNamespace.class, new SampleTypeWithNamespace()); Assert.assertEquals(new QName("urn:staxon:jaxb:test", "sampleTypeWithNamespace"), sampleTypeWithNamespace.getName()); Assert.assertEquals(SampleTypeWithNamespace.class, sampleTypeWithNamespace.getDeclaredType()); Assert.assertNull(provider.createElement(EmptyType.class, new EmptyType())); }
public JAXBElement<?> createElement(Class<?> type, Object value) throws JAXBException { if (type.getAnnotation(XmlRootElement.class) != null) { QName name = getXmlRootElementName(type); if (name != null) { @SuppressWarnings({"rawtypes", "unchecked"}) JAXBElement<?> genericElement = new JAXBElement(name, type, value); return genericElement; } } else if (type.getAnnotation(XmlType.class) != null) { Method method = getXmlElementDeclMethod(type); if (method != null) { try { return (JAXBElement<?>)method.invoke(method.getDeclaringClass().newInstance(), value); } catch (Exception e) { throw new JAXBException("Cannot create JAXBElement", e); } } } return null; }
JsonXMLRootProvider { public JAXBElement<?> createElement(Class<?> type, Object value) throws JAXBException { if (type.getAnnotation(XmlRootElement.class) != null) { QName name = getXmlRootElementName(type); if (name != null) { @SuppressWarnings({"rawtypes", "unchecked"}) JAXBElement<?> genericElement = new JAXBElement(name, type, value); return genericElement; } } else if (type.getAnnotation(XmlType.class) != null) { Method method = getXmlElementDeclMethod(type); if (method != null) { try { return (JAXBElement<?>)method.invoke(method.getDeclaringClass().newInstance(), value); } catch (Exception e) { throw new JAXBException("Cannot create JAXBElement", e); } } } return null; } }
JsonXMLRootProvider { public JAXBElement<?> createElement(Class<?> type, Object value) throws JAXBException { if (type.getAnnotation(XmlRootElement.class) != null) { QName name = getXmlRootElementName(type); if (name != null) { @SuppressWarnings({"rawtypes", "unchecked"}) JAXBElement<?> genericElement = new JAXBElement(name, type, value); return genericElement; } } else if (type.getAnnotation(XmlType.class) != null) { Method method = getXmlElementDeclMethod(type); if (method != null) { try { return (JAXBElement<?>)method.invoke(method.getDeclaringClass().newInstance(), value); } catch (Exception e) { throw new JAXBException("Cannot create JAXBElement", e); } } } return null; }  }
JsonXMLRootProvider { public JAXBElement<?> createElement(Class<?> type, Object value) throws JAXBException { if (type.getAnnotation(XmlRootElement.class) != null) { QName name = getXmlRootElementName(type); if (name != null) { @SuppressWarnings({"rawtypes", "unchecked"}) JAXBElement<?> genericElement = new JAXBElement(name, type, value); return genericElement; } } else if (type.getAnnotation(XmlType.class) != null) { Method method = getXmlElementDeclMethod(type); if (method != null) { try { return (JAXBElement<?>)method.invoke(method.getDeclaringClass().newInstance(), value); } catch (Exception e) { throw new JAXBException("Cannot create JAXBElement", e); } } } return null; }  QName getName(Class<?> type); JAXBElement<?> createElement(Class<?> type, Object value); }
JsonXMLRootProvider { public JAXBElement<?> createElement(Class<?> type, Object value) throws JAXBException { if (type.getAnnotation(XmlRootElement.class) != null) { QName name = getXmlRootElementName(type); if (name != null) { @SuppressWarnings({"rawtypes", "unchecked"}) JAXBElement<?> genericElement = new JAXBElement(name, type, value); return genericElement; } } else if (type.getAnnotation(XmlType.class) != null) { Method method = getXmlElementDeclMethod(type); if (method != null) { try { return (JAXBElement<?>)method.invoke(method.getDeclaringClass().newInstance(), value); } catch (Exception e) { throw new JAXBException("Cannot create JAXBElement", e); } } } return null; }  QName getName(Class<?> type); JAXBElement<?> createElement(Class<?> type, Object value);  }