1 package edu.berkeley.sbp.util;
3 import java.lang.reflect.*;
5 /** Random reflection-related utilities */
6 public final class Reflection {
8 private static Object rebuild(Object o, Class c) {
9 //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
10 if (o==null || c.isAssignableFrom(o.getClass())) return o;
11 if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0));
12 if (o instanceof Character && c == String.class) return o+"";
13 if (o instanceof Object[]) {
14 Object[] a = (Object[])o;
16 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), a.length);
17 for(int i=0; i<ret.length; i++) {
18 Object o2 = rebuild(a[i], c.getComponentType());
19 //if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
24 if (c == String.class) {
26 for(int i=0; i<a.length; i++)
27 if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
30 StringBuffer s = new StringBuffer();
31 for(int i=0; i<a.length; i++)
32 s.append(a[i] instanceof Character
33 ? (((Character)a[i]).charValue())+""
39 } else if (c.isArray()) {
40 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
44 throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
49 public static Object[] newArray(Class c, int i) {
50 return (Object[])Array.newInstance(c, i);
53 public static Object[] lub(Object[] argo) {
54 if (argo==null) return null;
56 for(Object o : argo) if (o != null) c = Reflection.lub(c, o.getClass());
57 if (c==null) c = Object.class;
58 Object[] ret = Reflection.newArray(c, argo.length);
59 System.arraycopy(argo, 0, ret, 0, argo.length);
63 public static Class lub(Class a, Class b) {
64 if (a==null) return b;
65 if (b==null) return a;
67 if (a.isAssignableFrom(b)) return a;
68 if (b.isAssignableFrom(a)) return b;
69 return lub(b, a.getSuperclass());
72 /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
73 public static Class forNameOrNull(String s) {
75 return Class.forName(s);
76 } catch (ClassNotFoundException _) {
81 public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
82 /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
83 public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
85 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
87 Object[] args2 = new Object[args.length];
88 System.arraycopy(args, 0, args2, 0, args.length);
90 for(int i=0; i<args.length; i++) {
91 Class goal = argTypes[i];
92 Class actual = args[i] == null ? null : args[i].getClass();
93 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
95 args[i] = rebuild(args[i], goal);
97 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
102 System.out.println("\ninvoking " + o + "." + m);
103 for(int i=0; i<args.length; i++) {
104 if (args[i] instanceof Object[]) {
105 System.out.print(" arg => " + zoo(args[i]));
107 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
111 // FIXME: deal with the case where it is an inner class ctor
112 return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
113 } catch (Exception e) {
114 throw new RuntimeException(e);
118 private static String zoo(Object o) {
119 if (o==null) return "null";
120 if (o instanceof Object[]) {
122 for(int j=0; j<((Object[])o).length; j++)
123 ret += zoo(((Object[])o)[j]) + " ";