Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6490736

LTP: XmlEncoder causes side-effect when using lazy initialization getter code

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs
    • 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 :
      Unlike java bean byte code serialization with ObjectOutputStream, XmlEncoder has a side-effect to the original bean when the getter method uses lazy init code. Infact the getter methods are invoked twice during the encoding. However, the xml output does look like expected.

      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"/>
      </java>

      After calling XmlEncoder
      Bean type = null, flavor = null
      ACTUAL -
      Lazily fetching flavor...
      Lazily fetching flavor...
      Lazily fetching type...
      Lazily fetching type...
      <?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>

      After calling XmlEncoder...
      Bean type = COLUMBIAN, flavor = MEDIUM ROAST


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.uhc.ubh.service.bean;

      import java.beans.XMLEncoder;
      import java.io.BufferedOutputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.Serializable;

      public class Bean implements Serializable {
        
        private static final String DEFAULT_FLAVOR = "MEDIUM ROAST";
        
        //Make the following bean properties scope as public to demonstrate side-effect.
        public String type = null;
        public String flavor = null;
        
        public String getType() {
          if (type == null) {
            System.out.println("Lazily fetching type...");
            type = new BeanFactory().getType();
          }
          return type;
        }

        public void setType(String type) {
          this.type = type;
        }
        
        public String getFlavor() {
          if (flavor == null) {
            System.out.println("Lazily fetching flavor...");
            flavor = DEFAULT_FLAVOR;
          }
          return flavor;
        }
        
        public void setFlavor(String flavor) {
          this.flavor = flavor;
        }
        
        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();
          System.out.println(encodeAsXml(bean));

          //Caused
          System.out.println("After calling XmlEncoder...");
          System.out.println("Bean type = " + bean.type + ", flavor = " + " " + bean.flavor);
          
        }
        
        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 ----------

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: