It turns out that we can make protected and private fields accessible to
the object serialization code which means that we can automatically serialize said data members. Hooray! git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2075 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
//
|
//
|
||||||
// $Id: Streamer.java,v 1.2 2002/12/02 22:10:10 mdb Exp $
|
// $Id: Streamer.java,v 1.3 2002/12/20 23:27:12 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.io;
|
package com.threerings.io;
|
||||||
|
|
||||||
import java.lang.NoSuchMethodException;
|
import java.lang.NoSuchMethodException;
|
||||||
|
import java.lang.reflect.AccessibleObject;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@@ -340,34 +341,18 @@ public class Streamer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reflect on all the public fields
|
// reflect on all the object's fields
|
||||||
Field[] fields = target.getFields();
|
|
||||||
ArrayList flist = new ArrayList();
|
ArrayList flist = new ArrayList();
|
||||||
int fcount = fields.length;
|
getFields(target, flist);
|
||||||
for (int ii = 0; ii < fcount; ii++) {
|
int fcount = flist.size();
|
||||||
Field field = fields[ii];
|
_fields = (Field[])flist.toArray(new Field[fcount]);
|
||||||
int mods = field.getModifiers();
|
|
||||||
// skip static, transient and non-public fields
|
|
||||||
if (((mods & Modifier.STATIC) != 0) ||
|
|
||||||
((mods & Modifier.PUBLIC) == 0) ||
|
|
||||||
((mods & Modifier.TRANSIENT) != 0)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// otherwise add the field to the list
|
|
||||||
flist.add(field);
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert the list of fields into an array
|
|
||||||
fcount = flist.size();
|
|
||||||
_fields = new Field[fcount];
|
|
||||||
flist.toArray(_fields);
|
|
||||||
|
|
||||||
// obtain field marshallers for all of our fields
|
// obtain field marshallers for all of our fields
|
||||||
_marshallers = new FieldMarshaller[fcount];
|
_marshallers = new FieldMarshaller[fcount];
|
||||||
for (int ii = 0; ii < fcount; ii++) {
|
for (int ii = 0; ii < fcount; ii++) {
|
||||||
_marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]);
|
_marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]);
|
||||||
// Log.info("Using " + _marshallers[ii] + " for " +
|
// Log.info("Using " + _marshallers[ii] + " for " +
|
||||||
// _fields[ii].getName() + ".");
|
// _target.getName() + "." + _fields[ii].getName() + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
// look up the reader and writer methods
|
// look up the reader and writer methods
|
||||||
@@ -383,6 +368,37 @@ public class Streamer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains all non-transient, non-static fields of the specified class
|
||||||
|
* and its parent classes.
|
||||||
|
*/
|
||||||
|
protected void getFields (Class clazz, ArrayList flist)
|
||||||
|
{
|
||||||
|
// first reflect on our parent's fields if it has such
|
||||||
|
Class pclazz = clazz.getSuperclass();
|
||||||
|
if (!pclazz.equals(Object.class)) {
|
||||||
|
getFields(pclazz, flist);
|
||||||
|
}
|
||||||
|
|
||||||
|
// then reflect on our own declared fields
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
|
||||||
|
// override the default accessibility check for the fields
|
||||||
|
AccessibleObject.setAccessible(fields, true);
|
||||||
|
|
||||||
|
int fcount = fields.length;
|
||||||
|
for (int ii = 0; ii < fcount; ii++) {
|
||||||
|
Field field = fields[ii];
|
||||||
|
int mods = field.getModifiers();
|
||||||
|
// skip static and transient fields
|
||||||
|
if (((mods & Modifier.STATIC) != 0) ||
|
||||||
|
((mods & Modifier.TRANSIENT) != 0)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
flist.add(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates our streamers table and registers streamers for all of the
|
* Creates our streamers table and registers streamers for all of the
|
||||||
* basic types.
|
* basic types.
|
||||||
|
|||||||
Reference in New Issue
Block a user