JDK-8013557 : XMLEncoder in 1.7 can't encode objects initialized in no argument constructor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 7u13,8
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2013-04-04
  • Updated: 2014-11-17
  • Resolved: 2013-05-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u40Fixed 8 b94Fixed
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_13 " 
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux silent 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
The behavior of XMLEncoder changed in Java 1.7 such that it is no longer compatible with any class that initializes the values of objects inside its no argument constructor.  Objects of primitive types (e.g. Integer) seem to work OK, but more complex objects it doesn't work.  Note that all the code works just fine in Java 1.6.  This bug is bad enough that XMLEncoder is no longer usable in 1.7 and I'm not aware of any work around.

-------- Serialization Code -------------

Hack o = new Hack(new Vector3D_F64(1,2,3));
XMLEncoder encoder = null;
try {
encoder = new XMLEncoder(
new FileOutputStream(fileName)
);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}

encoder.writeObject(o);
encoder.close();

-------- Class To Be Serialized --------------

public class Hack implements Serializable {
public Vector3D_F64 value;

public Hack() {
// Serialization works when the line below is commented out
value = new Vector3D_F64();
}

public Hack(Vector3D_F64 value) {
this.value = value;
}

public Vector3D_F64 getValue() {
return value;
}

public void setValue(Vector3D_F64 value) {
this.value = value;
}
} 

-------------- Broken 1.7 Output ---------------
<?xml version= " 1.0 "  encoding= " UTF-8 " ?>
<java version= " 1.7.0_13 "  class= " java.beans.XMLDecoder " >
 <object class= " boofcv.examples.Hack "  id= " Hack0 " />
</java>

------------- Output when the line above is commented out ---------------------------

<?xml version= " 1.0 "  encoding= " UTF-8 " ?>
<java version= " 1.7.0_13 "  class= " java.beans.XMLDecoder " >
 <object class= " boofcv.examples.Hack "  id= " Hack0 " >
  <void class= " boofcv.examples.Hack "  method= " getField " >
   <string>value</string>
   <void method= " set " >
    <object idref= " Hack0 " />
    <object class= " georegression.struct.point.Vector3D_F64 "  id= " Vector3D_F640 " >
     <void class= " georegression.struct.GeoTuple3D_F64 "  method= " getField " >
      <string>x</string>
      <void method= " set " >
       <object idref= " Vector3D_F640 " />
       <double>1.0</double>
      </void>
     </void>
     <void class= " georegression.struct.GeoTuple3D_F64 "  method= " getField " >
      <string>y</string>
      <void method= " set " >
       <object idref= " Vector3D_F640 " />
       <double>2.0</double>
      </void>
     </void>
     <void class= " georegression.struct.GeoTuple3D_F64 "  method= " getField " >
      <string>z</string>
      <void method= " set " >
       <object idref= " Vector3D_F640 " />
       <double>3.0</double>
      </void>
     </void>
    </object>
   </void>
  </void>
 </object>
</java>

REGRESSION.  Last worked in version 6u31


REPRODUCIBILITY :
This bug can be reproduced always.