JDK-4835940 : LTP: Serialized form of bean is invalid
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-03-21
  • Updated: 2003-03-24
  • Resolved: 2003-03-24
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 03/21/2003


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Serialized form of the bean is corrupt and does not
correspond to the actual bean state. It cannot be deserialized
back.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Compile&Run attached class
2.
3.

EXPECTED VERSUS ACTUAL BEHAVIOR :
The serialized form of the bean contains broken object
references. Serialized object is below:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0" class="java.beans.XMLDecoder">
 <object class="Test">
  <void property="table">
   <void method="put">
    <void class="Test">
     <void property="table">
      <void method="put">
       <object idref="Font0"/>
       <string>qqqqqqqqqqqq</string>
      </void>
     </void>
     <void id="Font0" property="key"/>
    </void>
    <object idref="Font0"/>
    <string>qqqqqqqqqqqq</string>
   </void>
  </void>
  <void id="Font0" property="key"/>
 </object>
</java>


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.Exception: Unbound variable: Font0
Continuing ...
java.lang.NullPointerException
Continuing ...



This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.util.HashMap;
import java.util.Arrays;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.*;
import java.awt.Font;

public class Test {
	private Font key = new Font("Arial", Font.BOLD, 12);  //  !!!
	private HashMap table = new HashMap();

	public Test() {
	}

	public Font getKey() {
		return key;
	}

	public void setKey(Font key) {
		this.key = key;
	}

	public HashMap getTable() {
		return table;
	}

	public void setTable(HashMap table) {
		this.table = table;
	}

	public void setString(String value) {
		table.put(getKey(), value);
	}

	public static void main(String[] args) {
		try {
			Test lr = new Test();
			lr.setString("qqqqqqqqqqqq");
			write(lr, new FileOutputStream("test.xml"));
			read(new FileInputStream("test.xml"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void write(Object obj, OutputStream out) throws
IOException {
		BufferedOutputStream bout = new BufferedOutputStream
(out);
		XMLEncoder encoder = new XMLEncoder(out);
		encoder.writeObject(obj);
		encoder.close();
		bout.close();
	}

	public static Object read(InputStream in) throws IOException {
		XMLDecoder decoder = new XMLDecoder(in);
		Object result = decoder.readObject();
		decoder.close();
		return result;
	}

}

---------- END SOURCE ----------
(Review ID: 146830) 
======================================================================

Comments
WORK AROUND Add the getString() method to the Bean.
11-06-2004

EVALUATION The XMLDecoder attempts to reconstruct the value of the string property but the getString method does not exist. So the xml decoder attempts to reconstruct this property from the side effects on the HashMap. It appears that the object graph gets confused. This bug has been fixed in 1.4.2 under 4679556. ###@###.### 2003-03-24
24-03-2003