Need to write out the class name of a streamable field as it may be a

derived class of the declared field type.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1095 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-03-06 23:45:01 +00:00
parent 5e5f871f58
commit 0bc39b6d3f
@@ -1,5 +1,5 @@
// //
// $Id: StreamableFieldMarshaller.java,v 1.4 2002/02/19 03:30:55 mdb Exp $ // $Id: StreamableFieldMarshaller.java,v 1.5 2002/03/06 23:45:01 mdb Exp $
package com.threerings.presents.io; package com.threerings.presents.io;
@@ -8,6 +8,9 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.samskivert.io.NestableIOException;
import com.samskivert.util.StringUtil;
import com.threerings.presents.io.Streamable; import com.threerings.presents.io.Streamable;
public class StreamableFieldMarshaller implements FieldMarshaller public class StreamableFieldMarshaller implements FieldMarshaller
@@ -16,11 +19,10 @@ public class StreamableFieldMarshaller implements FieldMarshaller
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
Streamable value = (Streamable)field.get(obj); Streamable value = (Streamable)field.get(obj);
// we freak out if our streamable is null
if (value == null) { if (value == null) {
out.writeByte(0); out.writeUTF("");
} else { } else {
out.writeByte(1); out.writeUTF(value.getClass().getName());
value.writeTo(out); value.writeTo(out);
} }
} }
@@ -29,14 +31,15 @@ public class StreamableFieldMarshaller implements FieldMarshaller
throws IOException, IllegalAccessException throws IOException, IllegalAccessException
{ {
try { try {
byte isNull = in.readByte();
Streamable value = null; Streamable value = null;
String scnm = in.readUTF();
// instantiate and unserialize the streamable if we actually // instantiate and unserialize the streamable if we actually
// have a value // have a value
if (isNull == 1) { if (!StringUtil.blank(scnm)) {
Class sclass = Class.forName(scnm);
// create a new instance into which to unmarshall the field // create a new instance into which to unmarshall the field
value = (Streamable)field.getType().newInstance(); value = (Streamable)sclass.newInstance();
// unserialize it // unserialize it
value.readFrom(in); value.readFrom(in);
} }
@@ -44,10 +47,10 @@ public class StreamableFieldMarshaller implements FieldMarshaller
// and set the value in the object // and set the value in the object
field.set(obj, value); field.set(obj, value);
} catch (InstantiationException ie) { } catch (Exception e) {
throw new IOException("Unable to instantiate streamable " + String errmsg = "Unable to instantiate streamable " +
"[field=" + field + ", object=" + obj + "[field=" + field + ", object=" + obj + "]";
", error=" + ie + "]"); throw new NestableIOException(errmsg, e);
} }
} }
} }