checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index 5c30d44..3a129d4 100644 (file)
@@ -1,12 +1,13 @@
 package edu.berkeley.sbp.util;
 import java.io.*;
 import java.lang.reflect.*;
+import java.lang.annotation.*;
 
 /** 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 +17,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 +52,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)o).toString();
+        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;
@@ -172,4 +200,188 @@ 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) {
+        try {
+            Object ret = _class.newInstance();
+            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());
+                System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
+                f[i].set(ret, tgt);
+            }
+            return ret;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static Object impose(Method _method, Object[] fields) {
+        try {
+            Class[] argTypes = _method.getParameterTypes();
+            Object[] args = new Object[argTypes.length];
+            int j = 0;
+            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]);
+                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+                args[i] = tgt;
+            }
+            System.err.println("invoking " + _method + " with " + Reflection.show(args));
+            return _method.invoke(null, args);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static Object impose(Constructor _ctor, Object[] fields) {
+        try {
+            Class[] argTypes = _ctor.getParameterTypes();
+            Object[] args = new Object[argTypes.length];
+            int j = 0;
+            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]);
+                System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
+                args[i] = tgt;
+            }
+            return _ctor.newInstance(args);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    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) {
+        if (o==null) return null;
+        if (c.isInstance(o)) return o;
+        if (c == char.class) {
+            return o.toString().charAt(0);
+        }
+
+        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) {
+                    System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
+                    ok = false;
+                }
+            }
+            if (ok) {
+                System.err.println("hit with " + c.getComponentType().getName());
+                return Array.newInstance(c.getComponentType(), ((Object[])o).length);
+            }
+        }
+        return o;
+    }
+
+    public static abstract class Bindable {
+        public abstract String getSimpleName();
+        public abstract String toString();
+        public abstract <A extends Annotation> A getAnnotation(Class<A> c);
+        public abstract Object impose(Object[] fields);
+        public boolean isAnnotationPresent(Class<? extends Annotation> c) { return getAnnotation(c) != null; }
+
+        public abstract Annotation[][] getArgAnnotations();
+        public abstract String[]       getArgNames();
+
+        public static Bindable create(Object o) {
+            if (o instanceof Class) return new BindableClass((Class)o);
+            if (o instanceof Method) return new BindableMethod((Method)o);
+            if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
+            return null;
+        }
+    }
+
+    private static class BindableMethod extends Bindable {
+        private final Method _method;
+        public String toString() { return "BindableMethod["+_method+"]"; }
+        public BindableMethod(Method _method) { this._method = _method; }
+        public String getSimpleName() { return _method.getName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return _method.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
+        public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
+        public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
+    }
+    private static class BindableConstructor extends Bindable {
+        private final Constructor _constructor;
+        public String toString() { return "BindableConstructor["+_constructor+"]"; }
+        public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; }
+        public String getSimpleName() { return _constructor.getName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return _constructor.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
+        public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
+        public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
+    }
+    private static class BindableClass extends Bindable {
+        private final Class _class;
+        public String toString() { return "BindableClass["+_class+"]"; }
+        public BindableClass(Class _class) { this._class = _class; }
+        public String getSimpleName() { return _class.getSimpleName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return (A)_class.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_class, fields); }
+        public Annotation[][] getArgAnnotations() {
+            Field[] fields = _class.getFields();
+            Annotation[][] ret = new Annotation[fields.length][];
+            for(int i=0; i<fields.length; i++)
+                ret[i] = fields[i].getAnnotations();
+            return ret;
+        }
+        public String[]       getArgNames() {
+            Field[] fields = _class.getFields();
+            String[] ret = new String[fields.length];
+            for(int i=0; i<fields.length; i++)
+                ret[i] = fields[i].getName();
+            return ret;
+        }
+    }
+
 }