2
votes

I am trying to map the below interface using Moxy's XML Metadata extension. But when I try to load it, I am getting the below error. I can't add a public constructor to the AddressType as it is an enum.

My question is: Why is Moxy impl looking at AddressType even though I didn't specify in the xml metadata?

public interface TokenizedUnitedStatesAddress
{
    class AddressType extends Enum
    {
        public static final AddressType STREET = new AddressType("street");    
        public static final AddressType PO_BOX = new AddressType("poBox");    
        public static final AddressType RURAL_ROUTE = new AddressType("ruralRoute");

        public static AddressType getEnum(final String type)
        {
            return (AddressType) getEnum(AddressType.class, type);
        }

        protected AddressType(final String name)
        {
            super(name);
        }
    }

    String getApartmentNumber();

    //removed other getters for brevity
}

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
   version="2.4" package-name="com.abc.ic.domain.country.us">
   <java-types>
      <java-type name="TokenizedUnitedStatesAddress">
         <xml-root-element />
         <xml-type
            prop-order="StreetPreDirection StreetNumber StreetName StreetType StreetPostDirection UnitDesignator UnitNumber AddressLine1 AddressLine2 City State PostalCode CarrierRoute LengthAtAddress OwnershipStatus" />
         <java-attributes>
            <xml-element name="StreetPreDirection" java-attribute="preDirectional" />
            <xml-element name="StreetNumber" java-attribute="houseNumber" />
            <xml-element name="StreetName" java-attribute="streetName" />
            <xml-element name="StreetType" java-attribute="streetType" />
            <xml-element name="StreetPostDirection" java-attribute="postDirection" />
            <xml-element name="UnitNumber" java-attribute="apartmentNumber" />
            <xml-element name="AddressLine1" java-attribute="primaryAddress" />
            <xml-element name="AddressLine2" java-attribute="secondaryAddress" />
            <xml-element name="City" java-attribute="cityName" />
            <xml-element name="State" java-attribute="stateAbbreviation" />
            <xml-element name="PostalCode" java-attribute="zipCode" />
         </java-attributes>
      </java-type>
   </java-types>
</xml-bindings>

javax.xml.bind.JAXBException: 
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.
 - with linked exception:
[Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:908)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:157)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:170)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:157)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:117)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:107)
2
This may be a bug, could you enter one at the following location: bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink.bdoughan
"Mxoy Impl introspecting the class even though XML is being used to provide metadata" - is this the correct bug summary? Also, it would be good if you can explain what is going on as an answer to this question .Aravind Yarram
Also, I can fix this bug for you if you can provide which class I should look intoAravind Yarram
The external mapping file is used to augment metadata supplied by annotations. It can also be used to totally replace it (see: blog.bdoughan.com/2011/09/…). It appears as though MOXy is trying to process the enum as a domain class. org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor` could be the culprit. We'd appreciate any patches you wish to submit. I wish I could be more assistance at the moment, but I'm headed out of town. You can also post to the EclipseLink forum: eclipse.org/forums/index.php?t=thread&frm_id=111bdoughan
ok. But the AddressType is not Java 5 enum. It is a the Enum abstraction from commons-lang.Aravind Yarram

2 Answers

1
votes

Mxoy impl still introspects the class even though XML is used to provide metadata. This is because, by design, the external mapping file is used to augment metadata supplied by annotations.

The issue however is that the commons-land Enum abstraction requires us to have a non-public single argument constructor for the enums. I fixed this issue by adding a public no-arg constructor that initializes a default enum. This is sufficient for my application. I've however created a bug which can be followed here.

Note: I also looked at the foctory-method option of Moxy but it requires an empty arg method as the factory method which is not the case in case of Enum.

0
votes

I am a developer on the EclipseLink MOXy team, and I've been looking at this issue. You are correct as to why the AddressType class was introspected, and I see that you have a workaround.

Another solution would be to create an XmlAdapter that can convert between Apache Enum classes and their XML (string) representation, as follows:

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.apache.commons.lang.enums.Enum;

import enumbindings.TokenizedUnitedStatesAddress.AddressType;

public class ApacheEnumAdapter extends XmlAdapter<String, Enum> {

    public ApacheEnumAdapter() {
    }

    @Override
    public Enum unmarshal(String s) throws Exception {
        return AddressType.getEnum(s);
    }

    @Override
    public String marshal(Enum e) throws Exception {
        if (null == e) {
            return null;
        }
        return e.getName();
    }

}

And then hook up the adapter in your bindings file like this:

...
<xml-element name="StreetType" java-attribute="streetType">
    <xml-java-type-adapter value="enumbindings.ApacheEnumAdapter" />
</xml-element>
...

As far as the bug you entered, EclipseLink is actually behaving correctly in this situation, we do not do any special handling of Apache Commons classes and so a default no-arg constructor (or some other handling mechanism) is still required. However I will update your bug and change it to an enhancement request to support Apache Enums out of the box, and we will evaluate it.

Thanks,

Rick