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++) if (a[i]==null || !(a[i] instanceof Character)) ok = false;
28 StringBuffer s = new StringBuffer();
29 for(int i=0; i<a.length; i++) s.append((((Character)a[i])).charValue());
33 } else if (c.isArray()) {
34 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
38 throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
43 public static Object[] newArray(Class c, int i) {
44 return (Object[])Array.newInstance(c, i);
47 public static Object[] lub(Object[] argo) {
48 if (argo==null) return null;
50 for(Object o : argo) if (o != null) c = Reflection.lub(c, o.getClass());
51 if (c==null) c = Object.class;
52 Object[] ret = Reflection.newArray(c, argo.length);
53 System.arraycopy(argo, 0, ret, 0, argo.length);
57 public static Class lub(Class a, Class b) {
58 if (a==null) return b;
59 if (b==null) return a;
61 if (a.isAssignableFrom(b)) return a;
62 if (b.isAssignableFrom(a)) return b;
63 return lub(b, a.getSuperclass());
66 /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
67 public static Class forNameOrNull(String s) {
69 return Class.forName(s);
70 } catch (ClassNotFoundException _) {
75 /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
76 public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
78 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
80 Object[] args2 = new Object[args.length];
81 System.arraycopy(args, 0, args2, 0, args.length);
83 for(int i=0; i<args.length; i++) {
84 Class goal = argTypes[i];
85 Class actual = args[i] == null ? null : args[i].getClass();
86 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
88 args[i] = rebuild(args[i], goal);
90 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
95 System.out.println("\ninvoking " + o + "." + m);
96 for(int i=0; i<args.length; i++) {
97 if (args[i] instanceof Object[]) {
98 System.out.print(" arg => " + zoo(args[i]));
100 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
104 // FIXME: deal with the case where it is an inner class ctor
105 return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
106 } catch (Exception e) {
107 throw new RuntimeException(e);
111 private static String zoo(Object o) {
112 if (o==null) return "null";
113 if (o instanceof Object[]) {
115 for(int j=0; j<((Object[])o).length; j++)
116 ret += zoo(((Object[])o)[j]) + " ";