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 {
Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
ret[0] = o;
return ret;
+ } else if (c==int.class && o instanceof Number) { return o;
} else {
throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
}
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;
if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
return true;
}
+ public static boolean isStatic(Field f) {
+ if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
+ return false;
+ }
public static Field getField(Class c, String s) {
try {
if (f.getName().equals(s))
return f;
} catch (Exception e) { }
+ if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
+ return getField(c.getSuperclass(), s);
+ }
+ public static Field getField(Class c, int i) {
+ try {
+ for(Field f : c.getDeclaredFields()) {
+ if (isStatic(f)) continue;
+ return f;
+ }
+ if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
+ return getField(c.getSuperclass(), i);
+ } catch (Exception e) { }
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;
+ }
+
}