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 indent(String s, int indent) {
55 if (s.indexOf('\n')==-1) return s;
56 StringBuffer ret = new StringBuffer();
57 for(int i=0; i<s.length(); i++) {
61 for(int j=0; j<indent; j++)
64 return ret.toString();
67 public static String show(Object o) {
68 if (o==null) return "null";
69 if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
70 Object[] arr = (Object[])o;
71 StringBuffer ret = new StringBuffer();
72 ret.append(o.getClass().getComponentType().getName());
74 for(int i=0; i<arr.length; i++)
75 ret.append(indent(show(arr[i]), 4) + "\n");
76 return ret.toString();
79 public static Object lub(Object argo) {
80 if (argo instanceof Object[]) return lub((Object[])argo);
83 public static Object[] lub(Object[] argo) {
84 if (argo==null) return null;
86 for(int i=0; i<argo.length; i++) {
87 if (argo[i]==null) continue;
88 argo[i] = lub(argo[i]);
89 c = Reflection.lub(c, argo[i].getClass());
91 if (c==null) c = Object.class;
92 Object[] ret = Reflection.newArray(c, argo.length);
93 System.arraycopy(argo, 0, ret, 0, argo.length);
97 public static Class lub(Class a, Class b) {
98 if (a==null) return b;
99 if (b==null) return a;
101 if (a.isAssignableFrom(b)) return a;
102 if (b.isAssignableFrom(a)) return b;
103 if (a.isArray() && b.isArray())
104 return arrayClass(lub(a.getComponentType(), b.getComponentType()));
105 return lub(b, a.getSuperclass());
108 public static Class arrayClass(Class c) {
109 return Reflection.newArray(c, 0).getClass();
112 /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
113 public static Class forNameOrNull(String s) {
115 return Class.forName(s);
116 } catch (ClassNotFoundException _) {
121 public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
122 /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
123 public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
125 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
127 Object[] args2 = new Object[args.length];
128 System.arraycopy(args, 0, args2, 0, args.length);
130 for(int i=0; i<args.length; i++) {
131 Class goal = argTypes[i];
132 Class actual = args[i] == null ? null : args[i].getClass();
133 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
135 args[i] = rebuild(args[i], goal);
137 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
142 System.out.println("\ninvoking " + o + "." + m);
143 for(int i=0; i<args.length; i++) {
144 if (args[i] instanceof Object[]) {
145 System.out.print(" arg => " + zoo(args[i]));
147 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
151 // FIXME: deal with the case where it is an inner class ctor
152 return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
153 } catch (Exception e) {
154 throw new RuntimeException(e);
158 private static String zoo(Object o) {
159 if (o==null) return "null";
160 if (o instanceof Object[]) {
162 for(int j=0; j<((Object[])o).length; j++)
163 ret += zoo(((Object[])o)[j]) + " ";
169 public static boolean isConcrete(Class c) {
170 if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false;
171 if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
174 public static boolean isStatic(Field f) {
175 if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
179 public static Field getField(Class c, String s) {
181 for(Field f : c.getDeclaredFields())
182 if (f.getName().equals(s))
184 } catch (Exception e) { }
185 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
186 return getField(c.getSuperclass(), s);
188 public static Field getField(Class c, int i) {
190 for(Field f : c.getDeclaredFields()) {
191 if (isStatic(f)) continue;
194 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
195 return getField(c.getSuperclass(), i);
196 } catch (Exception e) { }