classpath-patches
[Top][All Lists]
Advanced

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

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


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

This seems like it must be duplicated somewhere, but I couldn't find
it quickly.

I also looked but could not find code to reuse.

There is getPackagePortion in Class, maybe making that
package-private and then using it would be good.

We're in java.io...

I wouldn't mind adding a utility method somewhere if I'm pointed to the right place. On irc, Tom seemed to think it's trivial enough to not bother.

GNU style is to put line breaks before operators, not after them.

OK, fixed, I reattach the patch.

I wonder if there is a strange case here where someone adds a private
method to the class hierarchy "later" (after all the other classes are
compiled).  Should the private method hide the superclass methods of
the same signature?  Perhaps this is an incompatible change, offhand I
forget.

I don't know the answer. If somebody can come up with a mauve test...

In the mean time, my patch improves on the current behavior.

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 17:24:35 -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 17:24:37 -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]