checkpoint
[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     public 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++)
27                     if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
28                         ok = false;
29                 if (ok) {
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())+""
34                                  : (String)a[i]
35                                  );
36                     return s.toString();
37                 }
38             }
39         } else if (c.isArray()) {
40             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
41             ret[0] = o;
42             return ret;
43         } else if (c==int.class && o instanceof Number) { return o;
44         } else {
45             throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
46         }
47         return o;
48     }
49
50     public static Object[] newArray(Class c, int i) {
51         return (Object[])Array.newInstance(c, i);
52     }
53
54     public static String show(Object o) {
55         if (o==null) return "null";
56         if (o instanceof Show) return ((Show)o).toString();
57         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
58         Object[] arr = (Object[])o;
59         StringBuffer ret = new StringBuffer();
60         ret.append(o.getClass().getComponentType().getName());
61         ret.append("["+arr.length+"]:");
62         for(int i=0; i<arr.length; i++)
63             ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
64         return ret.toString();
65     }
66
67     public static String show(Show o) {
68         StringBuffer ret = new StringBuffer();
69         for(Field f : o.getClass().getFields()) {
70             ret.append("\n" + f.getName() + " = ");
71             try {
72                 ret.append(show(f.get(o)));
73             } catch (Exception e) {
74                 ret.append("**"+e+"**");
75                 throw new RuntimeException(e);
76             }
77         }
78         return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
79     }
80
81     public static Object lub(Object argo) {
82         if (argo instanceof Object[]) return lub((Object[])argo);
83         return argo;
84     }
85     public static Object[] lub(Object[] argo) {
86         if (argo==null) return null;
87         Class c = null;
88         for(int i=0; i<argo.length; i++) {
89             if (argo[i]==null) continue;
90             argo[i] = lub(argo[i]);
91             c = Reflection.lub(c, argo[i].getClass());
92         }
93         if (c==null) c = Object.class;
94         Object[] ret = Reflection.newArray(c, argo.length);
95         System.arraycopy(argo, 0, ret, 0, argo.length);
96         return ret;
97     }
98
99     public static Class lub(Class a, Class b) {
100         if (a==null) return b;
101         if (b==null) return a;
102         if (a==b) return a;
103         if (a.isAssignableFrom(b)) return a;
104         if (b.isAssignableFrom(a)) return b;
105         if (a.isArray() && b.isArray())
106             return arrayClass(lub(a.getComponentType(), b.getComponentType()));
107         return lub(b, a.getSuperclass());
108     }
109
110     public static Class arrayClass(Class c) {
111         return Reflection.newArray(c, 0).getClass();
112     }
113
114     /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
115     public static Class forNameOrNull(String s) {
116         try {
117             return Class.forName(s); 
118         } catch (ClassNotFoundException _) {
119             return null;
120         }
121     }
122
123     public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
124     /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
125     public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
126         try {
127             Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
128             boolean ok = true;
129             Object[] args2 = new Object[args.length];
130             System.arraycopy(args, 0, args2, 0, args.length);
131             args = args2;
132             for(int i=0; i<args.length; i++) {
133                 Class goal = argTypes[i];
134                 Class actual = args[i] == null ? null : args[i].getClass();
135                 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
136                 try {
137                     args[i] = rebuild(args[i], goal);
138                 } catch (Error e) {
139                     throw new Error(e.getMessage() + "\n   while trying to fuzzyInvoke("+m.getName()+")");
140                 }
141             }
142             // for debugging
143             /*
144             System.out.println("\ninvoking " + o + "." + m);
145             for(int i=0; i<args.length; i++) {
146                 if (args[i] instanceof Object[]) {
147                     System.out.print("  arg => " + zoo(args[i]));
148                 } else {
149                     System.out.println("  arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
150                 }
151             }
152             */
153             // FIXME: deal with the case where it is an inner class ctor
154             return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
155         } catch (Exception e) {
156             throw new RuntimeException(e);
157         }
158     }
159
160     private static String zoo(Object o) {
161         if (o==null) return "null";
162         if (o instanceof Object[]) {
163             String ret = "[ ";
164             for(int j=0; j<((Object[])o).length; j++)
165                 ret += zoo(((Object[])o)[j]) + " ";
166             return ret+"]";
167         }
168         return o+"";
169     }
170
171     public static boolean isConcrete(Class c) {
172         if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false;
173         if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
174         return true;
175     }
176     public static boolean isStatic(Field f) {
177         if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
178         return false;
179     }
180
181     public static Field getField(Class c, String s) {
182         try {
183             for(Field f : c.getDeclaredFields())
184                 if (f.getName().equals(s))
185                     return f;
186         } catch (Exception e) { }
187         if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
188         return getField(c.getSuperclass(), s);
189     }
190     public static Field getField(Class c, int i) {
191         try {
192             for(Field f : c.getDeclaredFields()) {
193                 if (isStatic(f)) continue;
194                 return f;
195             }
196             if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
197             return getField(c.getSuperclass(), i);
198         } catch (Exception e) { }
199         return null;
200     }
201
202     public static interface Show {
203     }
204
205     public static Object impose(Class _class, Object[] fields) {
206         try {
207             Object ret = _class.newInstance();
208             Field[] f = _class.getFields();
209             int j = 0;
210             for(int i=0; i<f.length; i++) {
211                 Object tgt = Reflection.lub(fields[i]);
212                 if (f[i].getType() == String.class) tgt = stringify(tgt);
213                 // FUGLY
214                 tgt = coerce(tgt, f[i].getType());
215                 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
216                 f[i].set(ret, tgt);
217             }
218             return ret;
219         } catch (Exception e) {
220             e.printStackTrace();
221             throw new RuntimeException(e);
222         }
223     }
224
225     public static String stringify(Object o) {
226         if (o==null) return "";
227         if (!(o instanceof Object[])) return o.toString();
228         Object[] arr = (Object[])o;
229         StringBuffer ret = new StringBuffer();
230         for(int i=0; i<arr.length; i++)
231             ret.append(arr[i]);
232         return ret.toString();
233     }
234
235     public static Object coerce(Object o, Class c) {
236         if (o==null) return null;
237         if (c.isInstance(o)) return o;
238         if (c == char.class) {
239             return o.toString().charAt(0);
240         }
241
242         if (o.getClass().isArray() &&
243             o.getClass().getComponentType().isArray() &&
244             o.getClass().getComponentType().getComponentType() == String.class &&
245             c.isArray() &&
246             c.getComponentType() == String.class) {
247             String[] ret = new String[((Object[])o).length];
248             for(int i=0; i<ret.length; i++) {
249                 StringBuffer sb = new StringBuffer();
250                 for(Object ob : (Object[])(((Object[])o)[i]))
251                     sb.append(ob);
252                 ret[i] = sb.toString();
253             }
254             return ret;
255         }
256
257         if (c.isArray() && (c.getComponentType().isInstance(o))) {
258             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
259             ret[0] = o;
260             return ret;
261         }
262
263         if (o.getClass().isArray() && c.isArray()) {
264             boolean ok = true;
265             for(int i=0; i<((Object[])o).length; i++) {
266                 Object ob = (((Object[])o)[i]);
267                 if (ob != null) {
268                     System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
269                     ok = false;
270                 }
271             }
272             if (ok) {
273                 System.err.println("hit with " + c.getComponentType().getName());
274                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
275             }
276         }
277         return o;
278     }
279
280 }