-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.4.2
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.4.2_12"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_12-b03)
Java HotSpot(TM) Client VM (build 1.4.2_12-b03, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Even though the initial/default values of the bean properties are null. By setting the property values that match the lazy getter method's output, XmlEncoder assumes that there is no change to the properties and hence the resulting output xml does not have values set by the user of the bean.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_12" class="java.beans.XMLDecoder">
<object class="com.uhc.ubh.service.bean.Bean">
<void property="flavor">
<string>MEDIUM ROAST</string>
</void>
<void property="type">
<string>COLUMBIAN</string>
</void>
</object>
</java>
ACTUAL -
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_12" class="java.beans.XMLDecoder">
<object class="com.uhc.ubh.service.bean.Bean"/>
</java>
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import com.uhc.ubh.service.util.Util;
public class Bean implements Serializable {
private static final String DEFAULT_FLAVOR = "MEDIUM ROAST";
private String type = null;
private String flavor = null;
public String getType() {
if (type == null) {
type = new BeanFactory().getType();
}
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFlavor() {
if (flavor == null) {
flavor = DEFAULT_FLAVOR;
}
return flavor;
}
public void setFlavor(String flavor) {
this.flavor = flavor;
}
public String toString() {
return getType() + " " + getFlavor();
}
public static class BeanFactory {
public String getType() {
return "COLUMBIAN";
}
public String getFlavor() {
return "MEDIUM ROAST";
}
}
public static void main(String[] args) {
Bean bean = new Bean();
bean.setType("COLUMBIAN");
bean.setFlavor("MEDIUM ROAST");
System.out.println(encodeAsXml(bean));
}
private static String encodeAsXml(Object bean) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder encoder = null;
try {
encoder = new XMLEncoder(
new BufferedOutputStream(out));
encoder.writeObject(bean);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (encoder != null) encoder.close();
}
return out.toString();
}
}
---------- END SOURCE ----------
java version "1.4.2_12"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_12-b03)
Java HotSpot(TM) Client VM (build 1.4.2_12-b03, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Even though the initial/default values of the bean properties are null. By setting the property values that match the lazy getter method's output, XmlEncoder assumes that there is no change to the properties and hence the resulting output xml does not have values set by the user of the bean.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_12" class="java.beans.XMLDecoder">
<object class="com.uhc.ubh.service.bean.Bean">
<void property="flavor">
<string>MEDIUM ROAST</string>
</void>
<void property="type">
<string>COLUMBIAN</string>
</void>
</object>
</java>
ACTUAL -
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_12" class="java.beans.XMLDecoder">
<object class="com.uhc.ubh.service.bean.Bean"/>
</java>
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import com.uhc.ubh.service.util.Util;
public class Bean implements Serializable {
private static final String DEFAULT_FLAVOR = "MEDIUM ROAST";
private String type = null;
private String flavor = null;
public String getType() {
if (type == null) {
type = new BeanFactory().getType();
}
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFlavor() {
if (flavor == null) {
flavor = DEFAULT_FLAVOR;
}
return flavor;
}
public void setFlavor(String flavor) {
this.flavor = flavor;
}
public String toString() {
return getType() + " " + getFlavor();
}
public static class BeanFactory {
public String getType() {
return "COLUMBIAN";
}
public String getFlavor() {
return "MEDIUM ROAST";
}
}
public static void main(String[] args) {
Bean bean = new Bean();
bean.setType("COLUMBIAN");
bean.setFlavor("MEDIUM ROAST");
System.out.println(encodeAsXml(bean));
}
private static String encodeAsXml(Object bean) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder encoder = null;
try {
encoder = new XMLEncoder(
new BufferedOutputStream(out));
encoder.writeObject(bean);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (encoder != null) encoder.close();
}
return out.toString();
}
}
---------- END SOURCE ----------
- relates to
-
JDK-6490731 LTP: XmlEncoder doesn't encode properties set via lazy initialization
-
- Closed
-
-
JDK-6490736 LTP: XmlEncoder causes side-effect when using lazy initialization getter code
-
- Closed
-