/Users/lyon/j4p/src/classUtils/delegate/ReflectUtil.java
|
1 package classUtils.delegate;
2
3 import javax.swing.JOptionPane;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method;
8 import java.lang.reflect.Modifier;
9 import java.util.Vector;
10
11 /**
12 * ReflectUtil class This class is provided by
13 * instructor to get class information
14 */
15 public class ReflectUtil {
16 protected Class c;
17 private Object o;
18
19 public ReflectUtil(Object _o) {
20 o = _o;
21 c = o.getClass();
22 }
23
24 public Constructor[] getConstructors() {
25 return
26 c.getDeclaredConstructors();
27 }
28
29 public Field[] getFields() {
30 return c.getDeclaredFields();
31 }
32
33 public Method[] getMethods() {
34 return c.getDeclaredMethods();
35 }
36
37 public boolean isContainedBy(Method ma[], Method m) {
38 for (int i = 0; i < ma.length; i++) {
39 final String name = m.getName();
40 if (name.equals("clone")) return true;
41 if (ma[i].getName().equals(name)) return true;
42 }
43 return false;
44 }
45
46 public Method[] getAllMethodsNotInObjectClass(Method m[]) {
47 Class oc = Object.class;
48 Method oms[] = oc.getMethods();
49 Vector vOut = new Vector();
50 for (int i = 0; i < m.length; i++) {
51 if (isContainedBy(oms, m[i])) continue;
52 if (m[i].getName().equals("clone")){
53 System.out.println("clone is being added!!:");
54 }
55 vOut.addElement(m[i]);
56 System.out.println(m[i].getName());
57 }
58 Method oma[] = new Method[vOut.size()];
59 vOut.copyInto(oma);
60 return oma;
61 }
62
63 /**
64 * @return an array with all the methods (including the object classes);
65 */
66 public Method[] getAllMethods() {
67 Vector v = new Vector();
68 Method ma[] = c.getMethods();
69 for (int i = 0; i < ma.length; i++) {
70 if (ma[i].getDeclaringClass() !=
71 Object.class)
72 v.addElement(ma[i]);
73 }
74 ma = new Method[v.size()];
75 for (int i = 0; i < v.size(); i++)
76 ma[i] = ((Method) v.elementAt(i));
77 return getAllMethodsNotInObjectClass(ma);
78 }
79
80 /**
81 * getClasses is not implemented in the
82 * cutils.reflection API, yet.
83 */
84 public Class[] getClasses() {
85 return c.getClasses();
86 }
87
88 public String[] getReadMethodNames() {
89 Method m[] = getMethods();
90 Vector v = new Vector();
91 for (int i = 0; i < m.length; i++) {
92 String s = getName(m[i]);
93 if (s.startsWith("get") ||
94 s.startsWith("is")) {
95 v.addElement(s);
96 System.out.println(s);
97 }
98 }
99 String getterArray[] = new String[v.size()];
100 for (int i = 0;
101 i < getterArray.length;
102 i++)
103 getterArray[i] =
104 (String) v.elementAt(i);
105 return getterArray;
106 }
107
108 public String[] getReadMethodNames(int n) {
109 Method m[] = getMethodsWithNArgs(n);
110 Vector v = new Vector();
111 for (int i = 0; i < m.length; i++) {
112 String s = getName(m[i]);
113 if (s.startsWith("get") ||
114 s.startsWith("is"))
115 v.addElement(s);
116 }
117 String getterArray[] = new String[v.size()];
118 for (int i = 0;
119 i < getterArray.length;
120 i++)
121 getterArray[i] =
122 (String) v.elementAt(i);
123 return getterArray;
124 }
125
126 public String[] getWriteMethodNames() {
127 Method m[] = getMethods();
128 Vector v = new Vector();
129 for (int i = 0; i < m.length; i++) {
130 String s = getName(m[i]);
131 if (s.startsWith("set")) {
132 v.addElement(s);
133 System.out.println(s);
134 }
135 }
136 String setterArray[] = new String[v.size()];
137 for (int i = 0;
138 i < setterArray.length;
139 i++)
140 setterArray[i] =
141 (String) v.elementAt(i);
142 return setterArray;
143 }
144
145 /**
146 * getMethod takes a string and returns a
147 * corresponding method.
148 */
149 public Method getMethod(String s) {
150 Method m = null;
151 try {
152 m = c.getMethod(s, new Class[]{});
153 } catch (NoSuchMethodException e) {
154 System.out.println(e);
155 }
156 return m;
157 }
158
159 /**
160 * invoke a methodName string with no
161 * arguments.
162 */
163 public Object invoke(String methodName) {
164 Method m = getMethod(methodName);
165 Object ret = null;
166 try {
167 ret = m.invoke(o, null);
168 } catch (InvocationTargetException e) {
169 e.printStackTrace();
170 } catch (IllegalAccessException e) {
171 e.printStackTrace();
172 }
173 return ret;
174 }
175
176 public int getModifiers(Method m) {
177 return m.getModifiers();
178 }
179
180 public String getModifierString(Method m) {
181 return Modifier.toString(getModifiers(m));
182 }
183
184 /**
185 * Print an array of objects
186 */
187 public static void println(Object o[]) {
188 for (int i = 0; i < o.length; i++)
189 System.out.println(o[i]);
190 }
191
192 /**
193 * convert an array of string into a big
194 * string with new lines
195 */
196 public static String toString(Object o[]) {
197 String s = "";
198 for (int i = 0; i < o.length; i++)
199 s = s + o[i] + "\n";
200 return s;
201 }
202
203 public void printInfo() {
204 System.out.println("Info on class " +
205 getClassName());
206 System.out.println("Constructors:");
207 println(getConstructors());
208 System.out.println("Fields:");
209 println(getFields());
210 System.out.println("Methods:");
211 println(getMethods());
212 System.out.println("Methods with 0 arguments");
213 println(getMethodsWithNArgs(0));
214 System.out.println("read methods");
215 println(getReadMethodNames());
216 System.out.println("write methods");
217 println(getWriteMethodNames());
218 System.out.println("Classes");
219 println(getClasses());
220 }
221
222 public String getClassName() {
223 return c.getName();
224 }
225
226 Method[] getMethodsWithNArgs(int n) {
227 Method m[] = getMethods();
228 Vector v = new Vector();
229 for (int i = 0; i < m.length; i++) {
230 Class ca[] = m[i].getParameterTypes();
231 if (ca.length == n)
232 v.addElement(m[i]);
233 }
234 Method ma[] = new Method[v.size()];
235 for (int i = 0; i < v.size(); i++)
236 ma[i] = (Method) v.elementAt(i);
237 return ma;
238 }
239
240 public String getName(Method m) {
241 return m.getName();
242 }
243
244 public String getInfoString(Method m) {
245 return
246 "for method " + m.getName() +
247 "\nThe modifier = " +
248 getModifierString(m) +
249 "\nThe return type =" +
250 m.getReturnType().getName() +
251 "\n The arguments for this method are " +
252 toString(m.getParameterTypes());
253 }
254
255 public void printInfo(Method m) {
256 System.out.println(getInfoString(m));
257 }
258
259 public static void main(String args[]) {
260 ReflectUtil ru = new ReflectUtil(new java.util.Date());
261 ru.startCommandLineInterpreter();
262 //ru.printInfo();
263 }
264
265 public void startCommandLineInterpreter() {
266 String s = null;
267 String prompt = "enter command:";
268 while (!(s = getString(prompt)).startsWith("quit"))
269 try {
270 prompt = s + "=" + invoke(s);
271 } catch (Exception e) {
272 prompt = e.toString();
273 }
274 ;
275 }
276
277 public static String getString(String prompt) {
278 return JOptionPane.showInputDialog(prompt);
279 }
280 }
281
282
283