harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r418009 - 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, 29 Jun 2006 11:15:49 GMT
Author: tellison
Date: Thu Jun 29 04:15:47 2006
New Revision: 418009

URL: http://svn.apache.org/viewvc?rev=418009&view=rev
Log:
Apply patch for HARMONY-592 (java.lang.Enum does not deserialize correctly)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.Harmony.ser
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.1.ser
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.2.ser
  (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/lang/EnumTest.golden.3.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=418009&r1=418008&r2=418009&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 Jun 29 04:15:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -787,6 +787,8 @@
                 return readNewString(unshared);
             case TC_LONGSTRING:
                 return readNewLongString(unshared);
+            case TC_ENUM:
+                return readEnum(unshared);
             case TC_REFERENCE:
                 if (unshared) {
                     readNewHandle();
@@ -1239,7 +1241,7 @@
             }
             int lastIndex = 0;
             for (int i = 0; i < classList.size(); i++) {
-                Class<?> superclass = (Class) classList.get(i);
+                Class<?> superclass = classList.get(i);
                 int index = findStreamSuperclass(superclass, streamClassList,
                         lastIndex);
                 if (index == -1) {
@@ -1247,7 +1249,7 @@
                 } else {
                     for (int j = lastIndex; j <= index; j++) {
                         readObjectForClass(object,
-                                (ObjectStreamClass) streamClassList.get(j));
+                                streamClassList.get(j));
                     }
                     lastIndex = index + 1;
                 }
@@ -1491,7 +1493,82 @@
         }
         throw new InvalidClassException(Msg.getString("K00d1")); //$NON-NLS-1$
     }
+    
+    /*
+     * read class type for Enum, note there's difference between enum and
+     * normal classes
+     */
+    private ObjectStreamClass readEnumDesc() throws IOException,
+            ClassNotFoundException {
+        ObjectStreamClass classDesc;     
+        byte tc = nextTC();
+        if (tc != TC_CLASSDESC) {
+            throw new StreamCorruptedException(Msg.getString(
+                    "K00d2", Integer.toHexString(tc & 0xff))); //$NON-NLS-1$
+        }
+        primitiveData = input;
+        Integer oldHandle = descriptorHandle;
+        descriptorHandle = new Integer(nextHandle());
+        classDesc = readClassDescriptor();
+        if (descriptorHandle != null) {
+            registerObjectRead(classDesc, descriptorHandle);
+        }
+        descriptorHandle = oldHandle;
+        primitiveData = emptyStream;
+        classDesc.setClass(resolveClass(classDesc));
+        // Consume unread class annotation data and TC_ENDBLOCKDATA
+        discardData();
+        ObjectStreamClass superClass = readClassDesc();
+        classDesc.setSuperclass(superClass);
+        // Check SUIDs, note all SUID for Enum is 0L
+        if (0L != classDesc.getSerialVersionUID()
+                || 0L != superClass.getSerialVersionUID()) {
+            throw new InvalidClassException(superClass.getName(), Msg
+                    .getString("K00da", superClass, //$NON-NLS-1$
+                            superClass));
+        }        
+        tc = nextTC();
+        // discard TC_ENDBLOCKDATA after classDesc if any
+        if (tc == TC_ENDBLOCKDATA) {
+            // read next parent class. For enum, it may be null
+            superClass.setSuperclass(readClassDesc());
+        } else {
+            // not TC_ENDBLOCKDATA, push back for next read
+            pushbackTC();
+        }
+        return classDesc;
+    }
+
+    private Object readEnum(boolean unshared) throws OptionalDataException,
+            ClassNotFoundException, IOException {
+        // read classdesc for Enum first
+        ObjectStreamClass classDesc = readEnumDesc();
+        Integer newHandle = new Integer(nextHandle());
+        // read name after class desc
+        String name;
+        byte tc = nextTC();
+        switch (tc) {
+        case TC_REFERENCE:
+            if (unshared) {
+                readNewHandle();
+                throw new InvalidObjectException(Msg.getString("KA002")); //$NON-NLS-1$
+            }
+            name = (String) readCyclicReference();
+            break;
+        case TC_STRING:
+            name = (String) readNewString(unshared);
+            break;
+        default:
+            throw new StreamCorruptedException(Msg.getString("K00d2"));//$NON-NLS-1$
+        }
 
+        Enum result = Enum.valueOf((Class)classDesc.forClass(), name);
+        if (!unshared) {
+            registerObjectRead(result, newHandle);
+        }
+        return result;
+    }
+    
     /**
      * Reads a new class descriptor from the receiver. It is assumed the class
      * descriptor has not been read yet (not a cyclic reference). Return the

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=418009&r1=418008&r2=418009&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 Jun 29 04:15:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -1924,6 +1924,10 @@
 				return writeNewArray(object, objClass, objClass
 						.getComponentType(), unshared);
 
+            if (object instanceof Enum){
+                return writeNewEnum(object, objClass, unshared);
+            }
+            
 			// Not a String or Class or Array. Default procedure.
 			return writeNewObject(object, objClass, unshared);
 		} finally {
@@ -1931,16 +1935,96 @@
 		}
 	}
 
-	/**
-	 * Method to be overriden by subclasses to write <code>object</code> into
-	 * the receiver's underlying stream.
-	 * 
-	 * @param object
-	 *            the object
-	 * 
-	 * @throws IOException
-	 *             If an IO exception happened when writing the object
-	 */
+    // write for Enum Class Desc only, which is different from other classes
+    private ObjectStreamClass writeEnumDesc(Class theClass, boolean unshared)
+            throws IOException {
+        // write classDesc, classDesc for enum is different
+        ObjectStreamClass classDesc = ObjectStreamClass.lookup(theClass);
+        // SUID of enum is 0L
+        classDesc.setSerialVersionUID(0L);
+        // set flag for enum, the flag is (SC_SERIALIZABLE | SC_ENUM)
+        classDesc.setFlags((byte)(SC_SERIALIZABLE|SC_ENUM));
+        Integer previousHandle = (Integer) objectsWritten.get(classDesc);
+        Integer handle = null;
+        if (!unshared) {
+            handle = dumpCycle(classDesc);
+        }
+        if (handle == null) {
+            Class classToWrite = classDesc.forClass();
+            // If we got here, it is a new (non-null) classDesc that will have
+            // to be registered as well
+            registerObjectWritten(classDesc);
+
+            output.writeByte(TC_CLASSDESC);
+            if (protocolVersion == PROTOCOL_VERSION_1)
+                writeNewClassDesc(classDesc);
+            else {
+                // So write...() methods can be used by
+                // subclasses during writeClassDescriptor()
+                primitiveTypes = output;
+                writeClassDescriptor(classDesc);
+                primitiveTypes = null;
+            }
+            // Extra class info (optional)
+            annotateClass(classToWrite);
+            drain(); // flush primitive types in the annotation
+            output.writeByte(TC_ENDBLOCKDATA);
+            // write super class
+            writeClassDesc(classDesc.getSuperclass(), unshared);
+            if (unshared) {
+                // remove reference to unshared object
+                removeUnsharedReference(classDesc, previousHandle);
+            }
+        }
+        return classDesc;
+    }
+
+    private Integer writeNewEnum(Object object, Class theClass, boolean unshared)
+            throws IOException {
+        // write new Enum
+        EmulatedFieldsForDumping originalCurrentPutField = currentPutField; // save
+        // null it, to make sure one will be computed if needed
+        currentPutField = null;
+
+        output.writeByte(TC_ENUM);
+        ObjectStreamClass classDesc = writeEnumDesc(theClass, unshared);
+
+        Integer previousHandle = (Integer) objectsWritten.get(object);
+        Integer handle = registerObjectWritten(object);        
+
+        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);
+        }
+
+        if (unshared) {
+            // remove reference to unshared object
+            removeUnsharedReference(object, previousHandle);
+        }
+        currentPutField = originalCurrentPutField;
+        return handle;
+    }
+
+    /**
+     * Method to be overriden by subclasses to write <code>object</code> into
+     * the receiver's underlying stream.
+     * 
+     * @param object
+     *            the object
+     * 
+     * @throws IOException
+     *             If an IO exception happened when writing the object
+     */
 	protected void writeObjectOverride(Object object) throws IOException {
 		// Subclasses must override.
 		throw new IOException();

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=418009&r1=418008&r2=418009&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 Jun 29 04:15:47 2006
@@ -15,7 +15,10 @@
 
 package org.apache.harmony.tests.java.lang;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
+import tests.util.SerializationTester;
 
 public class EnumTest extends TestCase {
 
@@ -32,7 +35,7 @@
     
     enum Bogus {
         UNUSED
-    }
+    }   
     
     /**
      * @tests java.lang.Enum#compareTo(java.lang.Enum) 
@@ -169,4 +172,72 @@
         
         assertEquals(0, Empty.values().length);
     }
+    
+    /**
+     * @test Serialization/deserilazation compatibility with RI.
+     */
+    public void test_compatibilitySerialization() throws Exception{
+        assertTrue(SerializationTester.assertCompabilityEquals(Sample.CURLY,"serialization/java/lang/EnumTest.golden.1.ser"));
+    }
+    
+    /**
+     * @test Serialization/deserilazation compatibility with RI.
+     */
+    public void test_compatibilitySerialization_inClass() throws Exception{
+        assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum(),"serialization/java/lang/EnumTest.golden.2.ser"));
+    }
+    
+    /**
+     * @test Serialization/deserilazation compatibility.
+     */
+    public void test_serialization() throws IOException, ClassNotFoundException{
+        Sample object = Sample.CURLY;
+        Sample deObject = (Sample) SerializationTester
+                .getDeserilizedObject(object);
+        assertEquals(object, deObject);
+    }
+    
+    /**
+     * test a class that has enums as its fields.
+     * 
+     * @test Serialization/deserilazation compatibility.
+     */
+    public void test_serialization_inClass() throws IOException, ClassNotFoundException{
+        MockEnum mock = new MockEnum();
+        MockEnum test = (MockEnum) SerializationTester
+                .getDeserilizedObject(mock);
+        assertEquals(mock.i,test.i);
+        assertEquals(mock.str,test.str);
+        assertEquals(mock.samEnum,test.samEnum);
+    }
+    
+    /**
+     * test a class that has enums and a string of same name as its fields.
+     * 
+     * @test Serialization/deserilazation compatibility with RI.
+     */
+    public void test_compatibilitySerialization_inClass_Complex() throws Exception{
+        assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum2(),"serialization/java/lang/EnumTest.golden.3.ser"));
+    }
+    
+    /**
+     * @test Serialization/deserilazation compatibility with Harmony.
+     */
+    public void test_compatibilitySerialization_inClass_Complex_Harmony() throws Exception{
+        assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum2(),"serialization/java/lang/EnumTest.Harmony.ser"));
+    }
+    
+    /**
+     * test a class that has enums and a string of same name as its fields.
+     * 
+     * @test Serialization/deserilazation compatibility.
+     */
+    public void test_serialization_inClass_Complex() throws Exception{
+        MockEnum2 mock = new MockEnum2();
+        MockEnum2 test = (MockEnum2) SerializationTester
+                .getDeserilizedObject(mock);
+        assertEquals(mock.i,test.i);
+        assertEquals(mock.str,test.str);
+        assertEquals(mock.samEnum,test.samEnum);
+    }    
 }

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java?rev=418009&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java
Thu Jun 29 04:15:47 2006
@@ -0,0 +1,56 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ */
+
+package org.apache.harmony.tests.java.lang;
+
+import java.io.Serializable;
+
+class MockEnum implements Serializable {
+    private static final long serialVersionUID = -1678507713086705252L;
+
+    enum Sample {
+        LARRY, MOE, CURLY
+    }
+
+    enum Sample2 {
+        RED, BLUE, YELLO
+    }
+
+    String str;
+
+    int i;
+
+    Sample2 samEnum;
+
+    Sample larry = Sample.LARRY;
+
+    MockEnum() {
+        str = "test";
+        i = 99;
+        samEnum = Sample2.BLUE;
+    }
+
+    public boolean equals(Object arg0) {
+        if (!(arg0 instanceof MockEnum)) {
+            return false;
+        }
+        MockEnum test = (MockEnum) arg0;
+        if (str.equals(test.str) && i == test.i && samEnum == test.samEnum)
{
+            return true;
+        }
+        return false;
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java?rev=418009&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java
Thu Jun 29 04:15:47 2006
@@ -0,0 +1,60 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.
+ */
+
+package org.apache.harmony.tests.java.lang;
+
+import java.io.Serializable;
+
+class MockEnum2 implements Serializable {
+
+    private static final long serialVersionUID = -4812214670022262730L;
+
+    enum Sample {
+        LARRY, MOE, CURLY
+    }
+
+    enum Sample2 {
+        RED, BLUE, YELLO
+    }
+
+    String str;
+
+    int i;
+
+    Sample samEnum;
+
+    Sample larry = Sample.LARRY;
+
+    String myStr = "LARRY";
+
+    MockEnum2() {
+        str = "test";
+        i = 99;
+        samEnum = larry;
+    }
+
+    public boolean equals(Object arg0) {
+        if (!(arg0 instanceof MockEnum2)) {
+            return false;
+        }
+        MockEnum2 test = (MockEnum2) arg0;
+        if (str.equals(test.str) && i == test.i && samEnum == test.samEnum
+                && myStr.equals(test.myStr)) {
+            return true;
+        }
+        return false;
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/MockEnum2.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

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

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

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

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

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

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



Mime
View raw message