checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index 5c30d44..fd43a45 100644 (file)
@@ -6,7 +6,7 @@ import java.lang.reflect.*;
 public final class Reflection {
     
     public static Object rebuild(Object o, Class c) {
-        System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
+        //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
         if (o==null || c.isAssignableFrom(o.getClass())) return o;
         if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0));
         if (o instanceof Character && c == String.class) return o+"";
@@ -16,7 +16,7 @@ public final class Reflection {
                 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), a.length);
                 for(int i=0; i<ret.length; i++) {
                     Object o2 = rebuild(a[i], c.getComponentType());
-                    if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
+                    //if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
                     ret[i] = o2;
                 }
                 return ret;
@@ -51,6 +51,31 @@ 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 Object[])) return o.toString() + " : " + o.getClass().getName();
+        Object[] arr = (Object[])o;
+        StringBuffer ret = new StringBuffer();
+        ret.append(o.getClass().getComponentType().getName());
+        ret.append("[]:\n");
+        for(int i=0; i<arr.length; i++)
+            ret.append(indent(show(arr[i]), 4) + "\n");
+        return ret.toString();
+    }
+
     public static Object lub(Object argo) {
         if (argo instanceof Object[]) return lub((Object[])argo);
         return argo;