1 package edu.berkeley.sbp.util;
3 import java.lang.reflect.*;
5 /** Random reflection-related utilities */
6 public final class Reflection {
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;
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());
24 if (c == String.class) {
26 for(int i=0; i<a.length; i++)
27 if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
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())+""
39 } else if (c.isArray()) {
40 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
43 } else if (c==int.class && o instanceof Number) { return o;
45 throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
50 public static Object[] newArray(Class c, int i) {
51 return (Object[])Array.newInstance(c, i);
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();
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() + " = ");
72 ret.append(show(f.get(o)));
73 } catch (Exception e) {
74 ret.append("**"+e+"**");
75 throw new RuntimeException(e);
78 return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
81 public static Object lub(Object argo) {
82 if (argo instanceof Object[]) return lub((Object[])argo);
85 public static Object[] lub(Object[] argo) {
86 if (argo==null) return 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());
93 if (c==null) c = Object.class;
94 Object[] ret = Reflection.newArray(c, argo.length);
95 System.arraycopy(argo, 0, ret, 0, argo.length);
99 public static Class lub(Class a, Class b) {
100 if (a==null) return b;
101 if (b==null) 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());
110 public static Class arrayClass(Class c) {
111 return Reflection.newArray(c, 0).getClass();
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) {
117 return Class.forName(s);
118 } catch (ClassNotFoundException _) {
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) {
127 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
129 Object[] args2 = new Object[args.length];
130 System.arraycopy(args, 0, args2, 0, args.length);
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;
137 args[i] = rebuild(args[i], goal);
139 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
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]));
149 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
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);
160 private static String zoo(Object o) {
161 if (o==null) return "null";
162 if (o instanceof Object[]) {
164 for(int j=0; j<((Object[])o).length; j++)
165 ret += zoo(((Object[])o)[j]) + " ";
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;
176 public static boolean isStatic(Field f) {
177 if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
181 public static Field getField(Class c, String s) {
183 for(Field f : c.getDeclaredFields())
184 if (f.getName().equals(s))
186 } catch (Exception e) { }
187 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
188 return getField(c.getSuperclass(), s);
190 public static Field getField(Class c, int i) {
192 for(Field f : c.getDeclaredFields()) {
193 if (isStatic(f)) continue;
196 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
197 return getField(c.getSuperclass(), i);
198 } catch (Exception e) { }
202 public static interface Show {