jaxb
  1. jaxb
  2. JAXB-986

Using marshaller on xjc generated classes causes Exception. Works in jdk7, not in jdk 8.

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.2.8 (JDK 8)
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Windows 7
      java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b109)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b51, mixed mode)

      Description

      Using marshaller on xjc generated classes causes java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

      Works in jdk7, not in jdk 8.

      Java file generated by xjc in jdk8 is different than what is produced
      by xjc in jdk7.

      Schema:
      <?xml version="1.0"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:simpleType name="intlist">
      <xs:list itemType="xs:int"/>
      </xs:simpleType>
      <xs:complexType name="Person">
      <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="scores" type="intlist"/>
      </xs:sequence>
      </xs:complexType>
      <xs:element name="person" type="Person"/>
      </xs:schema>

      Difference in Person.java
      in jdk8:
      @XmlList
      @XmlElement(type = Integer.class)
      @XmlSchemaType(name = "anySimpleType")
      protected List<Integer> scores;

      in jdk 7:
      @XmlList
      @XmlElement(type = Integer.class)
      protected List<Integer> scores;

      REGRESSION : Additional Information
      java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b109)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b51, mixed mode)

      xjc 2.2.8-b20130806.1801

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile xsd to Java files:
      $ xjc.exe -p jaxb -d src test.xsd

      Compile test program and run.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected an XML file to be created with Person object.
      ACTUAL -
      Exception

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.ClassCastException: java.lang.Integer cannot be cast to
      java.lang.String

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package jaxb;

      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.JAXBElement;
      import javax.xml.bind.JAXBException;
      import javax.xml.bind.Marshaller;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.util.List;

      public class App {

      public static void main( String[] args ) throws Exception {

      File file = new File( "test-out.xml");
      FileOutputStream fis = new FileOutputStream( file );
      ObjectFactory of = new ObjectFactory();

      Person person = of.createPerson();
      person.setName( "Bob" );
      List<Integer> values = person.getScores();
      values.add( 1 );
      values.add( 2 );

      JAXBElement jaxbElement = of.createPerson( person );

      JAXBContext ctx = JAXBContext.newInstance( "jaxb" );
      try

      { Marshaller marshaller = ctx.createMarshaller(); // Dies here with: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String marshaller.marshal( jaxbElement, fis ); fis.close(); }

      catch( JAXBException e )

      { throw new RuntimeException( e ); }

      }
      }

        Activity

        Hide
        dkulp added a comment -

        This should be raised from Minor to Critical.

        This bug also exists in "JAXB RI v2.2.10-b140310.1920".

        This is preventing the WS-Discovery service in CXF from working properly as the WS-Discovery API's have a XmlList of QNames and trying to marshal the Discovery messages is causing ClassCast exceptions from QName to String.

        Show
        dkulp added a comment - This should be raised from Minor to Critical. This bug also exists in "JAXB RI v2.2.10-b140310.1920". This is preventing the WS-Discovery service in CXF from working properly as the WS-Discovery API's have a XmlList of QNames and trying to marshal the Discovery messages is causing ClassCast exceptions from QName to String.
        Hide
        jotel71 added a comment - - edited

        It's obvious regression and priority should be much higher then minor!

        The problem is when some of the elements references an extended simple type by name. The XJC adds @XmlSchemaType annotation to the generated classes which won't match actual type used. And having such classes is not possible to properly marshal it to the XML stream without class cast exception!

        Bug still exists in version 2.2.11

        Show
        jotel71 added a comment - - edited It's obvious regression and priority should be much higher then minor! The problem is when some of the elements references an extended simple type by name. The XJC adds @XmlSchemaType annotation to the generated classes which won't match actual type used. And having such classes is not possible to properly marshal it to the XML stream without class cast exception! Bug still exists in version 2.2.11
        Hide
        jotel71 added a comment -

        As a workaround (not nice but working) you can use annotate plugin and override wrongly produced annotation. Just add a similar constriction to your bindings:

         <jaxb:bindings node="//xs:element[@type='YourSimpleType']">
                <annox:annotate target="field">@javax.xml.bind.annotation.XmlSchemaType(name="YourSimpleType")</annox:annotate>
        </jaxb:bindings>
        
        Show
        jotel71 added a comment - As a workaround (not nice but working) you can use annotate plugin and override wrongly produced annotation. Just add a similar constriction to your bindings: <jaxb:bindings node= " //xs:element[@type='YourSimpleType']" > <annox:annotate target= "field" >@javax.xml.bind.annotation.XmlSchemaType(name= "YourSimpleType" )</annox:annotate> </jaxb:bindings>
        Hide
        dkulp added a comment -

        As another workaround, the CXF team created an xjc plugin that tried to detect this problem and removes the XmlSchemaType annotation:

        http://repo1.maven.org/maven2/org/apache/cxf/xjcplugins/cxf-xjc-bug986/3.0.3/

        I still cannot believe a bug this critical and has a reproducible test case has been open for more than a year.

        Show
        dkulp added a comment - As another workaround, the CXF team created an xjc plugin that tried to detect this problem and removes the XmlSchemaType annotation: http://repo1.maven.org/maven2/org/apache/cxf/xjcplugins/cxf-xjc-bug986/3.0.3/ I still cannot believe a bug this critical and has a reproducible test case has been open for more than a year.

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            rne11
          • Votes:
            8 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated: