classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] Re: RFC: Patch for duplicate entries in serialPersisten


From: Guilhem Lavaux
Subject: Re: [cp-patches] Re: RFC: Patch for duplicate entries in serialPersistentFields
Date: Wed, 14 Dec 2005 20:39:43 +0100
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050322)

Hi,

I have just commited the attached patch which is just a slight modification of what I have already sent with some documentation.

Mark, actually I don't see what benefit we could get of moving some Array.sort & copy to getSerialPersistentFields. So I have not done it for the moment.

Cheers,

Guilhem.

Mark Wielaard wrote:
Hi Stuart,

On Sun, 2005-12-11 at 21:42 -0500, Stuart Ballard wrote:

Throw.uncheckedThrow(new InvalidClassException("..."));

A perfectly portable illegal-exception-thrower :)


It is a nice hack. But I agree with Guilhem that "illegally" throwing
checked exceptions from methods which are not explicitly documented to
do things like that should be avoided.

Cheers,

Mark

Index: java/io/ObjectOutputStream.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectOutputStream.java,v
retrieving revision 1.63
diff -u -r1.63 ObjectOutputStream.java
--- java/io/ObjectOutputStream.java     1 Nov 2005 14:54:23 -0000       1.63
+++ java/io/ObjectOutputStream.java     14 Dec 2005 19:37:07 -0000
@@ -442,6 +442,10 @@
         realOutput.writeByte(flags);
 
         ObjectStreamField[] fields = osc.fields;
+
+       if (fields == ObjectStreamClass.INVALID_FIELDS)
+         throw new InvalidClassException("serialPersistentFields in class " + 
osc.getName() + " is invalid");
+
         realOutput.writeShort(fields.length);
 
         ObjectStreamField field;
Index: java/io/ObjectStreamClass.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectStreamClass.java,v
retrieving revision 1.43
diff -u -r1.43 ObjectStreamClass.java
--- java/io/ObjectStreamClass.java      16 Sep 2005 17:24:04 -0000      1.43
+++ java/io/ObjectStreamClass.java      14 Dec 2005 19:37:07 -0000
@@ -63,6 +63,8 @@
 
 public class ObjectStreamClass implements Serializable
 {
+  static final ObjectStreamField[] INVALID_FIELDS = new ObjectStreamField[0];
+
   /**
    * Returns the <code>ObjectStreamClass</code> for <code>cl</code>.
    * If <code>cl</code> is null, or is not <code>Serializable</code>,
@@ -71,6 +73,11 @@
    * same <code>ObjectStreamClass</code> object and no recalculation
    * will be done.
    *
+   * Warning: If this class contains an invalid serialPersistentField arrays
+   * lookup will not throw anything. However address@hidden #getFields()} will 
return
+   * an empty array and address@hidden java.io.ObjectOutputStream#writeObject} 
will throw an 
+   * address@hidden java.io.InvalidClassException}.
+   *
    * @see java.io.Serializable
    */
   public static ObjectStreamClass lookup(Class cl)
@@ -148,6 +155,8 @@
    * Returns the serializable (non-static and non-transient) Fields
    * of the class represented by this ObjectStreamClass.  The Fields
    * are sorted by name.
+   * If fields were obtained using serialPersistentFields and this array
+   * is faulty then the returned array of this method will be empty.
    *
    * @return the fields.
    */
@@ -608,6 +617,28 @@
            fields = getSerialPersistentFields(cl);
            if (fields != null)
              {
+               ObjectStreamField[] fieldsName = new 
ObjectStreamField[fields.length];
+               System.arraycopy(fields, 0, fieldsName, 0, fields.length);
+
+               Arrays.sort (fieldsName, new Comparator() {
+                       public int compare(Object o1, Object o2)
+                       {
+                         ObjectStreamField f1 = (ObjectStreamField)o1;
+                         ObjectStreamField f2 = (ObjectStreamField)o2;
+                           
+                         return f1.getName().compareTo(f2.getName());
+                       }
+                   });
+               
+               for (int i=1; i < fields.length; i++)
+                 {
+                   if 
(fieldsName[i-1].getName().equals(fieldsName[i].getName()))
+                       {
+                           fields = INVALID_FIELDS;
+                           return;
+                       }
+                 }
+
                Arrays.sort (fields);
                // Retrieve field reference.
                for (int i=0; i < fields.length; i++)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]