Hi,

This fixes bug #22841 by rewriting the ObjectValidators support. It
seems this code has been wrong since 1998 when it was first added. But
it is not a feature that is often used.

2005-10-12  Mark Wielaard  <[EMAIL PROTECTED]>

        Fixes bug #22841
        * java/io/ObjectInputStream (validators): Removed field.
        (isDeserializing): Removed field.
        (currentObjectValidators): New field.
        (ObjectInputStream): Remove validators and isDeserializing
        initialization.
        (readObject): Remove isDeserializing logic. Don't call
        invokeValidators() here.
        (parseContent): Call invokeValidators() after TC_OBJECT done.
        (registerValidation): Create currentObjectValidators if needed.
        (invokeValidators): Rewritten.

This fixes the gnu.testlet.java.io.ObjectInputStream.registerValidation
mauve tests and introduces no regressions.

Committed,

Mark
Index: java/io/ObjectInputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectInputStream.java,v
retrieving revision 1.68
diff -u -r1.68 ObjectInputStream.java
--- java/io/ObjectInputStream.java	17 Sep 2005 15:46:22 -0000	1.68
+++ java/io/ObjectInputStream.java	12 Oct 2005 19:28:48 -0000
@@ -52,6 +52,8 @@
 import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.TreeSet;
 import java.util.Vector;
 
 public class ObjectInputStream extends InputStream
@@ -90,7 +92,6 @@
       }
 
     this.resolveEnabled = false;
-    this.isDeserializing = false;
     this.blockDataPosition = 0;
     this.blockDataBytes = 0;
     this.blockData = new byte[BUFFER_SIZE];
@@ -98,7 +99,6 @@
     this.realInputStream = new DataInputStream(in);
     this.nextOID = baseWireHandle;
     this.objectLookupTable = new Hashtable();
-    this.validators = new Vector();
     this.classLookupTable = new Hashtable();
     setBlockDataMode(true);
     readStreamHeader();
@@ -126,42 +126,28 @@
     if (this.useSubclassMethod)
       return readObjectOverride();
 
-    boolean was_deserializing;
-
     Object ret_val;
-    was_deserializing = this.isDeserializing;
-
     boolean old_mode = setBlockDataMode(false);
-
-    this.isDeserializing = true;
-
     byte marker = this.realInputStream.readByte();
 
-    depth += 2;
+    if (DEBUG)
+      depth += 2;
 
     if(dump) dumpElement("MARKER: 0x" + Integer.toHexString(marker) + " ");
 
     try
       {
  	ret_val = parseContent(marker);
-       }
-     finally
-       {
+      }
+    finally
+      {
  	setBlockDataMode(old_mode);
- 	
- 	this.isDeserializing = was_deserializing;
- 	
- 	depth -= 2;
- 	
- 	if (! was_deserializing)
-	  {
- 	    if (validators.size() > 0)
- 	      invokeValidators();
- 	  }
-       }
-     
-     return ret_val;
-   }
+ 	if (DEBUG)
+	  depth -= 2;
+      }
+    
+    return ret_val;
+  }
 
    /**
     * Handles a content block within the stream, which begins with a marker
@@ -347,8 +333,10 @@
  	  int handle = assignNewHandle(obj);
  	  Object prevObject = this.currentObject;
  	  ObjectStreamClass prevObjectStreamClass = this.currentObjectStreamClass;
+	  TreeSet prevObjectValidators = this.currentObjectValidators;
  	  
  	  this.currentObject = obj;
+	  this.currentObjectValidators = null;
  	  ObjectStreamClass[] hierarchy =
  	    inputGetObjectStreamClasses(clazz);
  	  
@@ -400,7 +388,10 @@
  	  this.currentObject = prevObject;
  	  this.currentObjectStreamClass = prevObjectStreamClass;
  	  ret_val = processResolution(osc, obj, handle);
- 	  
+	  if (currentObjectValidators != null)
+	    invokeValidators();
+	  this.currentObjectValidators = prevObjectValidators;
+
  	  break;
  	}
 	
@@ -732,8 +723,10 @@
       throw new InvalidObjectException("attempt to add a null "
 				       + "ObjectInputValidation object");
 
-    this.validators.addElement(new ValidatorAndPriority (validator,
-							 priority));
+    if (currentObjectValidators == null)
+      currentObjectValidators = new TreeSet();
+    
+    currentObjectValidators.add(new ValidatorAndPriority(validator, priority));
   }
 
 
@@ -1837,18 +1830,19 @@
   // on OBJ
   private void invokeValidators() throws InvalidObjectException
   {
-    Object[] validators = new Object[this.validators.size()];
-    this.validators.copyInto (validators);
-    Arrays.sort (validators);
-
     try
       {
-	for (int i=0; i < validators.length; i++)
-	  ((ObjectInputValidation)validators[i]).validateObject();
+	Iterator it = currentObjectValidators.iterator();
+	while(it.hasNext())
+	  {
+	    ValidatorAndPriority vap = (ValidatorAndPriority) it.next();
+	    ObjectInputValidation validator = vap.validator;
+	    validator.validateObject();
+	  }
       }
     finally
       {
-	this.validators.removeAllElements();
+	currentObjectValidators = null;
       }
   }
 
@@ -1897,10 +1891,9 @@
   private Hashtable objectLookupTable;
   private Object currentObject;
   private ObjectStreamClass currentObjectStreamClass;
+  private TreeSet currentObjectValidators;
   private boolean readDataFromBlock;
-  private boolean isDeserializing;
   private boolean fieldsAlreadyRead;
-  private Vector validators;
   private Hashtable classLookupTable;
   private GetField prereadFields;
 

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to