improve Reflection.lub() behavior for nested arrays
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index 2cc7b0a..a1c1dde 100644 (file)
@@ -1,8 +1,11 @@
+// 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 {
     
@@ -94,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;
     }
 
@@ -210,62 +215,67 @@ public final class Reflection {
         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));
+        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;
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new RuntimeException(e);
         }
+        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 {
-            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);
+            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 {
-            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);
+            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) {
@@ -279,6 +289,13 @@ public final class Reflection {
     }
 
     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) {
@@ -315,12 +332,10 @@ public final class Reflection {
             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);
             }
         }