I'm working with jaxb 1.0.2.
I'm trying to use <xjc:typeSubstitution type="complex"> for some objects
with xsd:ID and xsd:IDREF attributes. Just adding the tag breaks things.
(Removing it fixes the problems, but then I can't use the feature.)
Inside of the jaxb-generated TypeImpl.java, the code tries to cast the
xsd:IDREF instances to com.sun.xml.bind.marshaller.IdentifiableObjects.
But the objects don't implement the interface, so I get a
ClassCastException (took some digging to find it.
Util.handlePrintConversionException looses it inside an
AbortSerializationException).
IDs work fine without <xjc:typeSubstitution type="complex">. (I need
the typeSubstitution to give the jaxb-gnerated objects some behavior.)
You should be able to reproduce the problem by running this test case, that
combines a build script, JUnit test, schema and some support classes.
Thanks for taking care of us,
Dave and Jeremy
Directory structure for the test case:
jaxb-bug-report
- build.xml
- source
- com
- alphatech
- jaxb
- report
- MySmallThingImpl.java
- MySpecialSmallThingImpl.java
- TypeSubstitutionTest.java
- jaxb-bug.xsd
- jaxb-javadoc.xslt
โ
build.xml:
<property file="$
{common}/source/properties/override-build.properties" />
<property file="${common}
/source/properties/build.properties" />
<mkdir dir="${compiled}
/jaxb-bug" />
<javac destdir="$
{compiled}/jaxb-bug" deprecation="yes" debug="yes" >
<copy todir="${compiled}
/jaxb-bug" overwrite="true">
<fileset dir="$
{build}/jaxb-java" >
<sysproperty key="testdir" value="$
{build}
/test-area"/>
<pathelement path="$
{product-jar}
" />
<pathelement path="$
{product-test-jar}
" />
<pathelement path="$
{junit-jar}
" />
<pathelement path="$
{jaxb-jar}
" />
<pathelement path="$
{jaxb-impl-jar}
" />
<pathelement path="$
{jaxb-libs-jar}
" />
<pathelement path="$
{jaxp-xercesImpl-jar}
" />
<pathelement path="$
{jaxb-namespace-jar}
" />
<pathelement path="$
{jaxb-relaxngDatatype-jar}
" />
<pathelement path="$
{jaxb-xsdlib-jar}
"/>
<pathelement path="$
{jaxb-jax-qname-jar}
"/>
MySpecialThingImpl.java:
package com.alphatech.jaxb.report;
public class MySmallThingImpl
extends com.alphatech.jaxb.report.impl.SmallThingTypeImpl
{
public MySmallThingImpl()
{ super(); }
}
MySpecialSmallThingImpl.java:
package com.alphatech.jaxb.report;
public class MySpecialSmallThingImpl
extends com.alphatech.jaxb.report.impl.SpecialSmallThingTypeImpl
{
public MySpecialSmallThingImpl()
{ super(); }
}
TypeSubstitutionTest.java:
package com.alphatech.jaxb.report.test;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.StringReader;
import java.io.Reader;
import javax.xml.transform.stream.StreamSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import com.alphatech.jaxb.report.Example;
import com.alphatech.jaxb.report.ObjectFactory;
//import com.alphatech.jaxb.report.impl.BigThingTypeImpl;
//import com.alphatech.jaxb.report.impl.BigThingImpl;
import com.alphatech.jaxb.report.BigThing;
import com.alphatech.jaxb.report.SmallThing;
import com.alphatech.jaxb.report.SpecialSmallThing;
import com.alphatech.jaxb.report.SpecialSmallThingType;
import com.alphatech.jaxb.report.MySmallThingImpl;
public class TypeSubstitutionTest
extends TestCase
{
public TypeSubstitutionTest(String testName)
{ super(testName); }
public void testRoundTrip()
{
ObjectFactory of = new ObjectFactory();
try
{ Example example = of.createExample(); JAXBContext jc = JAXBContext.newInstance("com.alphatech.jaxb.report"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE); StringWriter writer = new StringWriter(); marshaller.marshal(example,writer); // marshaller.marshal(agent,System.out); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setValidating( true ); Reader reader = new StringReader(writer.toString()); Example newExample = (Example)unmarshaller.unmarshal(new StreamSource(reader)); }
catch(JAXBException jaxbe)
{ jaxbe.printStackTrace(); fail(jaxbe.getMessage()); }
}
public void testRoundTripWithAdd()
{
ObjectFactory of = new ObjectFactory();
try
{ Example example = of.createExample(); // add a BigThing to example. BigThing bigThing = of.createBigThing(); SmallThing smallThing = of.createSmallThing(); bigThing.getSmallThing().add(smallThing); SpecialSmallThingType ssT = of.createSpecialSmallThingType(); bigThing.getSmallThing().add(ssT); example.getBigThing().add(bigThing); JAXBContext jc = JAXBContext.newInstance("com.alphatech.jaxb.report"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE); StringWriter writer = new StringWriter(); marshaller.marshal(example,writer); // marshaller.marshal(agent,System.out); Unmarshaller unmarshaller = jc.createUnmarshaller(); unmarshaller.setValidating( true ); Reader reader = new StringReader(writer.toString()); Example newExample = (Example)unmarshaller.unmarshal(new StreamSource(reader)); }
catch(JAXBException jaxbe)
{ jaxbe.printStackTrace(); fail(jaxbe.getMessage()); }
}
public void testCustomSmallType()
{
ObjectFactory of = new ObjectFactory();
try
{ BigThing bigThing = of.createBigThing(); System.out.println("created BigThing"); // bigThing.setSmallthing(new MySmallThingImpl()); SmallThing smallThing = of.createSmallThing(); System.out.println("created SmallThing"); // bigThing.setSmallthing(smallThing); bigThing.getSmallThing().add(smallThing); System.out.println("set SmallThing"); SpecialSmallThingType ssT = of.createSpecialSmallThingType(); System.out.println("created sst"); bigThing.getSmallThing().add(ssT); System.out.println("set ssT"); }
catch(JAXBException jaxbe)
{ jaxbe.printStackTrace(); fail(jaxbe.getMessage()); }
}
public static Test suite()
{ TestSuite suite = new TestSuite(); suite.addTest(new TypeSubstitutionTest("testRoundTrip")); suite.addTest(new TypeSubstitutionTest("testRoundTripWithAdd")); // suite.addTest(new TypeSubstitutionTest("testCustomSmallType")); return suite; }
}
jaxb-bug.xsd:
<xsd:schema targetNamespace="http://alphatech.com/pct/plan"
xmlns="http://alphatech.com/pct/plan"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" elementFormDefault="qualified"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">
xsd:annotation
xsd:appinfo
<jaxb:globalBindings fixedAttributeAsConstantProperty="true">
<xjc:typeSubstitution type="complex"/>
</jaxb:globalBindings>
jaxb:schemaBindings
<jaxb:package name="com.alphatech.jaxb.report">
jaxb:javadoc
This package contains JavaBean classes for reporting a bug to JaxB
<xsd:complexType name="SpecialSmallThingType">
xsd:annotation
xsd:appinfo
<jaxb:class implClass="com.alphatech.jaxb.report.MySpecialSmallThingImpl"/>
</xsd:appinfo>
</xsd:annotation>
xsd:complexContent
<xsd:extension base="SmallThingType">
<xsd:attribute name="token" type="xsd:token"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="SpecialSmallThing" type="SpecialSmallThingType"
substitutionGroup="SmallThing">
</xsd:element>
</xsd:schema>
jaxb-javadoc.xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@" >
xsl:copy
<xsl:copy-of select="@"/>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:element/xsd:annotation/xsd:documentation">
xsd:appinfo
jaxb:class
jaxb:javadoc
xsl:apply-templates/
</jaxb:javadoc>
</jaxb:class>
</xsd:appinfo>
xsl:copy
<xsl:copy-of select="@*"/>
xsl:apply-templates/
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:complexType/xsd:annotation/xsd:documentation">
xsd:appinfo
jaxb:class
jaxb:javadoc
xsl:apply-templates/
</jaxb:javadoc>
</jaxb:class>
</xsd:appinfo>
xsl:copy
<xsl:copy-of select="@*"/>
xsl:apply-templates/
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Environment
Operating System: All
Platform: All
Affected Versions
[JWSDP1.3 (JAXB1.0.2)]