[kaffe] CVS kaffe (guilhem): libraries/javalib/java/io/ObjectInputStream.java

Kaffe CVS cvs-commits at kaffe.org
Sun Feb 22 00:36:01 PST 2004


PatchSet 4448 
Date: 2004/02/22 08:33:09
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
        * libraries/javalib/java/io/ObjectInputStream.java
        (readClassDescriptor): Fixed field sorting. Keep elements of the
        mapping non null.
        (readFields): Fixed main loop and base logic. Small reindentation.

        * libraries/javalib/java/io/ObjectStreamField.java
        (lookupField): New method to update the field reference.
        (setBooleanField, setByteField, setCharField, setShortField,
        setIntField, setLongField, setFloatField, setDoubleField,
        setObjectField): Improved exception reporting.

        * libraries/javalib/java/io/ObjectStreamClass.java
        (setClass, setFields): Call lookupField when building the field
        database.

Members: 
	ChangeLog:1.2028->1.2029 
	libraries/javalib/java/io/ObjectInputStream.java:1.32->1.33 
	libraries/javalib/java/io/ObjectStreamClass.java:1.19->1.20 
	libraries/javalib/java/io/ObjectStreamField.java:1.6->1.7 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.2028 kaffe/ChangeLog:1.2029
--- kaffe/ChangeLog:1.2028	Fri Feb 20 15:59:37 2004
+++ kaffe/ChangeLog	Sun Feb 22 08:33:09 2004
@@ -1,3 +1,20 @@
+2004-02-22  Guilhem Lavaux <guilhem at kaffe.org>
+
+	* libraries/javalib/java/io/ObjectInputStream.java
+	(readClassDescriptor): Fixed field sorting. Keep elements of the
+	mapping non null.
+	(readFields): Fixed main loop and base logic. Small reindentation.
+
+	* libraries/javalib/java/io/ObjectStreamField.java
+	(lookupField): New method to update the field reference.
+	(setBooleanField, setByteField, setCharField, setShortField,
+	setIntField, setLongField, setFloatField, setDoubleField,
+	setObjectField): Improved exception reporting.
+
+	* libraries/javalib/java/io/ObjectStreamClass.java
+	(setClass, setFields): Call lookupField when building the field
+	database.
+
 2004-02-20  Ito Kazumitsu  <kaz at maczuka.gcd.org>
 
 	* libraries/clib/native/System.c
Index: kaffe/libraries/javalib/java/io/ObjectInputStream.java
diff -u kaffe/libraries/javalib/java/io/ObjectInputStream.java:1.32 kaffe/libraries/javalib/java/io/ObjectInputStream.java:1.33
--- kaffe/libraries/javalib/java/io/ObjectInputStream.java:1.32	Mon Feb  9 15:56:23 2004
+++ kaffe/libraries/javalib/java/io/ObjectInputStream.java	Sun Feb 22 08:33:10 2004
@@ -513,7 +513,7 @@
 	else
 	  {
 	    int comp_val =
-		real_fields[real_idx].compareTo (stream_fields[stream_idx]);
+		real_fields[real_idx].getName().compareTo (stream_fields[stream_idx].getName());
 
 	    if (comp_val < 0)
 	      {
@@ -527,21 +527,13 @@
 	      {
 		stream_field = stream_fields[stream_idx++];
 		real_field = real_fields[real_idx++];
-		if(stream_field.getType() != real_field.getType())
-		    throw new InvalidClassException
-			("invalid field type for " + real_field.getName() +
-			" in class " + name);
+		if (stream_field.getType() != real_field.getType())
+		  throw new InvalidClassException
+		    ("invalid field type for " + real_field.getName() +
+		     " in class " + name);
 	      }
 	  }
-	if (stream_field != null)
-	  {
-	    if (stream_field.getOffset() < 0)
-		stream_field = null;
-	    else if (!stream_field.isToSet())
-		real_field = null;
-	  }
-	if (real_field != null && !real_field.isToSet())
-	    real_field = null;
+
 	/* If some of stream_fields does not correspond to any of real_fields,
 	 * or the opposite, then fieldmapping will go short.
 	 */
@@ -550,7 +542,7 @@
 	    ObjectStreamField[] newfieldmapping =
 	      new ObjectStreamField[fieldmapping.length + 2];
 	    System.arraycopy(fieldmapping, 0,
-	      newfieldmapping, 0, fieldmapping.length);
+			     newfieldmapping, 0, fieldmapping.length);
 	    fieldmapping = newfieldmapping;
 	  }
 	fieldmapping[map_idx++] = stream_field;
@@ -1576,121 +1568,119 @@
       {
 	ObjectStreamField stream_field = fields[i];
 	ObjectStreamField real_field = fields[i + 1];
-	if(stream_field != null || real_field != null)
-	  {
-	    boolean read_value = stream_field != null;
-	    boolean set_value = real_field != null;
-	    String field_name;
-	    char type;
-	    if (stream_field != null)
-	      {
-		field_name = stream_field.getName();
-		type = stream_field.getTypeCode();
-	      }
-	    else
-	      {
-		field_name = real_field.getName();
-		type = real_field.getTypeCode();
-	      }
+	boolean read_value = (stream_field != null && stream_field.getOffset() >= 0 && stream_field.isToSet());
+	boolean set_value = (real_field != null && real_field.isToSet());
+	String field_name;
+	char type;
 
-	    switch(type)
-	      {
-		case 'Z':
-		  {
-		    boolean value =
-			read_value ? this.realInputStream.readBoolean() : false;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setBooleanField(obj, value);
-		    break;
-		  }
-		case 'B':
-		  {
-		    byte value =
-			read_value ? this.realInputStream.readByte() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setByteField(obj, value);
-		    break;
-		  }
-		case 'C':
-		  {
-		    char value =
-			read_value ? this.realInputStream.readChar(): 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setCharField(obj, value);
-		    break;
-		  }
-		case 'D':
-		  {
-		    double value =
-			read_value ? this.realInputStream.readDouble() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setDoubleField(obj, value);
-		    break;
-		  }
-		case 'F':
-		  {
-		    float value =
-			read_value ? this.realInputStream.readFloat() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setFloatField(obj, value);
-		    break;
-		  }
-		case 'I':
-		  {
-		    int value =
-			read_value ? this.realInputStream.readInt() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setIntField(obj, value);
-		    break;
-		  }
-		case 'J':
-		  {
-		    long value =
-			read_value ? this.realInputStream.readLong() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setLongField(obj, value);
-		    break;
-		  }
-		case 'S':
-		  {
-		    short value =
-			read_value ? this.realInputStream.readShort() : 0;
-		    if (dump && read_value && set_value)
-		    dumpElementln("  " + field_name + ": " + value);
-		    if (set_value)
-			real_field.setShortField(obj, value);
-		    break;
-		  }
-		case 'L':
-		case '[':
-		  {
-		    Object value =
-			read_value ? readObject() : null;
-		    if (set_value)
-			real_field.setObjectField(obj, value);
-		    break;
-		  }
-		default:
-		    throw new InternalError("Invalid type code: " + type);
-	      }
+	if (stream_field != null)
+	  {
+	    field_name = stream_field.getName();
+	    type = stream_field.getTypeCode();
+	  }
+	else
+	  {
+	    field_name = real_field.getName();
+	    type = real_field.getTypeCode();
+	  }
+	
+	switch(type)
+	  {
+	  case 'Z':
+	    {
+	      boolean value =
+		read_value ? this.realInputStream.readBoolean() : false;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setBooleanField(obj, value);
+	      break;
+	    }
+	  case 'B':
+	    {
+	      byte value =
+		read_value ? this.realInputStream.readByte() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setByteField(obj, value);
+	      break;
+	    }
+	  case 'C':
+	    {
+	      char value =
+		read_value ? this.realInputStream.readChar(): 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setCharField(obj, value);
+	      break;
+	    }
+	  case 'D':
+	    {
+	      double value =
+		read_value ? this.realInputStream.readDouble() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setDoubleField(obj, value);
+	      break;
+	    }
+	  case 'F':
+	    {
+	      float value =
+		read_value ? this.realInputStream.readFloat() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setFloatField(obj, value);
+	      break;
+	    }
+	  case 'I':
+	    {
+	      int value =
+		read_value ? this.realInputStream.readInt() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setIntField(obj, value);
+	      break;
+	    }
+	  case 'J':
+	    {
+	      long value =
+		read_value ? this.realInputStream.readLong() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setLongField(obj, value);
+	      break;
+	    }
+	  case 'S':
+	    {
+	      short value =
+		read_value ? this.realInputStream.readShort() : 0;
+	      if (dump && read_value && set_value)
+		dumpElementln("  " + field_name + ": " + value);
+	      if (set_value)
+		real_field.setShortField(obj, value);
+	      break;
+	    }
+	  case 'L':
+	  case '[':
+	    {
+	      Object value =
+		read_value ? readObject() : null;
+	      if (set_value && stream_field != null)
+		real_field.setObjectField(obj, stream_field.getTypeString(), value);
+	      break;
+	    }
+	  default:
+	    throw new InternalError("Invalid type code: " + type);
 	  }
       }
   }
-
+  
   // Toggles writing primitive data to block-data buffer.
   private boolean setBlockDataMode (boolean on)
   {
Index: kaffe/libraries/javalib/java/io/ObjectStreamClass.java
diff -u kaffe/libraries/javalib/java/io/ObjectStreamClass.java:1.19 kaffe/libraries/javalib/java/io/ObjectStreamClass.java:1.20
--- kaffe/libraries/javalib/java/io/ObjectStreamClass.java:1.19	Mon Feb  2 18:01:06 2004
+++ kaffe/libraries/javalib/java/io/ObjectStreamClass.java	Sun Feb 22 08:33:10 2004
@@ -344,6 +344,7 @@
 		newFieldList[k] = exportedFields[j];
 		newFieldList[k].setPersistent(true);
 		newFieldList[k].setToSet(false);
+		newFieldList[k].lookupField(clazz);
 		j++;
 	      }
 	    else
@@ -554,6 +555,19 @@
 	    if (fields != null)
 	      {
 		Arrays.sort (fields);
+		// Retrieve field reference.
+		for (int i=0; i < fields.length; i++)
+		  {
+		    try
+		      {
+			fields[i].lookupField(cl);
+		      }
+		    catch (NoSuchFieldException _)
+		      {
+			fields[i].setToSet(false);
+		      }
+		  }
+		
 		calculateOffsets();
 		return;
 	      }
@@ -799,7 +813,7 @@
 
     fieldsArray = new ObjectStreamField[ o.length ];
     System.arraycopy(o, 0, fieldsArray, 0, o.length);
-    
+
     return fieldsArray;
   }
 
Index: kaffe/libraries/javalib/java/io/ObjectStreamField.java
diff -u kaffe/libraries/javalib/java/io/ObjectStreamField.java:1.6 kaffe/libraries/javalib/java/io/ObjectStreamField.java:1.7
--- kaffe/libraries/javalib/java/io/ObjectStreamField.java:1.6	Mon Feb  2 18:01:06 2004
+++ kaffe/libraries/javalib/java/io/ObjectStreamField.java	Sun Feb 22 08:33:10 2004
@@ -41,6 +41,8 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import gnu.java.lang.reflect.TypeSignature;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 
 /**
  * This class intends to describe the field of a class for the serialization
@@ -99,7 +101,7 @@
  
   /**
    * There are many cases you can not get java.lang.Class from typename 
-   * if your context class loader cann not load it, then use typename to
+   * if your context class loader cannot load it, then use typename to
    * construct the field.
    *
    * @param name Name of the field to export.
@@ -292,7 +294,7 @@
   }
 
   /**
-   * This methods returns true if the field is marked as to be
+   * This method returns true if the field is marked as to be
    * set.
    *
    * @return True if it is to be set, false in the other cases.
@@ -303,109 +305,186 @@
     return toset;
   }
 
+  /**
+   * This method searches for its field reference in the specified class
+   * object. It requests privileges. If an error occurs the internal field
+   * reference is not modified.
+   *
+   * @throws NoSuchFieldException if the field name does not exist in this class.
+   * @throws SecurityException if there was an error requesting the privileges.
+   */
+  void lookupField(Class clazz) throws NoSuchFieldException, SecurityException
+  {
+    final Field f = clazz.getDeclaredField(name);
+    
+    AccessController.doPrivileged(new PrivilegedAction()
+      {
+	public Object run()
+	{
+	  f.setAccessible(true);
+	  return null;
+	}
+      });
+    
+    this.field = f;
+  }
+
   public String toString ()
   {
     return "ObjectStreamField< " + type + " " + name + " >";
   }
 
-  final void setBooleanField(Object obj, boolean val)
+  /*
+   * These methods set the required field in the class instance obj.
+   * They may throw NullPointerException if the field does not really exist.
+   */
+
+  final void setBooleanField(Object obj, boolean val) throws IOException
   {
-      try
+    try
       {
-	  field.setBoolean(obj, val);
+	field.setBoolean(obj, val);
       }
-      catch(IllegalAccessException x)
+    catch (IllegalArgumentException _)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
+      }
+    catch(IllegalAccessException x)
+      {
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setByteField(Object obj, byte val)
+  
+  final void setByteField(Object obj, byte val) throws IOException
   {
-      try
+    try
       {
-	  field.setByte(obj, val);
+	field.setByte(obj, val);
       }
-      catch(IllegalAccessException x)
+    catch (IllegalArgumentException _)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
+      }
+    catch(IllegalAccessException x)
+      {
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setCharField(Object obj, char val)
+  
+  final void setCharField(Object obj, char val) throws IOException
   {
-      try
+    try
+      {
+	field.setChar(obj, val);
+      }
+    catch (IllegalArgumentException _)
       {
-	  field.setChar(obj, val);
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
       }
-      catch(IllegalAccessException x)
+    catch(IllegalAccessException x)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setShortField(Object obj, short val)
+  
+  final void setShortField(Object obj, short val) throws IOException
   {
-      try
+    try
       {
-	  field.setShort(obj, val);
+	field.setShort(obj, val);
       }
-      catch(IllegalAccessException x)
+    catch (IllegalArgumentException _)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
+      }
+    catch(IllegalAccessException x)
+      {
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setIntField(Object obj, int val)
+  
+  final void setIntField(Object obj, int val) throws IOException
   {
-      try
+    try
+      {
+	field.setInt(obj, val);
+      }
+    catch (IllegalArgumentException _)
+      {
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
+      }
+    catch(IllegalAccessException x)
       {
-	  field.setInt(obj, val);
+	throw new InternalError(x.getMessage());
       }
-      catch(IllegalAccessException x)
+    catch (Exception _)
       {
-	  throw new InternalError(x.getMessage());
       }
   }
-
-  final void setLongField(Object obj, long val)
+  
+  final void setLongField(Object obj, long val) throws IOException
   {
-      try
+    try
       {
-	  field.setLong(obj, val);
+	field.setLong(obj, val);
       }
-      catch(IllegalAccessException x)
+    catch (IllegalArgumentException _)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
+      }
+    catch(IllegalAccessException x)
+      {
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setFloatField(Object obj, float val)
+  
+  final void setFloatField(Object obj, float val) throws IOException
   {
-      try
+    try
+      {
+	field.setFloat(obj, val);
+      }
+    catch (IllegalArgumentException _)
       {
-	  field.setFloat(obj, val);
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
       }
-      catch(IllegalAccessException x)
+    catch(IllegalAccessException x)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setDoubleField(Object obj, double val)
+  
+  final void setDoubleField(Object obj, double val) throws IOException
   {
-      try
+    try
+      {
+	field.setDouble(obj, val);
+      }
+    catch (IllegalArgumentException _)
       {
-	  field.setDouble(obj, val);
+	throw new InvalidClassException("incompatible field type for " + 
+					obj.getClass().getName() + "." + name);
       }
-      catch(IllegalAccessException x)
+    catch(IllegalAccessException x)
       {
-	  throw new InternalError(x.getMessage());
+	throw new InternalError(x.getMessage());
       }
   }
-
-  final void setObjectField(Object obj, Object val)
+  
+  final void setObjectField(Object obj, String valtype, Object val) throws IOException
   {
+    if (valtype == null ||
+	!typename.equals(valtype))
+      throw new InvalidClassException("incompatible field type for " + 
+				      obj.getClass().getName() + "." + name);
+ 
     try
       {
 	field.set(obj, val);




More information about the kaffe mailing list