1 package edu.berkeley.sbp.util;
3 import java.lang.reflect.*;
4 import java.lang.annotation.*;
6 /** Random reflection-related utilities */
7 public final class Reflection {
9 public static Object rebuild(Object o, Class c) {
10 //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c);
11 if (o==null || c.isAssignableFrom(o.getClass())) return o;
12 if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0));
13 if (o instanceof Character && c == String.class) return o+"";
14 if (o instanceof Object[]) {
15 Object[] a = (Object[])o;
17 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), a.length);
18 for(int i=0; i<ret.length; i++) {
19 Object o2 = rebuild(a[i], c.getComponentType());
20 //if (o2 != null) System.out.println("storing " + o2.getClass().getName() + " to " + c.getComponentType());
25 if (c == String.class) {
27 for(int i=0; i<a.length; i++)
28 if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
31 StringBuffer s = new StringBuffer();
32 for(int i=0; i<a.length; i++)
33 s.append(a[i] instanceof Character
34 ? (((Character)a[i]).charValue())+""
40 } else if (c.isArray()) {
41 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
44 } else if (c==int.class && o instanceof Number) { return o;
46 throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
51 public static Object[] newArray(Class c, int i) {
52 return (Object[])Array.newInstance(c, i);
55 public static String show(Object o) {
56 if (o==null) return "null";
57 if (o instanceof Show) return ((Show)o).toString();
58 if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
59 Object[] arr = (Object[])o;
60 StringBuffer ret = new StringBuffer();
61 ret.append(o.getClass().getComponentType().getName());
62 ret.append("["+arr.length+"]:");
63 for(int i=0; i<arr.length; i++)
64 ret.append(StringUtil.indent("\n"+show(arr[i]), 4));
65 return ret.toString();
68 public static String show(Show o) {
69 StringBuffer ret = new StringBuffer();
70 for(Field f : o.getClass().getFields()) {
71 ret.append("\n" + f.getName() + " = ");
73 ret.append(show(f.get(o)));
74 } catch (Exception e) {
75 ret.append("**"+e+"**");
76 throw new RuntimeException(e);
79 return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
82 public static Object lub(Object argo) {
83 if (argo instanceof Object[]) return lub((Object[])argo);
86 public static Object[] lub(Object[] argo) {
87 if (argo==null) return null;
89 for(int i=0; i<argo.length; i++) {
90 if (argo[i]==null) continue;
91 argo[i] = lub(argo[i]);
92 c = Reflection.lub(c, argo[i].getClass());
94 if (c==null) c = Object.class;
95 Object[] ret = Reflection.newArray(c, argo.length);
96 System.arraycopy(argo, 0, ret, 0, argo.length);
100 public static Class lub(Class a, Class b) {
101 if (a==null) return b;
102 if (b==null) return a;
104 if (a.isAssignableFrom(b)) return a;
105 if (b.isAssignableFrom(a)) return b;
106 if (a.isArray() && b.isArray())
107 return arrayClass(lub(a.getComponentType(), b.getComponentType()));
108 return lub(b, a.getSuperclass());
111 public static Class arrayClass(Class c) {
112 return Reflection.newArray(c, 0).getClass();
115 /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
116 public static Class forNameOrNull(String s) {
118 return Class.forName(s);
119 } catch (ClassNotFoundException _) {
124 public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
125 /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
126 public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
128 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
130 Object[] args2 = new Object[args.length];
131 System.arraycopy(args, 0, args2, 0, args.length);
133 for(int i=0; i<args.length; i++) {
134 Class goal = argTypes[i];
135 Class actual = args[i] == null ? null : args[i].getClass();
136 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
138 args[i] = rebuild(args[i], goal);
140 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
145 System.out.println("\ninvoking " + o + "." + m);
146 for(int i=0; i<args.length; i++) {
147 if (args[i] instanceof Object[]) {
148 System.out.print(" arg => " + zoo(args[i]));
150 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
154 // FIXME: deal with the case where it is an inner class ctor
155 return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
156 } catch (Exception e) {
157 throw new RuntimeException(e);
161 private static String zoo(Object o) {
162 if (o==null) return "null";
163 if (o instanceof Object[]) {
165 for(int j=0; j<((Object[])o).length; j++)
166 ret += zoo(((Object[])o)[j]) + " ";
172 public static boolean isConcrete(Class c) {
173 if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false;
174 if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
177 public static boolean isStatic(Field f) {
178 if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
182 public static Field getField(Class c, String s) {
184 for(Field f : c.getDeclaredFields())
185 if (f.getName().equals(s))
187 } catch (Exception e) { }
188 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
189 return getField(c.getSuperclass(), s);
191 public static Field getField(Class c, int i) {
193 for(Field f : c.getDeclaredFields()) {
194 if (isStatic(f)) continue;
197 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
198 return getField(c.getSuperclass(), i);
199 } catch (Exception e) { }
203 public static interface Show {
206 public static Object impose(Object o, Object[] fields) {
207 if (o instanceof Class) return impose((Class)o, fields);
208 if (o instanceof Method) return impose((Method)o, fields);
209 if (o instanceof Constructor) return impose((Constructor)o, fields);
212 public static Object impose(Class _class, Object[] fields) {
214 Object ret = _class.newInstance();
215 Field[] f = _class.getFields();
217 for(int i=0; i<f.length; i++) {
218 Object tgt = Reflection.lub(fields[i]);
219 if (f[i].getType() == String.class) tgt = stringify(tgt);
221 tgt = coerce(tgt, f[i].getType());
222 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
226 } catch (Exception e) {
228 throw new RuntimeException(e);
232 public static Object impose(Method _method, Object[] fields) {
234 Class[] argTypes = _method.getParameterTypes();
235 Object[] args = new Object[argTypes.length];
237 for(int i=0; i<args.length; i++) {
238 Object tgt = Reflection.lub(fields[i]);
239 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
241 tgt = Reflection.coerce(tgt, argTypes[i]);
242 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
245 System.err.println("invoking " + _method + " with " + Reflection.show(args));
246 return _method.invoke(null, args);
247 } catch (Exception e) {
248 throw new RuntimeException(e);
252 public static Object impose(Constructor _ctor, Object[] fields) {
254 Class[] argTypes = _ctor.getParameterTypes();
255 Object[] args = new Object[argTypes.length];
257 for(int i=0; i<args.length; i++) {
258 Object tgt = Reflection.lub(fields[i]);
259 if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
261 tgt = Reflection.coerce(tgt, argTypes[i]);
262 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
265 return _ctor.newInstance(args);
266 } catch (Exception e) {
267 throw new RuntimeException(e);
271 public static String stringify(Object o) {
272 if (o==null) return "";
273 if (!(o instanceof Object[])) return o.toString();
274 Object[] arr = (Object[])o;
275 StringBuffer ret = new StringBuffer();
276 for(int i=0; i<arr.length; i++)
278 return ret.toString();
281 public static Object coerce(Object o, Class c) {
282 if (o==null) return null;
283 if (c.isInstance(o)) return o;
284 if (c == char.class) {
285 return o.toString().charAt(0);
288 if (o.getClass().isArray() &&
289 o.getClass().getComponentType().isArray() &&
290 o.getClass().getComponentType().getComponentType() == String.class &&
292 c.getComponentType() == String.class) {
293 String[] ret = new String[((Object[])o).length];
294 for(int i=0; i<ret.length; i++) {
295 StringBuffer sb = new StringBuffer();
296 for(Object ob : (Object[])(((Object[])o)[i]))
298 ret[i] = sb.toString();
303 if (c.isArray() && (c.getComponentType().isInstance(o))) {
304 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
309 if (o.getClass().isArray() && c.isArray()) {
311 for(int i=0; i<((Object[])o).length; i++) {
312 Object ob = (((Object[])o)[i]);
314 System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
319 System.err.println("hit with " + c.getComponentType().getName());
320 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
326 public static abstract class Bindable {
327 public abstract String getSimpleName();
328 public abstract String toString();
329 public abstract <A extends Annotation> A getAnnotation(Class<A> c);
330 public boolean isAnnotationPresent(Class<? extends Annotation> c) { return getAnnotation(c) != null; }
331 public static Bindable create(Object o) {
332 if (o instanceof Class) return new BindableClass((Class)o);
333 if (o instanceof Method) return new BindableMethod((Method)o);
334 if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
339 private static class BindableMethod extends Bindable {
340 private final Method _method;
341 public String toString() { return "BindableMethod["+_method+"]"; }
342 public BindableMethod(Method _method) { this._method = _method; }
343 public String getSimpleName() { return _method.getName(); }
344 public <A extends Annotation> A getAnnotation(Class<A> c) { return _method.getAnnotation(c); }
346 private static class BindableConstructor extends Bindable {
347 private final Constructor _constructor;
348 public String toString() { return "BindableConstructor["+_constructor+"]"; }
349 public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; }
350 public String getSimpleName() { return _constructor.getName(); }
351 public <A extends Annotation> A getAnnotation(Class<A> c) { return _constructor.getAnnotation(c); }
353 private static class BindableClass extends Bindable {
354 private final Class _class;
355 public String toString() { return "BindableClass["+_class+"]"; }
356 public BindableClass(Class _class) { this._class = _class; }
357 public String getSimpleName() { return _class.getSimpleName(); }
358 public <A extends Annotation> A getAnnotation(Class<A> c) { return (A)_class.getAnnotation(c); }