Implemented StringUtil.fieldsToString() using flash's introspection.

Use it in SimpleStreamableObject's toString(), with some new formatting.
Unfortunately, the order of public variables seems random.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4746 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2007-07-02 20:33:59 +00:00
parent 8022af7815
commit b1773889d2
2 changed files with 35 additions and 2 deletions
@@ -21,7 +21,9 @@
package com.threerings.io {
import com.threerings.util.ClassUtil;
import com.threerings.util.StringBuilder;
import com.threerings.util.StringUtil;
/**
* A simple serializable object implements the {@link Streamable}
@@ -48,8 +50,9 @@ public class SimpleStreamableObject implements Streamable
public function toString () :String
{
var buf :StringBuilder = new StringBuilder("[");
buf.append(ClassUtil.tinyClassName(this)).append("(");
toStringBuilder(buf);
return buf.append("]").toString();
return buf.append(")]").toString();
}
/**
@@ -58,7 +61,7 @@ public class SimpleStreamableObject implements Streamable
*/
protected function toStringBuilder (buf :StringBuilder): void
{
// TODO: StringUtil.fieldsToString(buf, this);
StringUtil.fieldsToString(buf, this);
}
}
}
+30
View File
@@ -23,6 +23,8 @@ package com.threerings.util {
import flash.utils.ByteArray;
import flash.utils.describeType; // function import
import mx.utils.*;
public class StringUtil
@@ -202,6 +204,34 @@ public class StringUtil
return String(obj);
}
/**
* Return a string containing all the public fields of the object
*/
public static function fieldsToString (buf :StringBuilder, obj :Object) :void
{
var desc :XML = describeType(obj);
var appended :Boolean = false;
for each (var varName :String in desc..variable.@name) {
if (appended) {
buf.append(", ");
}
buf.append(varName, "=", obj[varName]);
appended = true;
}
}
/**
* Return a pretty basic toString of the supplied Object.
*/
public static function simpleToString (obj :Object) :String
{
var buf :StringBuilder = new StringBuilder("[");
buf.append(ClassUtil.tinyClassName(obj));
buf.append("(");
fieldsToString(buf, obj);
return buf.append(")]").toString();
}
/**
* Truncate the specified String if it is longer than maxLength.
* The string will be truncated at a position such that it is