From 24d126dd63c1c773cedf500d63def11565a545f1 Mon Sep 17 00:00:00 2001 From: Mike Thomas Date: Mon, 7 Mar 2011 21:07:25 +0000 Subject: [PATCH] Allow the streamer to be used in a jvm-neutral way, streaming fields in alphabetical order. Sun JVMs use the order they're defined in the file, IBM JVMs apparently use the reverse of that, and Dalvik/Android JVMs stream in alphabetical order. Ideally, the alphabetical mode would be the default but this would break all kinds of legacy stuff, so I'm not going to make that the default. (I had done this before on the narya-1.5 tags - doing correctly on trunk now.) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6520 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/main/java/com/threerings/io/Streamer.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/threerings/io/Streamer.java b/src/main/java/com/threerings/io/Streamer.java index cc803853d..6a97a4129 100644 --- a/src/main/java/com/threerings/io/Streamer.java +++ b/src/main/java/com/threerings/io/Streamer.java @@ -30,7 +30,8 @@ import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -526,6 +527,15 @@ public class Streamer // reflect on all the object's fields and remove all marked with NotStreamable List fields = Lists.newArrayList(); ClassUtil.getFields(_target, fields); + + /** Checks whether or not we should stream the fields in alphabetical order. This ensures + * cross-JVM compatibility since Class.getDeclaredFields() does not define an order. Due + * to legacy issues, this is false by default. + */ + if (Boolean.getBoolean("com.threerings.io.streamFieldsAlphabetically")) { + Collections.sort(fields, FIELD_ALPHA_COMPARATOR); + } + _fields = Iterables.toArray(Iterables.filter(fields, _isStreamableFieldPred), Field.class); int fcount = _fields.length; @@ -550,6 +560,13 @@ public class Streamer } } + protected Comparator FIELD_ALPHA_COMPARATOR = new Comparator() { + public int compare (Field arg0, Field arg1) + { + return arg0.getName().compareTo(arg1.getName()); + } + }; + /** Used to coerce the type system into quietude when reading enums from the wire. */ protected static enum EnumReader implements ByteEnum { NOT_USED;