This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Serialization of empty vector over a socket
Hi,
On Wed, 2002-07-24 at 02:42, Tom Tromey wrote:
>
> If this works for you (it did for me), I'll check it in.
> I'll try to add a new Mauve test for it too.
Argh. The exception was thrown in the debug code.
How could I have missed that?
This does work for the simple empty Vector test case,
but not for the attached testcase with a Vector that contains a couple
of Integer objects. Those contain final fields for which a
IllegalAccessException is thrown when we try to set them.
The attached diffs for natField.cc and ObjectInputStream.java make it
also work for that case. But it might be possible to optimize it a bit
since AccessibleObject can also set multiple fields at once.
Cheers,
Mark
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.util.Vector;
public class SerVector
{
public static void main(String[] args)
{
try
{
Vector v = new Vector();
for (int i=0; i <10; i++)
v.add(new Integer(i));
System.out.println(v);
// Turn Vector object into serialized byte array.
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(v);
oos.close();
out.close();
byte[] array = out.toByteArray();
// Turn serialized byte array into Vector object.
ByteArrayInputStream in = new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(in);
Vector inObject = (Vector) ois.readObject();
System.out.println(inObject);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
}
Index: java/lang/reflect/natField.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/reflect/natField.cc,v
retrieving revision 1.10
diff -u -r1.10 natField.cc
--- java/lang/reflect/natField.cc 23 Jan 2002 19:42:18 -0000 1.10
+++ java/lang/reflect/natField.cc 24 Jul 2002 08:41:10 -0000
@@ -257,7 +257,8 @@
setAddr (java::lang::reflect::Field* field, jclass caller, jobject obj)
{
void *addr = getAddr(field, caller, obj);
- if (field->getModifiers() & java::lang::reflect::Modifier::FINAL)
+ if (!field->isAccessible()
+ && field->getModifiers() & java::lang::reflect::Modifier::FINAL)
throw new java::lang::IllegalAccessException();
return addr;
}
Index: java/io/ObjectInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ObjectInputStream.java,v
retrieving revision 1.11
diff -u -r1.11 ObjectInputStream.java
--- java/io/ObjectInputStream.java 22 Jan 2002 22:40:14 -0000 1.11
+++ java/io/ObjectInputStream.java 24 Jul 2002 08:44:55 -0000
@@ -244,7 +244,7 @@
int handle = assignNewHandle (array);
readArrayElements (array, componentType);
for (int i=0, len=Array.getLength(array); i < len; i++)
- dumpElementln (" ELEMENT[" + i + "]=" + Array.get(array, i).toString());
+ dumpElementln (" ELEMENT[" + i + "]=" + Array.get(array, i));
ret_val = processResolution (array, handle);
break;
}
@@ -1401,6 +1401,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setBoolean (obj, val);
}
catch (Exception _)
@@ -1415,6 +1416,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setByte (obj, val);
}
catch (Exception _)
@@ -1429,6 +1431,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setChar (obj, val);
}
catch (Exception _)
@@ -1443,6 +1446,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setDouble (obj, val);
}
catch (Exception _)
@@ -1457,6 +1461,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setFloat (obj, val);
}
catch (Exception _)
@@ -1471,6 +1476,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setInt (obj, val);
}
catch (Exception _)
@@ -1486,6 +1492,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setLong (obj, val);
}
catch (Exception _)
@@ -1501,6 +1508,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
f.setShort (obj, val);
}
catch (Exception _)
@@ -1516,6 +1524,7 @@
{
Class klass = obj.getClass ();
Field f = getField (klass, field_name);
+ f.setAccessible(true);
// FIXME: We should check the type_code here
f.set (obj, val);
}