Was using the wrong getDeclaringClass() to get an enum's proper class. Things

are now correct, but unfortunately a little more complicated.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4371 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2006-09-12 00:47:27 +00:00
parent eb3c6ae9cf
commit 65b14cfdb5
2 changed files with 20 additions and 11 deletions
@@ -93,14 +93,16 @@ public class ObjectOutputStream extends DataOutputStream
_classmap = new HashMap<Class,ClassMapping>(); _classmap = new HashMap<Class,ClassMapping>();
} }
// otherwise, look up the class mapping record // otherwise, look up the class mapping record (if the target class is
Class sclass = object.getClass(); // an enum, convert it from its potentially enum-value specific class
// to the main class for that enum)
Class sclass = Streamer.getStreamerClass(object);
ClassMapping cmap = _classmap.get(sclass); ClassMapping cmap = _classmap.get(sclass);
// create a class mapping for this class if we've not got one // create a class mapping for this class if we've not got one
if (cmap == null) { if (cmap == null) {
// create a streamer instance and assign a code to this class // create a streamer instance and assign a code to this class
Streamer streamer = Streamer.getStreamer(sclass); Streamer streamer = Streamer.getStreamer(object);
// we specifically do not inline the getStreamer() call // we specifically do not inline the getStreamer() call
// into the ClassMapping constructor because we want to be // into the ClassMapping constructor because we want to be
// sure not to call _nextCode++ if getStreamer() throws an // sure not to call _nextCode++ if getStreamer() throws an
@@ -146,7 +148,7 @@ public class ObjectOutputStream extends DataOutputStream
public void writeBareObject (Object object) public void writeBareObject (Object object)
throws IOException throws IOException
{ {
writeBareObject(object, Streamer.getStreamer(object.getClass()), true); writeBareObject(object, Streamer.getStreamer(object), true);
} }
/** /**
+14 -7
View File
@@ -80,6 +80,18 @@ public class Streamer
} }
} }
/**
* Returns the class that should be used when streaming this object. In
* general that is the object's natural class, but for enum values, that
* might be its declaring class as enums use classes in a way that would
* otherwise pollute our id to class mapping space.
*/
public static Class getStreamerClass (Object object)
{
return (object instanceof Enum) ?
((Enum)object).getDeclaringClass() : object.getClass();
}
/** /**
* Obtains a {@link Streamer} that can be used to read and write * Obtains a {@link Streamer} that can be used to read and write
* objects of the specified target class. {@link Streamer} instances * objects of the specified target class. {@link Streamer} instances
@@ -90,7 +102,7 @@ public class Streamer
* does not implement {@link Streamable} and is not one of the basic * does not implement {@link Streamable} and is not one of the basic
* object types (@see {@link ObjectOutputStream}). * object types (@see {@link ObjectOutputStream}).
*/ */
public synchronized static Streamer getStreamer (Class target) public synchronized static Streamer getStreamer (Object object)
throws IOException throws IOException
{ {
// if we have not yet initialized ourselves, do so now // if we have not yet initialized ourselves, do so now
@@ -98,12 +110,7 @@ public class Streamer
createStreamers(); createStreamers();
} }
// if the target class is an enum, convert it from its potentially Class target = getStreamerClass(object);
// enum-value specific class to the main class for that enum
if (target.isEnum()) {
target = target.getDeclaringClass();
}
Streamer stream = _streamers.get(target); Streamer stream = _streamers.get(target);
if (stream == null) { if (stream == null) {
// make sure this is a streamable class // make sure this is a streamable class