optimizations to IntPairMap.java
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.util;
4 import java.io.*;
5 import java.lang.reflect.*;
6 import java.lang.annotation.*;
7
8 // FIXME: decent error reporting
9 /** Random reflection-related utilities */
10 public final class Reflection {
11     
12     public static Object rebuild(Object o, Class c) {
13         //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
14         if (o==null || c.isAssignableFrom(o.getClass())) return o;
15         if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0));
16         if (o instanceof Character && c == String.class) return o+"";
17         if (o instanceof Object[]) {
18             Object[] a = (Object[])o;
19             if (c.isArray()) {
20                 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), a.length);
21                 for(int i=0; i<ret.length; i++) {
22                     Object o2 = rebuild(a[i], c.getComponentType());
23                     //if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
24                     ret[i] = o2;
25                 }
26                 return ret;
27             }
28             if (c == String.class) {
29                 boolean ok = true;
30                 for(int i=0; i<a.length; i++)
31                     if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
32                         ok = false;
33                 if (ok) {
34                     StringBuffer s = new StringBuffer();
35                     for(int i=0; i<a.length; i++)
36                         s.append(a[i] instanceof Character
37                                  ? (((Character)a[i]).charValue())+""
38                                  : (String)a[i]
39                                  );
40                     return s.toString();
41                 }
42             }
43         } else if (c.isArray()) {
44             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
45             ret[0] = o;
46             return ret;
47         } else if (c==int.class && o instanceof Number) { return o;
48         } else {
49             throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
50         }
51         return o;
52     }
53
54     public static Object[] newArray(Class c, int i) {
55         return (Object[])Array.newInstance(c, i);
56     }
57
58     public static String show(Object o) {
59         if (o==null) return "null";
60         if (o instanceof Show) return show((Show)o);
61         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
62         Object[] arr = (Object[])o;
63         StringBuffer ret = new StringBuffer();
64         ret.append(o.getClass().getComponentType().getName());
65         ret.append("["+arr.length+"]:");
66         for(int i=0; i<arr.length; i++)
67             ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
68         return ret.toString();
69     }
70
71     public static String show(Show o) {
72         StringBuffer ret = new StringBuffer();
73         for(Field f : o.getClass().getFields()) {
74             ret.append("\n" + f.getName() + " = ");
75             try {
76                 ret.append(show(f.get(o)));
77             } catch (Exception e) {
78                 ret.append("**"+e+"**");
79                 throw new RuntimeException(e);
80             }
81         }
82         return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
83     }
84
85     public static Object lub(Object argo) {
86         if (argo instanceof Object[]) return lub((Object[])argo);
87         return argo;
88     }
89     public static Object[] lub(Object[] argo) {
90         if (argo==null) return null;
91         Class c = null;
92         for(int i=0; i<argo.length; i++) {
93             if (argo[i]==null) continue;
94             argo[i] = lub(argo[i]);
95             c = Reflection.lub(c, argo[i].getClass());
96         }
97         if (c==null) c = Object.class;
98         Object[] ret = Reflection.newArray(c, argo.length);
99         System.arraycopy(argo, 0, ret, 0, argo.length);
100         for(int i=0; i<ret.length; i++)
101             ret[i] = lub(ret[i]);
102         return ret;
103     }
104
105     public static Class lub(Class a, Class b) {
106         if (a==null) return b;
107         if (b==null) return a;
108         if (a==b) return a;
109         if (a.isAssignableFrom(b)) return a;
110         if (b.isAssignableFrom(a)) return b;
111         if (a.isArray() && b.isArray())
112             return arrayClass(lub(a.getComponentType(), b.getComponentType()));
113         return lub(b, a.getSuperclass());
114     }
115
116     public static Class arrayClass(Class c) {
117         return Reflection.newArray(c, 0).getClass();
118     }
119
120     /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
121     public static Class forNameOrNull(String s) {
122         try {
123             return Class.forName(s); 
124         } catch (ClassNotFoundException _) {
125             return null;
126         }
127     }
128
129     public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
130     /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
131     public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
132         try {
133             Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
134             boolean ok = true;
135             Object[] args2 = new Object[args.length];
136             System.arraycopy(args, 0, args2, 0, args.length);
137             args = args2;
138             for(int i=0; i<args.length; i++) {
139                 Class goal = argTypes[i];
140                 Class actual = args[i] == null ? null : args[i].getClass();
141                 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
142                 try {
143                     args[i] = rebuild(args[i], goal);
144                 } catch (Error e) {
145                     throw new Error(e.getMessage() + "\n   while trying to fuzzyInvoke("+m.getName()+")");
146                 }
147             }
148             // for debugging
149             /*
150             System.out.println("\ninvoking " + o + "." + m);
151             for(int i=0; i<args.length; i++) {
152                 if (args[i] instanceof Object[]) {
153                     System.out.print("  arg => " + zoo(args[i]));
154                 } else {
155                     System.out.println("  arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
156                 }
157             }
158             */
159             // FIXME: deal with the case where it is an inner class ctor
160             return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
161         } catch (Exception e) {
162             throw new RuntimeException(e);
163         }
164     }
165
166     private static String zoo(Object o) {
167         if (o==null) return "null";
168         if (o instanceof Object[]) {
169             String ret = "[ ";
170             for(int j=0; j<((Object[])o).length; j++)
171                 ret += zoo(((Object[])o)[j]) + " ";
172             return ret+"]";
173         }
174         return o+"";
175     }
176
177     public static boolean isConcrete(Class c) {
178         if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false;
179         if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
180         return true;
181     }
182     public static boolean isStatic(Field f) {
183         if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
184         return false;
185     }
186
187     public static Field getField(Class c, String s) {
188         try {
189             for(Field f : c.getDeclaredFields())
190                 if (f.getName().equals(s))
191                     return f;
192         } catch (Exception e) { }
193         if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
194         return getField(c.getSuperclass(), s);
195     }
196     public static Field getField(Class c, int i) {
197         try {
198             for(Field f : c.getDeclaredFields()) {
199                 if (isStatic(f)) continue;
200                 return f;
201             }
202             if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
203             return getField(c.getSuperclass(), i);
204         } catch (Exception e) { }
205         return null;
206     }
207
208     public static interface Show {
209     }
210
211     public static Object impose(Object o, Object[] fields) {
212         if (o instanceof Class) return impose((Class)o, fields);
213         if (o instanceof Method) return impose((Method)o, fields);
214         if (o instanceof Constructor) return impose((Constructor)o, fields);
215         return null;
216     }
217     public static Object impose(Class _class, Object[] fields) {
218         Object ret = null;
219         try { ret = _class.newInstance(); }
220         catch (Exception e) { rethrow(e, "while trying to instantiate a " + _class.getName()); }
221         Field[] f = _class.getFields();
222         int j = 0;
223         for(int i=0; i<f.length; i++) {
224             Object tgt = Reflection.lub(fields[i]);
225             if (f[i].getType() == String.class) tgt = stringify(tgt);
226             // FUGLY
227             tgt = coerce(tgt, f[i].getType());
228             try {
229                 f[i].set(ret, tgt);
230             } catch (Exception e) {
231                 rethrow(e, "while setting \n    " +
232                         f[i] +
233                         "\n     to " + show(tgt));
234             }
235         }
236         return ret;
237     }
238
239     public static Object rethrow(Exception e, String message) {
240         e.printStackTrace();
241         StackTraceElement[] st = e.getStackTrace();
242         RuntimeException re = new RuntimeException(e.getMessage() + "\n  " + message);
243         re.setStackTrace(st);
244         throw re;
245     }
246
247     public static Object impose(Method _method, Object[] fields) {
248         Object[] args = mkArgs(_method.getParameterTypes(), fields);
249         try {
250             return _method.invoke(null, args);
251         } catch (Exception e) {
252             return rethrow(e, "while trying to invoke \n    " +
253                            _method +
254                            "\n    with arguments " + show(args));
255         }
256     }
257     public static Object impose(Constructor _ctor, Object[] fields) {
258         Object[] args = mkArgs(_ctor.getParameterTypes(), fields);
259         try {
260             return _ctor.newInstance(args);
261         } catch (Exception e) {
262             return rethrow(e, "while trying to invoke \n    " +
263                            _ctor +
264                            "\n    with arguments " + show(args));
265         }
266     }
267
268     private static Object[] mkArgs(Class[] argTypes, Object[] fields) {
269         int j = 0;
270         Object[] args = new Object[argTypes.length];
271         for(int i=0; i<args.length; i++) {
272             Object tgt = Reflection.lub(fields[i]);
273             if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
274             // FUGLY
275             tgt = Reflection.coerce(tgt, argTypes[i]);
276             args[i] = tgt;
277         }
278         return args;
279     }
280
281     public static String stringify(Object o) {
282         if (o==null) return "";
283         if (!(o instanceof Object[])) return o.toString();
284         Object[] arr = (Object[])o;
285         StringBuffer ret = new StringBuffer();
286         for(int i=0; i<arr.length; i++)
287             ret.append(arr[i]);
288         return ret.toString();
289     }
290
291     public static Object coerce(Object o, Class c) {
292         try {
293             return coerce0(o, c);
294         } catch (Exception e) {
295             return (Object[])rethrow(e, "while trying to coerce " + show(o) + " to a " + c.getName());
296         }
297     }
298     public static Object coerce0(Object o, Class c) {
299         if (o==null) return null;
300         if (c.isInstance(o)) return o;
301         if (c == char.class) {
302             return o.toString().charAt(0);
303         }
304         if (c==int.class || c==Integer.class) {
305             String s = (String)coerce(o, String.class);
306             return new Integer(Integer.parseInt(s));
307         }
308
309         if (o.getClass().isArray() &&
310             o.getClass().getComponentType().isArray() &&
311             o.getClass().getComponentType().getComponentType() == String.class &&
312             c.isArray() &&
313             c.getComponentType() == String.class) {
314             String[] ret = new String[((Object[])o).length];
315             for(int i=0; i<ret.length; i++) {
316                 StringBuffer sb = new StringBuffer();
317                 for(Object ob : (Object[])(((Object[])o)[i]))
318                     sb.append(ob);
319                 ret[i] = sb.toString();
320             }
321             return ret;
322         }
323
324         if (c.isArray() && (c.getComponentType().isInstance(o))) {
325             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
326             ret[0] = o;
327             return ret;
328         }
329
330         if (o.getClass().isArray() && c.isArray()) {
331             boolean ok = true;
332             for(int i=0; i<((Object[])o).length; i++) {
333                 Object ob = (((Object[])o)[i]);
334                 if (ob != null) {
335                     ok = false;
336                 }
337             }
338             if (ok) {
339                 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
340             }
341         }
342         return o;
343     }
344
345 }