harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lian...@apache.org
Subject svn commit: r513229 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ObjectStreamClass.java main/java/java/io/ObjectStreamField.java test/java/tests/api/java/io/ObjectStreamFieldTest.java
Date Thu, 01 Mar 2007 08:49:03 GMT
Author: liangyx
Date: Thu Mar  1 00:49:02 2007
New Revision: 513229

URL: http://svn.apache.org/viewvc?view=rev&rev=513229
Log:
ObjectInputStream.readFields loses the unshared attribute when deserializing fields

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java?view=diff&rev=513229&r1=513228&r2=513229
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
Thu Mar  1 00:49:02 2007
@@ -314,7 +314,7 @@
         }
         fields = _fields;
     }
-
+    
     /**
      * Compute and return the Serial Version UID of the class <code>cl</code>.
      * The value is computed based on the class name, superclass chain, field
@@ -654,7 +654,33 @@
      *         represents
      */
     public ObjectStreamField[] getFields() {
+        copyFieldAttributes();
         return loadFields == null ? fields().clone() : loadFields.clone();
+    }
+
+    /**
+     * If a Class uses "serialPersistentFields" to define the serialized fields, 
+     * this.loadFields cannot get the "unshared" information when deserializing
+     * fields using current implementation of ObjectInputStream. This method 
+     * provides a way to copy the "unshared" attribute from this.fields.
+     *
+     */
+    private void copyFieldAttributes() {
+        if ((loadFields == null) || fields == null) {
+            return;
+        }
+        
+        for (int i = 0; i < loadFields.length; i++) {
+            ObjectStreamField loadField = loadFields[i];
+            String name = loadField.getName();
+            for (int j = 0; j < fields.length; j++) {
+                ObjectStreamField field = fields[j];
+                if (name.equals(field.getName())) {
+                    loadField.setUnshared(field.isUnshared());
+                    break;
+                }
+            }
+        }
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java?view=diff&rev=513229&r1=513228&r2=513229
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
Thu Mar  1 00:49:02 2007
@@ -342,4 +342,8 @@
     public boolean isUnshared() {
         return unshared;
     }
+    
+    void setUnshared(boolean unshared) {
+        this.unshared = unshared;
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java?view=diff&rev=513229&r1=513228&r2=513229
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java
Thu Mar  1 00:49:02 2007
@@ -19,6 +19,8 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.NotActiveException;
@@ -187,7 +189,30 @@
         assertEquals(Object.class, field.getType());
     }
     
-
+    public void test_isUnshared() throws Exception {
+        SerializableObject2 obj = new SerializableObject2();
+        
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(obj);
+        oos.close();
+        baos.close();
+        byte[] bytes = baos.toByteArray();
+        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        SerializableObject2 newObj = (SerializableObject2) ois.readObject();
+        
+        ObjectInputStream.GetField getField = newObj.getGetField();
+        ObjectStreamClass objectStreamClass = getField.getObjectStreamClass();
+        
+        assertTrue(objectStreamClass.getField("i").isUnshared());
+        assertFalse(objectStreamClass.getField("d").isUnshared());
+        assertTrue(objectStreamClass.getField("s").isUnshared());
+        
+        assertEquals(1000, getField.get("i", null));
+        assertEquals(SerializableObject2.today, getField.get("d", null));
+        assertEquals("Richard", getField.get("s", null));
+    }
 
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method
@@ -268,5 +293,37 @@
 
     public ObjectStreamClass getObjectStreamClass() {
         return temp;
+    }
+}
+
+class SerializableObject2 implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+    
+    private static final ObjectStreamField[] serialPersistentFields = {
+        new ObjectStreamField("i", Integer.class, true),
+        new ObjectStreamField("d", Date.class, false),
+        new ObjectStreamField("s", String.class, true),
+    };
+    
+    private ObjectInputStream.GetField getField;
+    
+    public static Date today = new Date(1172632429156l);
+    
+    public ObjectInputStream.GetField getGetField() {
+        return getField;
+    }
+    
+    private void writeObject(ObjectOutputStream o) throws IOException {
+        ObjectOutputStream.PutField putField = o.putFields();
+        putField.put("i", new Integer(1000));
+        putField.put("d", today);
+        putField.put("s", "Richard");
+        o.writeFields();
+    }
+
+    private void readObject(ObjectInputStream in) throws NotActiveException,
+            IOException, ClassNotFoundException {
+        getField = in.readFields();
     }
 }



Mime
View raw message