checkpoint
authoradam <adam@megacz.com>
Wed, 5 Jul 2006 01:33:21 +0000 (21:33 -0400)
committeradam <adam@megacz.com>
Wed, 5 Jul 2006 01:33:21 +0000 (21:33 -0400)
darcs-hash:20060705013321-5007d-58578e9b29a643d36f1f5eb30b142c3c7f2028c7.gz

src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/util/Reflection.java

index 2891b0f..359f715 100644 (file)
@@ -283,34 +283,9 @@ public class Demo {
             return null;
         }
         public Object plant(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);
-            }
+            return Reflection.impose(_class, fields);
         }
-    }
-
-    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 class TargetConstructor extends Target {
@@ -346,9 +321,9 @@ public class Demo {
                 int j = 0;
                 for(int i=0; i<args.length; i++) {
                     Object tgt = Reflection.lub(fields[i]);
-                    if (argTypes[i] == String.class) tgt = stringify(tgt);
+                    if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                     // FUGLY
-                    tgt = coerce(tgt, argTypes[i]);
+                    tgt = Reflection.coerce(tgt, argTypes[i]);
                     System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                     args[i] = tgt;
                 }
@@ -391,9 +366,9 @@ public class Demo {
                 int j = 0;
                 for(int i=0; i<args.length; i++) {
                     Object tgt = Reflection.lub(fields[i]);
-                    if (argTypes[i] == String.class) tgt = stringify(tgt);
+                    if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
                     // FUGLY
-                    tgt = coerce(tgt, argTypes[i]);
+                    tgt = Reflection.coerce(tgt, argTypes[i]);
                     System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
                     args[i] = tgt;
                 }
@@ -405,51 +380,6 @@ public class Demo {
         }
     }
 
-    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 Union cached = null;
     public static Union make() {
         if (cached != null) return cached;
index da16336..327a1dd 100644 (file)
@@ -201,4 +201,80 @@ public final class Reflection {
 
     public static interface Show {
     }
+
+    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 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;
+    }
+
 }