checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index c404ab5..247652a 100644 (file)
@@ -51,28 +51,28 @@ public final class Reflection {
         return (Object[])Array.newInstance(c, i);
     }
 
-    public static String indent(String s, int indent) {
-        if (s.indexOf('\n')==-1) return s;
-        StringBuffer ret = new StringBuffer();
-        for(int i=0; i<s.length(); i++) {
-            char c = s.charAt(i);
-            ret.append(c);
-            if (c=='\n')
-                for(int j=0; j<indent; j++)
-                    ret.append(' ');
-        }
-        return ret.toString();
-    }
-
     public static String show(Object o) {
         if (o==null) return "null";
+        if (o instanceof Show) {
+            StringBuffer ret = new StringBuffer();
+            for(Field f : o.getClass().getFields()) {
+                ret.append("\n" + f.getName() + " = ");
+                try {
+                    ret.append(show(f.get(o)));
+                } catch (Exception e) {
+                    ret.append("**"+e+"**");
+                    throw new RuntimeException(e);
+                }
+            }
+            return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
+        }
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
         ret.append(o.getClass().getComponentType().getName());
-        ret.append("["+arr.length+"]:\n");
+        ret.append("["+arr.length+"]:");
         for(int i=0; i<arr.length; i++)
-            ret.append(indent(show(arr[i]), 4) + "\n");
+            ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
         return ret.toString();
     }
 
@@ -197,4 +197,5 @@ public final class Reflection {
         return null;
     }
 
+    public static interface Show { }
 }