From a63d7446dd791d3386880a47892d4c8212ccf316 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 20 Dec 2002 23:27:12 +0000 Subject: [PATCH] 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 --- src/java/com/threerings/io/Streamer.java | 60 +++++++++++++++--------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/java/com/threerings/io/Streamer.java b/src/java/com/threerings/io/Streamer.java index eadb4c2ad..046f330fb 100644 --- a/src/java/com/threerings/io/Streamer.java +++ b/src/java/com/threerings/io/Streamer.java @@ -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; import java.lang.NoSuchMethodException; +import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -340,34 +341,18 @@ public class Streamer return; } - // reflect on all the public fields - Field[] fields = target.getFields(); + // reflect on all the object's fields ArrayList flist = new ArrayList(); - int fcount = fields.length; - for (int ii = 0; ii < fcount; ii++) { - Field field = fields[ii]; - 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); + getFields(target, flist); + int fcount = flist.size(); + _fields = (Field[])flist.toArray(new Field[fcount]); // obtain field marshallers for all of our fields _marshallers = new FieldMarshaller[fcount]; for (int ii = 0; ii < fcount; ii++) { _marshallers[ii] = FieldMarshaller.getFieldMarshaller(_fields[ii]); // Log.info("Using " + _marshallers[ii] + " for " + -// _fields[ii].getName() + "."); +// _target.getName() + "." + _fields[ii].getName() + "."); } // 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 * basic types.