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 Object lub(Object argo) {
55 if (argo instanceof Object[]) return lub((Object[])argo);
58 public static Object[] lub(Object[] argo) {
59 if (argo==null) return null;
61 for(int i=0; i<argo.length; i++) {
62 if (argo[i]==null) continue;
63 argo[i] = lub(argo[i]);
64 c = Reflection.lub(c, argo[i].getClass());
66 if (c==null) c = Object.class;
67 Object[] ret = Reflection.newArray(c, argo.length);
68 System.arraycopy(argo, 0, ret, 0, argo.length);
72 public static Class lub(Class a, Class b) {
73 if (a==null) return b;
74 if (b==null) return a;
76 if (a.isAssignableFrom(b)) return a;
77 if (b.isAssignableFrom(a)) return b;
78 if (a.isArray() && b.isArray())
79 return arrayClass(lub(a.getComponentType(), b.getComponentType()));
80 return lub(b, a.getSuperclass());
83 public static Class arrayClass(Class c) {
84 return Reflection.newArray(c, 0).getClass();
87 /** a version of <tt>Class.forName</tt> that returns <tt>null</tt> instead of throwing an exception */
88 public static Class forNameOrNull(String s) {
90 return Class.forName(s);
91 } catch (ClassNotFoundException _) {
96 public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); }
97 /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */
98 public static Object fuzzyInvoke(Object o, Member m, Object[] args) {
100 Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
102 Object[] args2 = new Object[args.length];
103 System.arraycopy(args, 0, args2, 0, args.length);
105 for(int i=0; i<args.length; i++) {
106 Class goal = argTypes[i];
107 Class actual = args[i] == null ? null : args[i].getClass();
108 if (args[i] == null || goal.isAssignableFrom(actual)) continue;
110 args[i] = rebuild(args[i], goal);
112 throw new Error(e.getMessage() + "\n while trying to fuzzyInvoke("+m.getName()+")");
117 System.out.println("\ninvoking " + o + "." + m);
118 for(int i=0; i<args.length; i++) {
119 if (args[i] instanceof Object[]) {
120 System.out.print(" arg => " + zoo(args[i]));
122 System.out.println(" arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
126 // FIXME: deal with the case where it is an inner class ctor
127 return m instanceof Method ? ((Method)m).invoke(o, args) : ((Constructor)m).newInstance(args);
128 } catch (Exception e) {
129 throw new RuntimeException(e);
133 private static String zoo(Object o) {
134 if (o==null) return "null";
135 if (o instanceof Object[]) {
137 for(int j=0; j<((Object[])o).length; j++)
138 ret += zoo(((Object[])o)[j]) + " ";
144 public static boolean isConcrete(Class c) {
145 if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false;
146 if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false;
149 public static boolean isStatic(Field f) {
150 if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
154 public static Field getField(Class c, String s) {
156 for(Field f : c.getDeclaredFields())
157 if (f.getName().equals(s))
159 } catch (Exception e) { }
160 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
161 return getField(c.getSuperclass(), s);
163 public static Field getField(Class c, int i) {
165 for(Field f : c.getDeclaredFields()) {
166 if (isStatic(f)) continue;
169 if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
170 return getField(c.getSuperclass(), i);
171 } catch (Exception e) { }