0

Migrating to Java8 and JAXB 2 adds @XmlSchemaType(name = "anySimpleType") annotation for referred types

I started facing one strange issue, once my project got upgraded to Java 8 and JAXB 2. I had list of enums, doubles as part of my XSD. I used to define them in following manner.

<xs:complexType name="TOCForResultCType">
    <xs:sequence>
        <xs:element name="SupportedPPType" minOccurs="0" type="PPType">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:simpleType name="PPType">
    <xs:list itemType="PostProcessingTypeSType"/>
</xs:simpleType>

<xs:simpleType name="PostProcessingTypeSType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="ANIMATION"/>
        <xs:enumeration value="NONE"/>
    </xs:restriction>
</xs:simpleType>

After migration to java8 and jaxb 2, it generated code like this

public class TOCForResultCType {

@XmlList
@XmlElement(name = "SupportedPPType")
@XmlSchemaType(name = "anySimpleType")
protected List<PostProcessingTypeSType> supportedPPType;

Here one can see this extra annotation @XmlSchemaType(name = "anySimpleType"). This extra annotation creates ClassCastException when trying to marshall the content using JAXB Marshaller.

This issue was happening when we were creating list of object using simpleType annotation. I also faced this issue when trying to create List in the same fashion.

Harbeer Kadian
  • 364
  • 2
  • 14

1 Answers1

0

So I have already found solution for this problem, and I am adding here as it can help some other struck programmer.

This solution is just workaround. AS i listed in the question, the source of problem is extra annotation @XmlSchemaType(name = "anySimpleType"). This annotation does not used to come when I was using Java 5 with JAXB 1. If while parsing XSD and generating JAXB Classes out of it, if we can prevent this annotation, it will solve the problem.

The way is to define the simple types as inline rather that referred types.

Here is the sample XSD

<xs:complexType name="TOCForResultCType">
    <xs:sequence>
        <xs:element name="SupportedPPType" minOccurs="0">
            <xs:simpleType>
                <xs:list itemType="PostProcessingTypeSType" />
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:simpleType name="PostProcessingTypeSType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="ANIMATION" />
        <xs:enumeration value="NONE" />
    </xs:restriction>
</xs:simpleType>

and the generated JAXB class will look like this.

public class TOCForResultCType {

@XmlList
@XmlElement(name = "SupportedPPType")
protected List<PostProcessingTypeSType> supportedPPType;

This solution was already listed in the following question jaxb: strange class cast exception on enum list

So once this extra annotation is gone, no classcastexception will come in the code. But was hard to understand for me, hence I am again posting Q/A for this.

Community
  • 1
  • 1
Harbeer Kadian
  • 364
  • 2
  • 14