Finally incorporated Par's Enum streaming support, modified to use the string
value to avoid future compatibility problems if someone saves an object to a database and then later adds a new enum anywhere but at the end of the list, thereby changing the ordinals. If you want maximal network efficiency, don't use enums. For most of what we'll do with them, it doesn't merit having the future incompatibility hitch of sending the ordinal. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4369 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -64,15 +64,19 @@ public class Streamer
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if it's an array, check the component type
|
// enums can be streamed
|
||||||
if (target.isArray()) {
|
if (target.isEnum()) {
|
||||||
target = target.getComponentType();
|
return true;
|
||||||
// and loop back around for another check with the comp type...
|
}
|
||||||
|
|
||||||
} else {
|
// if it's not an array, it must be streamable
|
||||||
// it'll be ok if the type (or component type) is Streamable
|
if (!target.isArray()) {
|
||||||
return Streamable.class.isAssignableFrom(target);
|
return Streamable.class.isAssignableFrom(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// otherwise extract the component type and loop back around for
|
||||||
|
// another check...
|
||||||
|
target = target.getComponentType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +201,14 @@ public class Streamer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we're writing an enum; write its string value (to avoid future
|
||||||
|
// compatibility issues if someone serializes an enum to a file and
|
||||||
|
// then adds a value to the enum, changing the ordinal assignments)
|
||||||
|
if (_target.isEnum()) {
|
||||||
|
out.writeUTF(((Enum)object).name());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// otherwise simply write out the fields via our field marshallers
|
// otherwise simply write out the fields via our field marshallers
|
||||||
int fcount = _fields.length;
|
int fcount = _fields.length;
|
||||||
for (int ii = 0; ii < fcount; ii++) {
|
for (int ii = 0; ii < fcount; ii++) {
|
||||||
@@ -245,6 +257,16 @@ public class Streamer
|
|||||||
"[" + length + "]'.");
|
"[" + length + "]'.");
|
||||||
}
|
}
|
||||||
return Array.newInstance(_target.getComponentType(), length);
|
return Array.newInstance(_target.getComponentType(), length);
|
||||||
|
|
||||||
|
} else if (_target.isEnum()) {
|
||||||
|
if (ObjectInputStream.STREAM_DEBUG) {
|
||||||
|
log.info(in.hashCode() + ": Creating enum '" +
|
||||||
|
_target.getName() + "'.");
|
||||||
|
}
|
||||||
|
@SuppressWarnings("unchecked") Object value =
|
||||||
|
Enum.valueOf(_target, in.readUTF());
|
||||||
|
return value;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (ObjectInputStream.STREAM_DEBUG) {
|
if (ObjectInputStream.STREAM_DEBUG) {
|
||||||
log.info(in.hashCode() + ": Creating object '" +
|
log.info(in.hashCode() + ": Creating object '" +
|
||||||
@@ -347,6 +369,11 @@ public class Streamer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// an enum has no additional state
|
||||||
|
if (_target.isEnum()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// otherwise simply read the fields via our field marshallers
|
// otherwise simply read the fields via our field marshallers
|
||||||
int fcount = _fields.length;
|
int fcount = _fields.length;
|
||||||
for (int ii = 0; ii < fcount; ii++) {
|
for (int ii = 0; ii < fcount; ii++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user