cf683f0871140f77def03ce5995d692153d06ca2
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
1 package edu.berkeley.sbp.util;
2 import java.io.*;
3 import java.lang.reflect.*;
4
5 /** Random reflection-related utilities */
6 public final class Reflection {
7
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;
15             if (c.isArray()) {
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());
20                     ret[i] = o2;
21                 }
22                 return ret;
23             }
24             if (c == String.class) {
25                 boolean ok = true;
26                 for(int i=0; i<a.length; i++) if (a[i]==null || !(a[i] instanceof Character)) ok = false;
27                 if (ok) {
28                     StringBuffer s = new StringBuffer();
29                     for(int i=0; i<a.length; i++) s.append((((Character)a[i])).charValue());
30                     return s.toString();
31                 }
32             }
33         } else if (c.isArray()) {
34             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
35             ret[0] = o;
36             return ret;
37         } else {
38             throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
39         }
40         return o;
41     }
42
43     public static Object[] newArray(Class c, int i) {
44         return (Object[])Array.newInstance(c, i);
45     }
46
47     public static Object[] lub(Object[] argo) {
48         if (argo==null) return null;
49         Class c = 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);
54         return ret;
55     }
56
57     public static Class lub(Class a, Class b) {
58         if (a==null) return b;
59         if (b==null) return a;
60         if (a==b) return a;
61         if (a.isAssignableFrom(b)) return a;
62         if (b.isAssignableFrom(a)) return b;
63         return lub(b, a.getSuperclass());
64     }
65
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) {
68         try {
69             return Class.forName(s); 
70         } catch (ClassNotFoundException _) {
71             return null;
72         }
73     }
74
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) {
77         try {
78             Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
79             boolean ok = true;
80             Object[] args2 = new Object[args.length];
81             System.arraycopy(args, 0, args2, 0, args.length);
82             args = args2;
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;
87                 try {
88                     args[i] = rebuild(args[i], goal);
89                 } catch (Error e) {
90                     throw new Error(e.getMessage() + "\n   while trying to fuzzyInvoke("+m.getName()+")");
91                 }
92             }
93             // for debugging
94             /*
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]));
99                 } else {
100                     System.out.println("  arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
101                 }
102             }
103             */
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);
108         }
109     }
110
111     private static String zoo(Object o) {
112         if (o==null) return "null";
113         if (o instanceof Object[]) {
114             String ret = "[ ";
115             for(int j=0; j<((Object[])o).length; j++)
116                 ret += zoo(((Object[])o)[j]) + " ";
117             return ret+"]";
118         }
119         return o+"";
120     }
121
122 }