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
This commit is contained in:
Tom Tromey 2003-08-01 03:02:00 +00:00 committed by Tom Tromey
parent 3c260e1d97
commit f7fe97da36
2 changed files with 50 additions and 13 deletions

View file

@ -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