C
Chris Riesbeck
My "gotcha" for the day... java.beans.XMLEncoder writes
a bean to a stream in XML form. But be careful if there
are static arrays! Here's what can go wrong.
First, a tiny class that "holds" an array of int's:
public class IntHolder {
private static final int[] initInts = { 0 };
private int[] myInts = initInts;
public int[] getInts() { return myInts; }
public void setInts(int[] ints) { myInts = ints; }
public String toString() {
return "myInts = [" + myInts[0] + "] initInts =[" + initInts[0] + "]";
}
}
Now some test code that creates an IntHolder, writes it
out in XML, and shows the arrays before and after:
import java.beans.*;
import java.io.*;
public class TestIntHolder {
public static void main(String[] args) throws IOException {
IntHolder h1 = new IntHolder();
h1.setInts(new int[]{ 3 });
System.out.println("h1: " + h1);
System.out.println("Writing h1");
XMLEncoder e = new XMLEncoder(new FileOutputStream("C:/temp/foo.xml"));
e.writeObject(h1);
e.close();
System.out.println("h1: " + h1);
}
}
Finally, the output (J2SDK 1.4.2):
h1: myInts = [3] initInts =[0] <-- initInts OK
Writing h1 <-- writing XML
h1: myInts = [3] initInts =[3] <-- initInts changed!
Note: If you make initInts non-static, this doesn't happen.
If you read the brief description of how XMLEncode works at
http://java.sun.com/products/jfc/tsc/articles/persistence4/#intro
you can probably guess at what's happening.
This seems like a bug to me -- encoding and writing should not
alter an object -- but it probably only breaks code that's asking
for trouble.
Anyone seen any discussion on this?
a bean to a stream in XML form. But be careful if there
are static arrays! Here's what can go wrong.
First, a tiny class that "holds" an array of int's:
public class IntHolder {
private static final int[] initInts = { 0 };
private int[] myInts = initInts;
public int[] getInts() { return myInts; }
public void setInts(int[] ints) { myInts = ints; }
public String toString() {
return "myInts = [" + myInts[0] + "] initInts =[" + initInts[0] + "]";
}
}
Now some test code that creates an IntHolder, writes it
out in XML, and shows the arrays before and after:
import java.beans.*;
import java.io.*;
public class TestIntHolder {
public static void main(String[] args) throws IOException {
IntHolder h1 = new IntHolder();
h1.setInts(new int[]{ 3 });
System.out.println("h1: " + h1);
System.out.println("Writing h1");
XMLEncoder e = new XMLEncoder(new FileOutputStream("C:/temp/foo.xml"));
e.writeObject(h1);
e.close();
System.out.println("h1: " + h1);
}
}
Finally, the output (J2SDK 1.4.2):
h1: myInts = [3] initInts =[0] <-- initInts OK
Writing h1 <-- writing XML
h1: myInts = [3] initInts =[3] <-- initInts changed!
Note: If you make initInts non-static, this doesn't happen.
If you read the brief description of how XMLEncode works at
http://java.sun.com/products/jfc/tsc/articles/persistence4/#intro
you can probably guess at what's happening.
This seems like a bug to me -- encoding and writing should not
alter an object -- but it probably only breaks code that's asking
for trouble.
Anyone seen any discussion on this?