classpath-patches
[Top][All Lists]
Advanced

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

[cp-patches] Serialization: readResolve and writeReplace in parent class


From: Daniel Bonniot
Subject: [cp-patches] Serialization: readResolve and writeReplace in parent class
Date: Mon, 27 Jun 2005 13:00:06 +0200
User-agent: Debian Thunderbird 1.0.2 (X11/20050602)


Hi,

As I just got my FSF assignment, here my first patch (against HEAD).

Purpose: readResolve and writeReplace are currently only looked for in the current class, while accessible parent methods must also be considered. This is what this patch does.

This patch is fixing two mauve errors I submitted earlier:
gnu.testlet.java.io.Serializable.ParentReadResolve
gnu.testlet.java.io.Serializable.ParentWriteReplace

Cheers,

Daniel

Index: ChangeLog
===================================================================
RCS file: /cvsroot/classpath/classpath/ChangeLog,v
retrieving revision 1.3915
diff -u -r1.3915 ChangeLog
--- ChangeLog   27 Jun 2005 00:12:08 -0000      1.3915
+++ ChangeLog   27 Jun 2005 10:46:21 -0000
@@ -1,3 +1,8 @@
+2005-06-27  Daniel Bonniot  <address@hidden>
+
+       * java/io/ObjectStreamClass.java
+       (cacheMethods): Fixed lookup of readResolve and writeReplace.
+
 2005-06-27  Tom Tromey  <address@hidden>
 
        * java/lang/Integer.java:
Index: java/io/ObjectStreamClass.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/ObjectStreamClass.java,v
retrieving revision 1.39
diff -u -r1.39 ObjectStreamClass.java
--- java/io/ObjectStreamClass.java      3 Feb 2005 19:20:05 -0000       1.39
+++ java/io/ObjectStreamClass.java      27 Jun 2005 10:46:23 -0000
@@ -486,19 +486,65 @@
     return null;
   }
 
+  private static boolean inSamePackage(Class c1, Class c2)
+  {
+    String name1 = c1.getName();
+    String name2 = c2.getName();
+
+    int id1 = name1.lastIndexOf('.');
+    int id2 = name2.lastIndexOf('.');
+
+    // Handle the default package
+    if (id1 == -1 || id2 == -1)
+      return id1 == id2;
+
+    String package1 = name1.substring(0, id1);
+    String package2 = name2.substring(0, id2);
+
+    return package1.equals(package2);
+  }
+
+  final static Class[] noArgs = new Class[0];
+
+  private static Method findAccessibleMethod(String name, Class from)
+  {
+    for (Class c = from; c != null; c = c.getSuperclass())
+      {
+       try
+         {
+           Method res = c.getDeclaredMethod(name, noArgs);
+           int mods = res.getModifiers();
+
+           if (c != from &&
+               (Modifier.isPrivate(mods) ||
+                !Modifier.isPublic(mods) && ! inSamePackage(c, from)))
+             continue;
+
+           return res;
+         }
+       catch (NoSuchMethodException e)
+         {
+         }
+      }
+
+    return null;
+  }
+
   private void cacheMethods()
   {
     Method[] methods = forClass().getDeclaredMethods();
+
     readObjectMethod = findMethod(methods, "readObject",
                                  new Class[] { ObjectInputStream.class },
                                  Void.TYPE, true);
     writeObjectMethod = findMethod(methods, "writeObject",
                                    new Class[] { ObjectOutputStream.class },
                                    Void.TYPE, true);
-    readResolveMethod = findMethod(methods, "readResolve",
-                                  new Class[0], Object.class, false);
-    writeReplaceMethod = findMethod(methods, "writeReplace",
-                                    new Class[0], Object.class, false);
+
+    // readResolve and writeReplace can be in parent classes, as long as they
+    // are accessible from this class.
+    readResolveMethod = findAccessibleMethod("readResolve", forClass());
+    writeReplaceMethod = findAccessibleMethod("writeReplace", forClass());
   }
 
   private ObjectStreamClass(Class cl)

reply via email to

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