1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.util;
5 import java.lang.reflect.*;
6 import java.lang.annotation.*;
8 // FIXME: decent error reporting
9 /** Random reflection-related utilities */
10 public final class Reflection {
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;
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());
28 if (c == String.class) {
30 for(int i=0; i<a.length; i++)
31 if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
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())+""
43 } else if (c.isArray()) {
44 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
47 } else if (c==int.class && o instanceof Number) { return o;
49 throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
54 public static Object[] newArray(Class c, int i) {
55 return (Object[])Array.newInstance(c, i);
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();
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() + " = ");
76 ret.append(show(f.get(o)));
77 } catch (Exception e) {
78 ret.append("**"+e+"**");
79 throw new RuntimeException(e);
82 return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
85 public static Object lub(Object argo) {
86 if (argo instanceof Object[]) return lub((Object[])argo);
89 public static Object[] lub(Object[] argo) {
90 if (argo==null) return 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());
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]);
105 public static Class lub(Class a, Class b) {
106 if (a==null) return b;
107 if (b==null) 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());
116 public static Class arrayClass(Class c) {
117 return Reflection.newArray(c, 0).getClass();
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) {
123 return Class.forName(s);
124 } catch (ClassNotFoundException _) {
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) {
133 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
135 Object[] args2 = new Object[args.length];
136 System.arraycopy(args, 0, args2, 0, args.length);
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;
143 args[i] = rebuild(args[i], goal);
145 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
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]));
155 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
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);
166 private static String zoo(Object o) {
167 if (o==null) return "null";
168 if (o instanceof Object[]) {
170 for(int j=0; j<((Object[])o).length; j++)
171 ret += zoo(((Object[])o)[j]) + " ";
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;
182 public static boolean isStatic(Field f) {
183 if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
187 public static Field getField(Class c, String s) {
189 for(Field f : c.getDeclaredFields())
190 if (f.getName().equals(s))
192 } catch (Exception e) { }
193 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
194 return getField(c.getSuperclass(), s);
196 public static Field getField(Class c, int i) {
198 for(Field f : c.getDeclaredFields()) {
199 if (isStatic(f)) continue;
202 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
203 return getField(c.getSuperclass(), i);
204 } catch (Exception e) { }
208 public static interface Show {
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);
217 public static Object impose(Class _class, Object[] fields) {
219 try { ret = _class.newInstance(); }
220 catch (Exception e) { rethrow(e, "while trying to instantiate a " + _class.getName()); }
221 Field[] f = _class.getFields();
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);
227 tgt = coerce(tgt, f[i].getType());
230 } catch (Exception e) {
231 rethrow(e, "while setting \n " +
233 "\n to " + show(tgt));
239 public static Object rethrow(Exception e, String message) {
241 StackTraceElement[] st = e.getStackTrace();
242 RuntimeException re = new RuntimeException(e.getMessage() + "\n " + message);
243 re.setStackTrace(st);
247 public static Object impose(Method _method, Object[] fields) {
248 Object[] args = mkArgs(_method.getParameterTypes(), fields);
250 return _method.invoke(null, args);
251 } catch (Exception e) {
252 return rethrow(e, "while trying to invoke \n " +
254 "\n with arguments " + show(args));
257 public static Object impose(Constructor _ctor, Object[] fields) {
258 Object[] args = mkArgs(_ctor.getParameterTypes(), fields);
260 return _ctor.newInstance(args);
261 } catch (Exception e) {
262 return rethrow(e, "while trying to invoke \n " +
264 "\n with arguments " + show(args));
268 private static Object[] mkArgs(Class[] argTypes, Object[] fields) {
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);
275 tgt = Reflection.coerce(tgt, argTypes[i]);
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++)
288 return ret.toString();
291 public static Object coerce(Object o, Class c) {
293 return coerce0(o, c);
294 } catch (Exception e) {
295 return (Object[])rethrow(e, "while trying to coerce " + show(o) + " to a " + c.getName());
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);
304 if (c==int.class || c==Integer.class) {
305 String s = (String)coerce(o, String.class);
306 return new Integer(Integer.parseInt(s));
309 if (o.getClass().isArray() &&
310 o.getClass().getComponentType().isArray() &&
311 o.getClass().getComponentType().getComponentType() == String.class &&
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]))
319 ret[i] = sb.toString();
324 if (c.isArray() && (c.getComponentType().isInstance(o))) {
325 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
330 if (o.getClass().isArray() && c.isArray()) {
332 for(int i=0; i<((Object[])o).length; i++) {
333 Object ob = (((Object[])o)[i]);
339 return Array.newInstance(c.getComponentType(), ((Object[])o).length);