users@jaxb.java.net

Unexpected class cast exception when using xs:anySimpleType

From: Luc Moreau <L.Moreau_at_ecs.soton.ac.uk>
Date: Tue, 06 Dec 2011 14:58:52 +0000

Hi,

I am using JAXB 2.2.4-1 (but the problem describe here also occurred
with older versions).


Each of the following XML elements with xsi:type attribute

<ex:demo xsi:type="xsd:QName">prim:align_warp</ex:demo>
<ex:demo xsi:type="xsd:int">10</ex:demo>
<ex:demo xsi:type="xsd:int">-10</ex:demo>
<ex:demo xsi:type="xsd:double">-10.55</ex:demo>
<ex:demo xsi:type="xsd:string">abc</ex:demo>
<ex:demo xsi:type="xsd:boolean">true</ex:demo>
<ex:demo xsi:type="xsd:anyURI">http://www.example.com/hi</ex:demo>

is compliant with the following XML Schema declaration:

<xs:element name="demo" type="xs:anySimpleType"/>

For it, JAXB generates a Java method, which I have successfully used
to generate the above instances.

     public JAXBElement<Object> createDemo(Object value) {
         return new JAXBElement<Object>(_Demo_QNAME, Object.class, null,
value);
     }

Instead of xs:anySimpleType, I would like to define the following

<xs:complexType name="TypedLiteral">
<xs:simpleContent>
<xs:extension base="xs:anySimpleType">
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:element name="demo" type="ex:TypedLiteral"/>

(In particular, with a view of adding further attributes. )

JAXB generates the following class

public class TypedLiteral
     implements Equals, HashCode, ToString
{

     @XmlValue
     @XmlSchemaType(name = "anySimpleType")
     protected Object value;

    ... getValue ... setValue ...


}

and the following method.

     public JAXBElement<TypedLiteral> createDemo(TypedLiteral value) {
         return new JAXBElement<TypedLiteral>(_Demo_QNAME,
TypedLiteral.class, null, value);
     }

Using the method to generate the instance, the following exception (see
below) is
thrown when serializing my java beans. Looking at the source code,
there seems to an expectation that the content of my value variable
(with @XmlSchemaType(name = "anySimpleType")) should be of type
String. My code fails when I provide a QName (with a view to generate
the following instance)

<ex:demo xsi:type="xsd:QName">prim:align_warp</ex:demo>


I thought the two schema definitions should be handled in a similar
way, but it's obviously not the case. Can you advise on this issue?

Best regards,
Luc



-------------------------------------------------------------------------------
Test set: org.openprovenance.prov.xml.PC1FullTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.644
sec <<< FAILURE!
testPC1Full(org.openprovenance.prov.xml.PC1FullTest) Time elapsed:
0.557 sec <<< ERROR!
java.lang.ClassCastException: javax.xml.namespace.QName cannot be cast
to java.lang.String
     at
com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$StringImplImpl.writeText(RuntimeBuiltinLeafInfoImpl.java:1031)
     at
com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeText(TransducedAccessor.java:261)
     at
com.sun.xml.bind.v2.runtime.property.ValueProperty.serializeBody(ValueProperty.java:87)
     at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:356)
     at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)
     at
com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:161)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:205)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:333)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:340)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76)
     at
com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:118)
     at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
     at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:356)
     at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)
     at
com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69)
     at
com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172)
     at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
     at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:356)
     at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)
     at
com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
     at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:356)
     at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:700)
     at
com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:161)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:205)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:333)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:340)
     at
com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76)
     at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:498)
     at
com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
     at
com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
     at
javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:83)
     at
org.openprovenance.prov.xml.ProvSerialiser.serialiseContainer(ProvSerialiser.java:109)
     at
org.openprovenance.prov.xml.PC1FullTest.testPC1Full(PC1FullTest.java:70)
     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 junit.framework.TestCase.runTest(TestCase.java:168)
     at junit.framework.TestCase.runBare(TestCase.java:134)
     at junit.framework.TestResult$1.protect(TestResult.java:110)
     at junit.framework.TestResult.runProtected(TestResult.java:128)
     at junit.framework.TestResult.run(TestResult.java:113)
     at junit.framework.TestCase.run(TestCase.java:124)
     at junit.framework.TestSuite.runTest(TestSuite.java:232)
     at junit.framework.TestSuite.run(TestSuite.java:227)
     at
org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
     at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
     at
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
     at
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
     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
org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
     at $Proxy0.invoke(Unknown Source)
     at
org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
     at
org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
     at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

testCopyPC1Full(org.openprovenance.prov.xml.PC1FullTest) Time elapsed:
0.001 sec <<< ERROR!
java.lang.NullPointerException
     at
org.openprovenance.prov.xml.ProvFactory.newContainer(ProvFactory.java:961)
     at
org.openprovenance.prov.xml.PC1FullTest.testCopyPC1Full(PC1FullTest.java:615)
     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 junit.framework.TestCase.runTest(TestCase.java:168)
     at junit.framework.TestCase.runBare(TestCase.java:134)
     at junit.framework.TestResult$1.protect(TestResult.java:110)
     at junit.framework.TestResult.runProtected(TestResult.java:128)
     at junit.framework.TestResult.run(TestResult.java:113)
     at junit.framework.TestCase.run(TestCase.java:124)
     at junit.framework.TestSuite.runTest(TestSuite.java:232)
     at junit.framework.TestSuite.run(TestSuite.java:227)
     at
org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
     at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
     at
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
     at
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
     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
org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
     at $Proxy0.invoke(Unknown Source)
     at
org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
     at
org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
     at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)





-- 
Professor Luc Moreau
Electronics and Computer Science   tel:   +44 23 8059 4487
University of Southampton          fax:   +44 23 8059 2865
Southampton SO17 1BJ               email: l.moreau_at_ecs.soton.ac.uk
United Kingdom                     http://www.ecs.soton.ac.uk/~lavm