JDK-7080156 : LTP: Sometimes XMLEncoder does not encode public array fields
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-08-17
  • Updated: 2013-05-28
  • Resolved: 2013-05-28
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
  Public array fields of a class are not encoded by XMLEncoder if the class' constructor assigns an array instance of same length to the filed.

REGRESSION.  Last worked in version 6u26

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See attached source code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The field y should be encoded in the resulting xml:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0" class="java.beans.XMLDecoder">
 <object class="com.vipersystems.input.X" id="X0">
  <void class="com.vipersystems.input.X" method="getField">
   <string>y</string>
   <void method="set">
    <object idref="X0"/>
    <array class="java.lang.String" length="1">
     <void index="0">
      <string>a</string>
     </void>
    </array>
   </void>
  </void>
 </object>
</java>
ACTUAL -
The field is missing from output:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0" class="java.beans.XMLDecoder">
 <object class="com.vipersystems.input.X" id="X0"/>
</java>

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.beans.*;
import java.io.*;
import java.nio.file.*;

public class X
{
	public String[] y;

	public X()
	{
		y = new String[1];
	}

	public String[] getY()
	{
		return y;
	}

	public void setY(String[] y)
	{
		this.y = y;
	}

	public static void main(String[] args) throws IOException
	{
		X x = new X();
		x.setY(new String[]
		{
			"a"
		});

		Files.write(Paths.get("z.xml"), getXmlBytes(x));
	}

	private static byte[] getXmlBytes(Object o)
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		XMLEncoder xmle = new XMLEncoder(baos);
		xmle.writeObject(o);
		xmle.flush();
		xmle.close();
		return baos.toByteArray();
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
- don't use 'public'  (in this case the gettets and setters are used)
or
- don't assign the field in constructor

Comments
There is the same problem as described in the 8013557 issue.
28-05-2013

EVALUATION Seems that the root of cause is the same as in the 6989223 CR.
23-08-2011