From f7fe97da3633ca6a5e611956b09f87bf078d1c77 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 1 Aug 2003 03:02:00 +0000 Subject: re PR libgcj/11737 (java.io.ObjectOutputStream.writeObject does not work with HashMap) Fix for PR libgcj/11737: * java/io/ObjectOutputStream.java (getMethod): Make method accessible. (getField): Likewise. (writeObject): Use getMethod. Import PrivilegedAction and AccessController. (callWriteMethod): Don't check whether m is null. Catch NoSuchMethodException. From-SVN: r70035 --- libjava/java/io/ObjectOutputStream.java | 54 +++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'libjava/java/io/ObjectOutputStream.java') diff --git a/libjava/java/io/ObjectOutputStream.java b/libjava/java/io/ObjectOutputStream.java index 4014fcd..49cb636 100644 --- a/libjava/java/io/ObjectOutputStream.java +++ b/libjava/java/io/ObjectOutputStream.java @@ -42,6 +42,8 @@ import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; +import java.security.PrivilegedAction; +import java.security.AccessController; import java.util.Hashtable; import gnu.java.io.ObjectIdentityWrapper; @@ -240,10 +242,11 @@ public class ObjectOutputStream extends OutputStream try { Class classArgs[] = {}; - m = obj.getClass ().getDeclaredMethod ("writeReplace", - classArgs); - // m can't be null by definition since an exception would - // have been thrown so a check for null is not needed. + m = getMethod(obj.getClass(), "writeReplace", + classArgs); + // m can't be null by definition since an + // exception would have been thrown so a check + // for null is not needed. obj = m.invoke (obj, new Object[] {}); } catch (NoSuchMethodException ignore) @@ -993,7 +996,8 @@ public class ObjectOutputStream extends OutputStream private void checkType (ObjectStreamField field, char type) throws IllegalArgumentException { - if (TypeSignature.getEncodingOfClass (field.getType ()).charAt (0) != type) + if (TypeSignature.getEncodingOfClass (field.getType ()).charAt (0) + != type) throw new IllegalArgumentException (); } }; @@ -1200,11 +1204,13 @@ public class ObjectOutputStream extends OutputStream { Class classArgs[] = {ObjectOutputStream.class}; Method m = getMethod (klass, "writeObject", classArgs); - if (m == null) - return; Object args[] = {this}; m.invoke (obj, args); } + catch (NoSuchMethodException nsme) + { + // Nothing. + } catch (InvocationTargetException x) { /* Rethrow if possible. */ @@ -1239,7 +1245,8 @@ public class ObjectOutputStream extends OutputStream } } - private byte getByteField (Object obj, Class klass, String field_name) throws IOException + private byte getByteField (Object obj, Class klass, String field_name) + throws IOException { try { @@ -1253,7 +1260,8 @@ public class ObjectOutputStream extends OutputStream } } - private char getCharField (Object obj, Class klass, String field_name) throws IOException + private char getCharField (Object obj, Class klass, String field_name) + throws IOException { try { @@ -1297,7 +1305,8 @@ public class ObjectOutputStream extends OutputStream } } - private int getIntField (Object obj, Class klass, String field_name) throws IOException + private int getIntField (Object obj, Class klass, String field_name) + throws IOException { try { @@ -1311,7 +1320,8 @@ public class ObjectOutputStream extends OutputStream } } - private long getLongField (Object obj, Class klass, String field_name) throws IOException + private long getLongField (Object obj, Class klass, String field_name) + throws IOException { try { @@ -1359,13 +1369,31 @@ public class ObjectOutputStream extends OutputStream private static Field getField (Class klass, String name) throws java.lang.NoSuchFieldException { - return klass.getDeclaredField(name); + final Field f = klass.getDeclaredField(name); + AccessController.doPrivileged(new PrivilegedAction() + { + public Object run() + { + f.setAccessible(true); + return null; + } + }); + return f; } private static Method getMethod (Class klass, String name, Class[] args) throws java.lang.NoSuchMethodException { - return klass.getDeclaredMethod(name, args); + final Method m = klass.getDeclaredMethod(name, args); + AccessController.doPrivileged(new PrivilegedAction() + { + public Object run() + { + m.setAccessible(true); + return null; + } + }); + return m; } // this value comes from 1.2 spec, but is used in 1.1 as well -- cgit v1.1