-
Notifications
You must be signed in to change notification settings - Fork 100
JAXB Ploymorphism with attributes is broken (throws NPE) #889
Description
I have an abstract identifier class, which has a value. Note that the value is annotated as an xml attribute. This example works if value is annotated as an element.
@XmlSeeAlso({
AbstractIDInt.class
})
@XmlRootElement(name = "AbstractID")
@XmlAccessorType(XmlAccessType.PROPERTY)
public abstract class AbstractID {
@XmlAttribute
abstract Object getValue ();
}
And, here is a class that extends the abstract id class:
@XmlRootElement(name = "AbstractIDInt")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class AbstractIDInt extends AbstractID {
Integer value;
Integer getValue ()
{ return value; }
public void setvalue (Integer value)
{ this.value = value; }
}
If you try to marshal anything that is of type AbstractID, you will get the following exception:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154)
at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty.(AttributeProperty.java:56)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:93)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(ClassBeanInfoImpl.java:132)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:305)
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 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:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
Here is a SO article discussing the issue:
http://stackoverflow.com/questions/9382200/jaxb-and-polymorphism
Environment
Using JAXB 2.2.X as part of CXF 2.4.X
Affected Versions
[2.2, 2.2.1, 2.2.2, 2.2.3, 2.2.3u1, 2.2.3u2, 2.2.4, 2.2.4u1, 2.2.4u2, 2.2.5]