views:

27

answers:

1

We have a class, with no control over the source, so no way to annotate it for JAXB. We also have a framework to take care of marshaling. Is there any way for this framework to ask that class whether it is marshallable when no annotations are present?

+1  A: 

There is no standard mechanism, but I have seem people accomplish this by trying to create the JAXBContext on the class:

public boolean isValidJAXBClass(Class aClass) {
    try {
        JAXBContext.newInstance(aClass);
    } catch(JAXBException e) {
        return false;
    }
    return true;
}

You don't need any annotations to marshal a JAXB object. You can get around having to have an @XmlRootElement by wrapping it in a JAXBElement.

If you want an alternative means to represent the metadata, EclipseLink JAXB (MOXy) has a externalized binding file based on the JAXB metadata

A sample file looks something like:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"&gt;
    <java-types>
        <java-type name="org.example.order.PurchaseOrder">
            <java-attributes>
                <xml-attribute java-attribute="id"/>
                <xml-element java-attribute="customer">
                    <xml-java-type-adapter value="org.example.order.CustomerAdapter"/>
                </xml-element>
                <xml-element java-attribute="lineItems" name="line-item"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

For more information see:

Blaise Doughan