You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/25 09:45:22 UTC

svn commit: r436697 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ test/java/tests/api/java/io/

Author: pyang
Date: Fri Aug 25 00:45:21 2006
New Revision: 436697

URL: http://svn.apache.org/viewvc?rev=436697&view=rev
Log:
Patch applied for HARMONY-1261 ([classlib][luni] ObjectStreamField.getType() method should return Object.class when this ObjectStreamField was obtained from a deserialized ObjectStreamClass instance)

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/ObjectStreamClass.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.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=436697&r1=436696&r2=436697&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 Fri Aug 25 00:45:21 2006
@@ -942,11 +942,20 @@
             if (isPrimType) {
                 classSig = String.valueOf(typecode);
             } else {
-                // The spec says it is a UTF, but experience shows they dump
-                // this String using writeObject (unlike the field name, which
-                // is saved with writeUTF)
-                classSig = (String) readObject();
-            }
+				// The spec says it is a UTF, but experience shows they dump
+				// this String using writeObject (unlike the field name, which
+				// is saved with writeUTF).
+                // And if resolveObject is enabled, the classSig may be modified 
+                // so that the original class descriptor cannot be read properly,
+                // so it is disabled.
+				boolean old = enableResolve;
+				try {
+					enableResolve = false;
+					classSig = (String) readObject();
+				} finally {
+					enableResolve = old;
+				}
+			}
             ObjectStreamField f = new ObjectStreamField(classSig, fieldName);
             fields[i] = f;
         }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java?rev=436697&r1=436696&r2=436697&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamClass.java Fri Aug 25 00:45:21 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.
@@ -601,7 +601,7 @@
 	 */
 
 	public ObjectStreamField getField(String name) {
-		ObjectStreamField[] allFields = fields();
+		ObjectStreamField[] allFields = getFields();
 		for (int i = 0; i < allFields.length; i++) {
 			ObjectStreamField f = allFields[i];
 			if (f.getName().equals(name)) {
@@ -641,7 +641,7 @@
 	 */
 
 	public ObjectStreamField[] getFields() {
-		return fields().clone();
+		return loadFields == null ? fields().clone() : loadFields.clone();
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java?rev=436697&r1=436696&r2=436697&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/ObjectStreamField.java Fri Aug 25 00:45:21 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.
@@ -43,6 +43,8 @@
 	private String typeString;
 
 	private boolean unshared;
+    
+    private boolean isDeserialized;
 
 	/**
 	 * Constructs an ObjectStreamField with the given name and the given type
@@ -95,6 +97,7 @@
 		}
 		this.name = name;
 		this.typeString = signature.replace('.', '/');
+        this.isDeserialized = true;
 	}
 
 	/**
@@ -142,16 +145,30 @@
 	}
 
 	/**
-	 * Return the type of the field the receiver represents
+	 * Return the type of the field the receiver represents,
+     * this is an internal method
 	 * 
 	 * @return A Class object representing the type of the field
 	 */
-	public Class<?> getType() {
+	private Class<?> getTypeInternal() {
 		if (type instanceof WeakReference) {
 			return (Class<?>)((WeakReference) type).get();
 		}
 		return (Class<?>) type;
 	}
+    
+    /**
+     * Return the type of the field the receiver represents
+     * 
+     * @return A Class object representing the type of the field
+     */
+    public Class<?> getType() {
+    	Class<?> cl = getTypeInternal();
+        if (isDeserialized && !cl.isPrimitive()) {
+            return Object.class;
+        }
+        return cl;
+    }
 
 	/**
 	 * Return the type code that corresponds to the class the receiver
@@ -160,7 +177,7 @@
 	 * @return A char, the typecode of the class
 	 */
 	public char getTypeCode() {
-		Class<?> t = getType();
+		Class<?> t = getTypeInternal();
 		if (t == Integer.TYPE) {
 			return 'I';
 		}
@@ -202,7 +219,7 @@
 			return null;
 		}
 		if (typeString == null) {
-			Class<?> t = getType();
+			Class<?> t = getTypeInternal();
 			String typeName = t.getName().replace('.', '/');
 			String str = (t.isArray()) ? typeName : ("L" + typeName + ';'); //$NON-NLS-1$
 			typeString = str.intern();
@@ -218,7 +235,7 @@
 	 *         type of this field is a regular class.
 	 */
 	public boolean isPrimitive() {
-		Class<?> t = getType();
+		Class<?> t = getTypeInternal();
 		return t != null && t.isPrimitive();
 	}
 
@@ -239,7 +256,7 @@
 	 * @return a printable representation for the receiver.
 	 */
 	public String toString() {
-		return this.getClass().getName() + '(' + getName() + ':' + getType()
+		return this.getClass().getName() + '(' + getName() + ':' + getTypeInternal()
 				+ ')';
 	}
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java?rev=436697&r1=436696&r2=436697&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/ObjectStreamFieldTest.java Fri Aug 25 00:45:21 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.
@@ -15,9 +15,18 @@
 
 package tests.api.java.io;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.NotActiveException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.ObjectStreamField;
 import java.io.Serializable;
+import java.io.StreamCorruptedException;
+import java.util.Date;
 
 public class ObjectStreamFieldTest extends junit.framework.TestCase {
 
@@ -134,6 +143,49 @@
 		assertTrue("toString on a long returned: " + bamField.toString(),
 				bamField.toString().indexOf("bam") >= 0);
 	}
+    
+    /**
+     * @tests java.io.ObjectStreamField#getType() 
+     */
+    public void test_getType_Deserialized() throws IOException,
+            ClassNotFoundException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(new SerializableObject());
+        oos.close();
+        baos.close();
+
+        byte[] bytes = baos.toByteArray();
+        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+        ObjectInputStream ois = new ObjectInputStream(bais);
+        SerializableObject obj = (SerializableObject) ois.readObject();
+
+        ObjectStreamClass oc = obj.getObjectStreamClass();
+        ObjectStreamField field = oc.getField("i");
+        assertEquals(Object.class, field.getType());
+    }
+    
+    /**
+     * @tests java.io.ObjectStreamField#getType()
+     */
+    public void test_getType_MockObjectInputStream() throws IOException, ClassNotFoundException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+        ObjectOutputStream oos = new ObjectOutputStream(baos);
+        oos.writeObject(new SerializableObject());
+        oos.close();
+        baos.close();
+
+        byte[] bytes = baos.toByteArray();
+        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+        MockObjectInputStream ois = new MockObjectInputStream(bais);
+        ois.readObject();
+
+        ObjectStreamClass oc = ois.getObjectStreamClass();
+        ObjectStreamField field = oc.getField("i");
+        assertEquals(Object.class, field.getType());
+    }
+    
+
 
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method
@@ -153,4 +205,66 @@
 	 */
 	protected void tearDown() {
 	}
+}
+
+class SerializableObject implements Serializable {
+    public ObjectInputStream.GetField getField = null;
+
+    private static final long serialVersionUID = -2953957835918368056L;
+
+    public Date d;
+
+    public Integer i;
+    
+    public Exception e;
+    
+    public SerializableObject() {
+        d = new Date();
+        i = new Integer(1);
+        e = new Exception("e");
+    }
+
+    private void writeObject(ObjectOutputStream o) throws IOException {
+        o.putFields().put("d", new Date());
+        o.putFields().put("i", new Integer(11));
+        o.writeFields();
+    }
+
+    private void readObject(ObjectInputStream in) throws NotActiveException,
+            IOException, ClassNotFoundException {
+        getField = in.readFields();
+        d = (Date) getField.get("d", null);
+        i = (Integer) getField.get("i", null);
+    }
+
+    public ObjectStreamClass getObjectStreamClass() {
+        return getField.getObjectStreamClass();
+    }
+}
+
+class MockObjectInputStream extends ObjectInputStream {
+    private ObjectStreamClass temp = null;
+
+    public MockObjectInputStream() throws SecurityException, IOException {
+        super();
+    }
+
+    public MockObjectInputStream(InputStream in)
+            throws StreamCorruptedException, IOException {
+        super(in);
+    }
+
+    public ObjectStreamClass readClassDescriptor() throws IOException,
+            ClassNotFoundException {
+        ObjectStreamClass osc = super.readClassDescriptor();
+        //To get the ObjectStreamClass of SerializableObject
+        if (osc.getSerialVersionUID() == -2953957835918368056L) {
+            temp = osc;
+        }
+        return osc;
+    }
+
+    public ObjectStreamClass getObjectStreamClass() {
+        return temp;
+    }
 }