harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r931856 - in /harmony/enhanced/java/trunk/classlib/modules/beans/src: main/java/java/beans/XMLEncoder.java test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java test/resources/xml/ObjectID.xml
Date Thu, 08 Apr 2010 10:33:29 GMT
Author: zhoukevin
Date: Thu Apr  8 10:33:29 2010
New Revision: 931856

URL: http://svn.apache.org/viewvc?rev=931856&view=rev
Log:
Previously, java.beans.XMLEncoder has a field "idSerialNo=0" to generate ID for referred (refCount
> 1) object. No matter what type of object it is, its ID is suffixed with a number counted
based on the single idSerialNo field. But according to the enclosed test case, different classes
should have separate number counter. This patch fix this problem and add a test case for recreation.

Added:
    harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ObjectID.xml
Modified:
    harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
    harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java?rev=931856&r1=931855&r2=931856&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/main/java/java/beans/XMLEncoder.java
Thu Apr  8 10:33:29 2010
@@ -77,8 +77,6 @@ public class XMLEncoder extends Encoder 
 
 	private boolean hasXmlHeader = false;
 
-	private int idSerialNo = 0;
-
 	/*
 	 * if any expression or statement references owner, it is set true in method
 	 * recordStatement() or recordExpressions(), and, at the first time
@@ -95,7 +93,9 @@ public class XMLEncoder extends Encoder 
 
 	private ReferenceMap records = new ReferenceMap();
 
-    private ReferenceMap cache = new ReferenceMap();
+    private ReferenceMap objPrePendingCache = new ReferenceMap();
+
+    private ReferenceMap clazzCounterMap = new ReferenceMap();
 
 	private boolean writingObject = false;
 
@@ -133,6 +133,15 @@ public class XMLEncoder extends Encoder 
 		return buf;
 	}
 
+    private String idSerialNoOfObject(Object obj) {
+        Class<?> clazz = obj.getClass();
+        Integer serialNo = (Integer) clazzCounterMap.get(clazz);
+        serialNo = serialNo == null ? 0 : serialNo;
+        String id = nameForClass(obj.getClass()) + serialNo;
+        clazzCounterMap.put(clazz, ++serialNo);
+        return id;
+    }
+
 	/**
 	 * Writes out all objects since last flush to the output stream.
 	 * <p>
@@ -171,8 +180,9 @@ public class XMLEncoder extends Encoder 
 
 			// clear statement records
 			records.clear();
-			flushPendingStat.clear();
-            cache.clear();
+            flushPendingStat.clear();
+            objPrePendingCache.clear();
+            clazzCounterMap.clear();
 
 			// remove all old->new mappings
 			super.clear();
@@ -264,9 +274,8 @@ public class XMLEncoder extends Encoder 
         }
 
         // generate id, if necessary
-        if (rec.refCount > 1) {
-            rec.id = nameForClass(obj.getClass()) + idSerialNo;
-            idSerialNo++;
+        if (rec.refCount > 1 && rec.id == null) {
+            rec.id = idSerialNoOfObject(obj);
         }
 
         // flush
@@ -303,10 +312,9 @@ public class XMLEncoder extends Encoder 
 
 	@SuppressWarnings("nls")
     private void flushOwner(Object obj, Record rec, int indent) {
-		if (rec.refCount > 1) {
-			rec.id = nameForClass(obj.getClass()) + idSerialNo;
-			idSerialNo++;
-		}
+        if (rec.refCount > 1 && rec.id == null) {
+            rec.id = idSerialNoOfObject(obj);
+        }
 
 		flushIndent(indent);
 		String tagName = "void";
@@ -920,7 +928,8 @@ public class XMLEncoder extends Encoder 
     @Override
     public void writeObject(Object o) {
         synchronized (this) {
-            ArrayList<Object> prePending = (ArrayList<Object>) cache.get(o);
+            ArrayList<Object> prePending = (ArrayList<Object>) objPrePendingCache
+                    .get(o);
             if (prePending == null) {
                 boolean oldWritingObject = writingObject;
                 writingObject = true;
@@ -941,7 +950,7 @@ public class XMLEncoder extends Encoder 
                 if (isNotCached && o != null) {
                     prePending = new ArrayList<Object>();
                     prePending.addAll(flushPrePending);
-                    cache.put(o, prePending);
+                    objPrePendingCache.put(o, prePending);
                 }
 
                 // add to pending

Modified: harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java?rev=931856&r1=931855&r2=931856&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
Thu Apr  8 10:33:29 2010
@@ -930,4 +930,96 @@ public class XMLEncoderTest extends Test
 
     public static class ChildClass extends ParentClass {
     }
+
+    public void testWriteObject_ObjectID() throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        XMLEncoder xmlEncoder = new XMLEncoder(bos);
+        ExampleA exampleAI = new ExampleA("exampleAI");
+        xmlEncoder.writeObject(exampleAI);
+        xmlEncoder.writeObject(exampleAI);
+        ExampleA exampleAII = new ExampleA("exampleAI");
+        xmlEncoder.writeObject(exampleAII);
+        xmlEncoder.writeObject(exampleAII);
+
+        ExampleB exampleBI = new ExampleB("exampleBI");
+        xmlEncoder.writeObject(exampleBI);
+        xmlEncoder.writeObject(exampleBI);
+        ExampleB exampleBII = new ExampleB("exampleBII");
+        xmlEncoder.writeObject(exampleBII);
+        xmlEncoder.writeObject(exampleBII);
+
+        ExampleC exampleCI = new ExampleC("exampleCI");
+        xmlEncoder.writeObject(exampleCI);
+        xmlEncoder.writeObject(exampleCI);
+        ExampleC exampleCII = new ExampleC("exampleCII");
+        xmlEncoder.writeObject(exampleCII);
+        xmlEncoder.writeObject(exampleCII);
+
+        xmlEncoder.close();
+
+        assertXMLContent(null, bos.toByteArray(), "/xml/ObjectID.xml");
+    }
+
+    public static class ExampleA {
+
+        private String name;
+
+        public ExampleA() {
+
+        }
+
+        public ExampleA(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    public static class ExampleB {
+
+        private String name;
+
+        public ExampleB() {
+
+        }
+
+        public ExampleB(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    public static class ExampleC {
+
+        private String name;
+
+        public ExampleC() {
+
+        }
+
+        public ExampleC(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
 }
\ No newline at end of file

Added: harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ObjectID.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ObjectID.xml?rev=931856&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ObjectID.xml
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/beans/src/test/resources/xml/ObjectID.xml
Thu Apr  8 10:33:29 2010
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<java version="${version}" class="java.beans.XMLDecoder"> 
+ <object id="XMLEncoderTest$ExampleA0" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleA">

+  <void property="name"> 
+   <string>exampleAI</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleA0"/> 
+ <object id="XMLEncoderTest$ExampleA1" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleA">

+  <void property="name"> 
+   <string>exampleAI</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleA1"/> 
+ <object id="XMLEncoderTest$ExampleB0" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleB">

+  <void property="name"> 
+   <string>exampleBI</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleB0"/> 
+ <object id="XMLEncoderTest$ExampleB1" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleB">

+  <void property="name"> 
+   <string>exampleBII</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleB1"/> 
+ <object id="XMLEncoderTest$ExampleC0" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleC">

+  <void property="name"> 
+   <string>exampleCI</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleC0"/> 
+ <object id="XMLEncoderTest$ExampleC1" class="org.apache.harmony.beans.tests.java.beans.XMLEncoderTest$ExampleC">

+  <void property="name"> 
+   <string>exampleCII</string> 
+  </void> 
+ </object> 
+ <object idref="XMLEncoderTest$ExampleC1"/> 
+</java> 
+



Mime
View raw message