harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r432196 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ test/java/org/apache/harmony/tests/java/lang/ test/resources/serialization/java/lang/
Date Thu, 17 Aug 2006 09:59:01 GMT
Author: pyang
Date: Thu Aug 17 02:59:01 2006
New Revision: 432196

URL: http://svn.apache.org/viewvc?rev=432196&view=rev
Log:
Patch applied for HARMONY-1163 ([classlib][luni] Two consecutive java.io.ObjectInputStream.readObject()
of enum type will trigger an unspecified StreamCorruptedException)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.4.ser
  (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java?rev=432196&r1=432195&r2=432196&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectInputStream.java
Thu Aug 17 02:59:01 2006
@@ -1536,12 +1536,22 @@
      */
     private ObjectStreamClass readEnumDesc() throws IOException,
             ClassNotFoundException {
-        ObjectStreamClass classDesc;
         byte tc = nextTC();
-        if (tc != TC_CLASSDESC) {
+        switch (tc) {
+        case TC_CLASSDESC:
+            return readEnumDescInternal(tc);            
+        case TC_REFERENCE:
+            return (ObjectStreamClass) readCyclicReference();
+        case TC_NULL:
+            return null;
+        default:
             throw new StreamCorruptedException(Msg.getString(
                     "K00d2", Integer.toHexString(tc & 0xff))); //$NON-NLS-1$
-        }
+        }        
+    }
+    
+    private ObjectStreamClass readEnumDescInternal(byte tc) throws IOException, ClassNotFoundException{
+        ObjectStreamClass classDesc;
         primitiveData = input;
         Integer oldHandle = descriptorHandle;
         descriptorHandle = new Integer(nextHandle());
@@ -1574,7 +1584,6 @@
         }
         return classDesc;
     }
-
     private Object readEnum(boolean unshared) throws OptionalDataException,
             ClassNotFoundException, IOException {
         // read classdesc for Enum first

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java?rev=432196&r1=432195&r2=432196&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectOutputStream.java
Thu Aug 17 02:59:01 2006
@@ -1496,7 +1496,11 @@
 		// The handle for the classDesc is NOT the handle for the
 		// class object being dumped. We must allocate a new handle and return
 		// it.
-		writeClassDesc(ObjectStreamClass.lookupStreamClass(object), unshared);
+        if (object.isEnum()){
+            writeEnumDesc(object, unshared);
+        } else {
+            writeClassDesc(ObjectStreamClass.lookupStreamClass(object), unshared);
+        }
 		
 		Integer previousHandle = objectsWritten.get(object);
 		Integer handle = registerObjectWritten(object);
@@ -1540,7 +1544,12 @@
 				flags |= SC_BLOCK_DATA;
 		}
 		output.writeByte(flags);
-		writeFieldDescriptors(classDesc, externalizable);
+        if (((byte)SC_ENUM | SC_SERIALIZABLE) != classDesc.getFlags()){
+            writeFieldDescriptors(classDesc, externalizable);
+        } else {
+            // enum write no fields
+            output.writeShort(0);    
+        }
 	}
 
 	/**
@@ -1973,7 +1982,15 @@
             drain(); // flush primitive types in the annotation
             output.writeByte(TC_ENDBLOCKDATA);
             // write super class
-            writeClassDesc(classDesc.getSuperclass(), unshared);
+            ObjectStreamClass superClass = classDesc.getSuperclass();            
+            if (null != superClass) {
+            	// super class is also enum
+                superClass.setFlags((byte)(SC_SERIALIZABLE|SC_ENUM));
+                superClass.setSerialVersionUID(0L);
+                writeEnumDesc(superClass.forClass(), unshared);
+            } else {
+                output.writeByte(TC_NULL);
+            }
             if (unshared) {
                 // remove reference to unshared object
                 removeUnsharedReference(classDesc, previousHandle);
@@ -1990,6 +2007,10 @@
         currentPutField = null;
 
         output.writeByte(TC_ENUM);
+        while (!theClass.isEnum() && null != theClass ){
+        	// write enum only
+            theClass = theClass.getSuperclass();
+        }
         ObjectStreamClass classDesc = writeEnumDesc(theClass, unshared);
 
         Integer previousHandle = objectsWritten.get(object);
@@ -1998,16 +2019,17 @@
         ObjectStreamField[] fields = classDesc.getSuperclass().fields();
         Class declaringClass = classDesc.getSuperclass().forClass();
         // Only write field "name" for enum class, which is the second field of
-        // enum
-        String str = (String) getFieldObj(object, declaringClass, fields[1]
-                .getName(), fields[1].getTypeString());
-
-        Integer strhandle = null;
-        if (!unshared) {
-            strhandle = dumpCycle(str);
-        }
-        if (null == strhandle) {
-            writeNewString(str, unshared);
+        // enum, that is fileds[1]. Ignore all non-fields and fields.length < 2
+        if (null != fields && fields.length > 1) {
+            String str = (String) getFieldObj(object, declaringClass, fields[1]
+                    .getName(), fields[1].getTypeString());
+            Integer strhandle = null;
+            if (!unshared) {
+                strhandle = dumpCycle(str);
+            }
+            if (null == strhandle) {
+                writeNewString(str, unshared);
+            }
         }
 
         if (unshared) {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java?rev=432196&r1=432195&r2=432196&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
Thu Aug 17 02:59:01 2006
@@ -16,8 +16,12 @@
 package org.apache.harmony.tests.java.lang;
 
 import java.io.IOException;
+import java.util.HashMap;
 
 import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+
 import tests.util.SerializationTester;
 
 public class EnumTest extends TestCase {
@@ -37,6 +41,10 @@
         UNUSED
     }   
     
+    enum Color {
+        Red, Green, Blue {};
+    }
+    
     /**
      * @tests java.lang.Enum#compareTo(java.lang.Enum) 
      */
@@ -239,5 +247,31 @@
         assertEquals(mock.i,test.i);
         assertEquals(mock.str,test.str);
         assertEquals(mock.samEnum,test.samEnum);
-    }    
+    }       
+    
+    /**
+     * test a map class that has enums.
+     * 
+     * @test Serialization/deserialization compatibility.
+     */
+    public void test_serialization_inMap() throws Exception {
+        // regression test for Harmony-1163
+        HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, 3);
+        SerializationTest.verifySelf(enumColorMap);
+    }
+    
+    /**
+     * test a map class that has enums.
+     * 
+     * @test Serialization/deserialization compatibility.
+     */
+    public void test_compatibilitySerialization_inMap() throws Exception {
+        // regression test for Harmony-1163
+        HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
+        enumColorMap.put(Color.Red, 1);
+        enumColorMap.put(Color.Blue, 3);        
+        assertTrue(SerializationTester.assertCompabilityEquals(enumColorMap,"serialization/java/lang/EnumTest.golden.4.ser"));
+    }
 }

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.4.ser
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.4.ser?rev=432196&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.4.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message