more improvements to TibDoc
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
1 package edu.berkeley.sbp.util;
2 import java.io.*;
3 import java.lang.reflect.*;
4
5 /** Random reflection-related utilities */
6 public final class Reflection {
7     
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;
15             if (c.isArray()) {
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());
20                     ret[i] = o2;
21                 }
22                 return ret;
23             }
24             if (c == String.class) {
25                 boolean ok = true;
26                 for(int i=0; i<a.length; i++)
27                     if (a[i]==null || (!(a[i] instanceof Character) && !(a[i] instanceof String)))
28                         ok = false;
29                 if (ok) {
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())+""
34                                  : (String)a[i]
35                                  );
36                     return s.toString();
37                 }
38             }
39         } else if (c.isArray()) {
40             Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
41             ret[0] = o;
42             return ret;
43         } else if (c==int.class && o instanceof Number) { return o;
44         } else {
45             throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName());
46         }
47         return o;
48     }
49
50     public static Object[] newArray(Class c, int i) {
51         return (Object[])Array.newInstance(c, i);
52     }
53
54     public static Object lub(Object argo) {
55         if (argo instanceof Object[]) return lub((Object[])argo);
56         return argo;
57     }
58     public static Object[] lub(Object[] argo) {
59         if (argo==null) return null;
60         Class c = 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());
65         }
66         if (c==null) c = Object.class;
67         Object[] ret = Reflection.newArray(c, argo.length);
68         System.arraycopy(argo, 0, ret, 0, argo.length);
69         return ret;
70     }
71
72     public static Class lub(Class a, Class b) {
73         if (a==null) return b;
74         if (b==null) return a;
75         if (a==b) 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());
81     }
82
83     public static Class arrayClass(Class c) {
84         return Reflection.newArray(c, 0).getClass();
85     }
86
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) {
89         try {
90             return Class.forName(s); 
91         } catch (ClassNotFoundException _) {
92             return null;
93         }
94     }
95
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) {
99         try {
100             Class[] argTypes = m instanceof Method ? ((Method)m).getParameterTypes() : ((Constructor)m).getParameterTypes();
101             boolean ok = true;
102             Object[] args2 = new Object[args.length];
103             System.arraycopy(args, 0, args2, 0, args.length);
104             args = args2;
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;
109                 try {
110                     args[i] = rebuild(args[i], goal);
111                 } catch (Error e) {
112                     throw new Error(e.getMessage() + "\n   while trying to fuzzyInvoke("+m.getName()+")");
113                 }
114             }
115             // for debugging
116             /*
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]));
121                 } else {
122                     System.out.println("  arg => " + args[i] + (args[i]==null?"":(" (a " + args[i].getClass().getName() + ")")));
123                 }
124             }
125             */
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);
130         }
131     }
132
133     private static String zoo(Object o) {
134         if (o==null) return "null";
135         if (o instanceof Object[]) {
136             String ret = "[ ";
137             for(int j=0; j<((Object[])o).length; j++)
138                 ret += zoo(((Object[])o)[j]) + " ";
139             return ret+"]";
140         }
141         return o+"";
142     }
143
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;
147         return true;
148     }
149     public static boolean isStatic(Field f) {
150         if ((f.getModifiers() & Modifier.STATIC) != 0) return true;
151         return false;
152     }
153
154     public static Field getField(Class c, String s) {
155         try {
156             for(Field f : c.getDeclaredFields())
157                 if (f.getName().equals(s))
158                     return f;
159         } catch (Exception e) { }
160         if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
161         return getField(c.getSuperclass(), s);
162     }
163     public static Field getField(Class c, int i) {
164         try {
165             for(Field f : c.getDeclaredFields()) {
166                 if (isStatic(f)) continue;
167                 return f;
168             }
169             if (c.getSuperclass()==null || c.getSuperclass()==c) return null;
170             return getField(c.getSuperclass(), i);
171         } catch (Exception e) { }
172         return null;
173     }
174
175 }