users@jaxb.java.net

Re: Abstract Types and Separate Compilation Times

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 11 Feb 2009 08:27:28 +0100

In Implementation.xsd, don't use an inline type for element
Implementation. If you extract the complexType, give it a name, (say
ImplType) and refer to that in the xs:element (if you still need that
element), all will be well.

Or simply remove the element, and use Implementation as the name for
the previously anonymous type.

I've made it my rule to avoid anonymous types. Apart from pitfalls
like this one, the resulting Java code will me much clearer, and names
remain close(r) to what is used in the schema, and they don't "jump
categories" (from a element/field to a complexType/class).

On Wed, Feb 11, 2009 at 2:33 AM, Jamie Johnson <jej2003_at_gmail.com> wrote:
> and more specifically
>
> Data d = new Data();
>
>
> d.getAbstractBase().add(new Implementation());
>
> gives the following when I try to marshall it
>
> Exception in thread "main" javax.xml.bind.MarshalException
> - with linked exception:
> [com.sun.istack.SAXException2: Instance of
> "com.test.implementation.Implementation" is substituting
> "com.test.abstractbase.AbstractType", but
> "com.test.implementation.Implementation" is bound to an anonymous type.]
> at
> com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
> at
> com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:254)
> at
> javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
> at com.csp.test.TestClass.main(TestClass.java:21)
> Caused by: com.sun.istack.SAXException2: Instance of
> "com.test.implementation.Implementation" is substituting
> "com.test.abstractbase.AbstractType", but
> "com.test.implementation.Implementation" is bound to an anonymous type.
> at
> com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:244)
> at
> com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:659)
> at
> com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
> at
> com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
> at
> com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:152)
> at
> com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:322)
> at
> com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:589)
> at
> com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:312)
> at
> com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
> at
> com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:325)
> ... 3 more
>
> On Tue, Feb 10, 2009 at 8:20 PM, Jamie Johnson <jej2003_at_gmail.com> wrote:
>>
>> Thanks for the reply Wolfgang,
>>
>> I tried your suggestion but I think I am missing something. I now have
>> the following in Data, but I can't add Implementation to this list. Am I
>> missing something?
>>
>> protected List<AbstractType> abstractBase;
>>
>>
>>
>>
>>
>> On Tue, Feb 10, 2009 at 8:49 AM, Wolfgang Laun <wolfgang.laun_at_gmail.com>
>> wrote:
>>>
>>> Hi Jamie,
>>>
>>> in Implementation.xsd you have
>>>
>>> <xs:import namespace="http://test.com/AbstractBase.xsd"
>>> schemaLocation="./AbstractBase.xsd"/>
>>> <xs:element name="Implementation"
>>> substitutionGroup="fb:AbstractBase">
>>> <xs:complexType>
>>> <xs:complexContent>
>>> <xs:extension base="fb:AbstractType">
>>>
>>> Omit the substitutionGroup attribute. You don't need this as the
>>> xs:extension is sufficient to give you the desired class hierarchy.
>>>
>>> You might use substitutionGroup if your schema (simply put) defines
>>> elements, with inline types, and you want to obtain inheritance
>>> structures based on these element definitions. This triggers the usage
>>> of the JAXBElement wrapper.
>>>
>>> Cheers
>>> Wolfgang
>>>
>>>
>>>
>>> On Tue, Feb 10, 2009 at 2:10 PM, Jamie Johnson <jej2003_at_gmail.com> wrote:
>>> > Compiling AbstractBase.xsd and Root.xsd together builds this:
>>> > @XmlElement(name = "AbstractBase", namespace =
>>> > "http://test.com/AbstractBase.xsd")
>>> > protected List<AbstractType> abstractBase;
>>> >
>>> > Compiling all three together builds this:
>>> > @XmlElementRef(name = "AbstractBase", namespace =
>>> > "http://test.com/AbstractBase.xsd", type = JAXBElement.class)
>>> > protected List<JAXBElement<? extends AbstractType>> abstractBase;
>>> >
>>> > The second result makes the result unusable as I can't do any
>>> > substitutions.
>>> >
>>> > On Tue, Feb 10, 2009 at 1:56 AM, Wolfgang Laun
>>> > <wolfgang.laun_at_gmail.com>
>>> > wrote:
>>> >>
>>> >> I cannot confirm that extending a complex type with abstract="true",
>>> >> either within the same schema or in a second schema imprting the first
>>> >> one,
>>> >> is sufficient to trigger the inclusion of the JAXBElement wrapper.
>>> >> Could you
>>> >> please post the (reduced) schema code?
>>> >>
>>> >> Wolfgang
>>> >>
>>> >> On Mon, Feb 9, 2009 at 6:47 PM, Jamie Johnson <jej2003_at_gmail.com>
>>> >> wrote:
>>> >>>
>>> >>> I am running into an issue where I have created several XSDs, xsd 1
>>> >>> has
>>> >>> an abstract type defined, xsd 2 implements the xsd (it is important
>>> >>> to note
>>> >>> that xsd 1 has no implementations of the abstract type). If I
>>> >>> compile these
>>> >>> schemas together I get the expected result in that the Java created
>>> >>> has a
>>> >>> method of the form.
>>> >>>
>>> >>> setGeometry(JAXBElement<? extends GeometryBaseType>)
>>> >>>
>>> >>> but if I compile them separately the method looks like
>>> >>>
>>> >>> setGeometry(GeometryBaseType)
>>> >>>
>>> >>> which does not allow me to properly use the object generated in xsd
>>> >>> 2.
>>> >>> Short of compiling everything together, how can I have this method
>>> >>> generated
>>> >>> appropriately?
>>> >>
>>> >
>>> >
>>> > ---------------------------------------------------------------------
>>> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>> >
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>
>>
>
>