improve Reflection.lub() behavior for nested arrays
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index 5c30d44..a1c1dde 100644 (file)
@@ -1,12 +1,16 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp.util;
 import java.io.*;
 import java.lang.reflect.*;
+import java.lang.annotation.*;
 
+// FIXME: decent error reporting
 /** Random reflection-related utilities */
 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 +20,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 +55,33 @@ public final class Reflection {
         return (Object[])Array.newInstance(c, i);
     }
 
+    public static String show(Object o) {
+        if (o==null) return "null";
+        if (o instanceof Show) return show((Show)o);
+        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+"]:");
+        for(int i=0; i<arr.length; i++)
+            ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
+        return ret.toString();
+    }
+
+    public static String show(Show o) {
+        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}";
+    }
+
     public static Object lub(Object argo) {
         if (argo instanceof Object[]) return lub((Object[])argo);
         return argo;
@@ -66,6 +97,8 @@ public final class Reflection {
         if (c==null) c = Object.class;
         Object[] ret = Reflection.newArray(c, argo.length);
         System.arraycopy(argo, 0, ret, 0, argo.length);
+        for(int i=0; i<ret.length; i++)
+            ret[i] = lub(ret[i]);
         return ret;
     }
 
@@ -172,4 +205,141 @@ public final class Reflection {
         return null;
     }
 
+    public static interface Show {
+    }
+
+    public static Object impose(Object o, Object[] fields) {
+        if (o instanceof Class) return impose((Class)o, fields);
+        if (o instanceof Method) return impose((Method)o, fields);
+        if (o instanceof Constructor) return impose((Constructor)o, fields);
+        return null;
+    }
+    public static Object impose(Class _class, Object[] fields) {
+        Object ret = null;
+        try { ret = _class.newInstance(); }
+        catch (Exception e) { rethrow(e, "while trying to instantiate a " + _class.getName()); }
+        Field[] f = _class.getFields();
+        int j = 0;
+        for(int i=0; i<f.length; i++) {
+            Object tgt = Reflection.lub(fields[i]);
+            if (f[i].getType() == String.class) tgt = stringify(tgt);
+            // FUGLY
+            tgt = coerce(tgt, f[i].getType());
+            try {
+                f[i].set(ret, tgt);
+            } catch (Exception e) {
+                rethrow(e, "while setting \n    " +
+                        f[i] +
+                        "\n     to " + show(tgt));
+            }
+        }
+        return ret;
+    }
+
+    public static Object rethrow(Exception e, String message) {
+        e.printStackTrace();
+        StackTraceElement[] st = e.getStackTrace();
+        RuntimeException re = new RuntimeException(e.getMessage() + "\n  " + message);
+        re.setStackTrace(st);
+        throw re;
+    }
+
+    public static Object impose(Method _method, Object[] fields) {
+        Object[] args = mkArgs(_method.getParameterTypes(), fields);
+        try {
+            return _method.invoke(null, args);
+        } catch (Exception e) {
+            return rethrow(e, "while trying to invoke \n    " +
+                           _method +
+                           "\n    with arguments " + show(args));
+        }
+    }
+    public static Object impose(Constructor _ctor, Object[] fields) {
+        Object[] args = mkArgs(_ctor.getParameterTypes(), fields);
+        try {
+            return _ctor.newInstance(args);
+        } catch (Exception e) {
+            return rethrow(e, "while trying to invoke \n    " +
+                           _ctor +
+                           "\n    with arguments " + show(args));
+        }
+    }
+
+    private static Object[] mkArgs(Class[] argTypes, Object[] fields) {
+        int j = 0;
+        Object[] args = new Object[argTypes.length];
+        for(int i=0; i<args.length; i++) {
+            Object tgt = Reflection.lub(fields[i]);
+            if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
+            // FUGLY
+            tgt = Reflection.coerce(tgt, argTypes[i]);
+            args[i] = tgt;
+        }
+        return args;
+    }
+
+    public static String stringify(Object o) {
+        if (o==null) return "";
+        if (!(o instanceof Object[])) return o.toString();
+        Object[] arr = (Object[])o;
+        StringBuffer ret = new StringBuffer();
+        for(int i=0; i<arr.length; i++)
+            ret.append(arr[i]);
+        return ret.toString();
+    }
+
+    public static Object coerce(Object o, Class c) {
+        try {
+            return coerce0(o, c);
+        } catch (Exception e) {
+            return (Object[])rethrow(e, "while trying to coerce " + show(o) + " to a " + c.getName());
+        }
+    }
+    public static Object coerce0(Object o, Class c) {
+        if (o==null) return null;
+        if (c.isInstance(o)) return o;
+        if (c == char.class) {
+            return o.toString().charAt(0);
+        }
+        if (c==int.class || c==Integer.class) {
+            String s = (String)coerce(o, String.class);
+            return new Integer(Integer.parseInt(s));
+        }
+
+        if (o.getClass().isArray() &&
+            o.getClass().getComponentType().isArray() &&
+            o.getClass().getComponentType().getComponentType() == String.class &&
+            c.isArray() &&
+            c.getComponentType() == String.class) {
+            String[] ret = new String[((Object[])o).length];
+            for(int i=0; i<ret.length; i++) {
+                StringBuffer sb = new StringBuffer();
+                for(Object ob : (Object[])(((Object[])o)[i]))
+                    sb.append(ob);
+                ret[i] = sb.toString();
+            }
+            return ret;
+        }
+
+        if (c.isArray() && (c.getComponentType().isInstance(o))) {
+            Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
+            ret[0] = o;
+            return ret;
+        }
+
+        if (o.getClass().isArray() && c.isArray()) {
+            boolean ok = true;
+            for(int i=0; i<((Object[])o).length; i++) {
+                Object ob = (((Object[])o)[i]);
+                if (ob != null) {
+                    ok = false;
+                }
+            }
+            if (ok) {
+                return Array.newInstance(c.getComponentType(), ((Object[])o).length);
+            }
+        }
+        return o;
+    }
+
 }