1 | /* |
---|
2 | Copyright 2006 by Sean Luke |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | package ec.util; |
---|
9 | import java.lang.reflect.*; |
---|
10 | import javax.swing.tree.*; |
---|
11 | import javax.swing.event.*; |
---|
12 | |
---|
13 | public class ReflectedObject implements TreeModel |
---|
14 | { |
---|
15 | Class type; // the class or TYPE of the object |
---|
16 | Object obj; // the object. Primitive objects are cast into their wrapper types |
---|
17 | String name; // the name of the instance variable holding the object. Top-level objects have a name of "->" |
---|
18 | Object uniq; // a unique identifier for this particular instance variable (used to make getIndexOfChild() possible, ick) |
---|
19 | static ReflectedObject unknown = new ReflectedObject(null) |
---|
20 | { |
---|
21 | public String toString() { return "<unknown>"; } |
---|
22 | }; |
---|
23 | |
---|
24 | public ReflectedObject(Object o) |
---|
25 | { |
---|
26 | this(o,(o==null ? Object.class : o.getClass()),null,null); |
---|
27 | } |
---|
28 | |
---|
29 | protected ReflectedObject(Object o, Class t, String n, Object u) |
---|
30 | { |
---|
31 | obj = o; |
---|
32 | type = t; |
---|
33 | name = n; |
---|
34 | uniq = u; |
---|
35 | } |
---|
36 | |
---|
37 | public String toString() |
---|
38 | { |
---|
39 | String field = (name == null ? "" : name + ": "); |
---|
40 | try { |
---|
41 | if (obj == null) return field + "null"; |
---|
42 | else if (type.isArray()) return field + type.getName() + ", length=" + Array.getLength(obj); |
---|
43 | return field+type.getName()+" "+obj.toString(); |
---|
44 | } catch (Exception e) { |
---|
45 | e.printStackTrace(); |
---|
46 | return field + type.getName() + " <error>"; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public int getNumChildren() |
---|
51 | { |
---|
52 | return getNumFields() + getNumProperties(); |
---|
53 | } |
---|
54 | |
---|
55 | public ReflectedObject getChild(int index) |
---|
56 | { |
---|
57 | int f = getNumFields(); |
---|
58 | if (index < f) return getField(index); |
---|
59 | else return getProperty(index - f); |
---|
60 | } |
---|
61 | |
---|
62 | public ReflectedObject[] getChildren() |
---|
63 | { |
---|
64 | ReflectedObject[] fields = getFields(); |
---|
65 | ReflectedObject[] props = getProperties(); |
---|
66 | ReflectedObject o[] = new ReflectedObject[fields.length + props.length]; |
---|
67 | System.arraycopy(fields,0,o,0,fields.length); |
---|
68 | System.arraycopy(props,0,o,fields.length,props.length); |
---|
69 | return o; |
---|
70 | } |
---|
71 | |
---|
72 | public int getNumFields() |
---|
73 | { |
---|
74 | try |
---|
75 | { |
---|
76 | if (obj == null || type.isPrimitive()) return 0; |
---|
77 | else if (type.isArray()) |
---|
78 | { |
---|
79 | return Array.getLength(obj); |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | return type.getFields().length; |
---|
84 | } |
---|
85 | } |
---|
86 | catch (IllegalArgumentException e) { e.printStackTrace(); throw new RuntimeException("Unexpected Exception: " + e); } |
---|
87 | } |
---|
88 | |
---|
89 | public ReflectedObject getField(int index) |
---|
90 | { |
---|
91 | try |
---|
92 | { |
---|
93 | if (obj == null || type.isPrimitive() || index < 0) return null; |
---|
94 | else if (type.isArray()) |
---|
95 | { |
---|
96 | int len = Array.getLength(obj); |
---|
97 | if (index > len) return null; |
---|
98 | return new ReflectedObject(Array.get(obj,index), type.getComponentType(),""+index, ""+index); |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | Field[] f = type.getFields(); |
---|
103 | int len = f.length; |
---|
104 | if (index > len) return null; |
---|
105 | return new ReflectedObject(f[index].get(obj), |
---|
106 | (f[index].get(obj) == null || f[index].getType().isPrimitive() ? |
---|
107 | f[index].getType() : f[index].get(obj).getClass()), |
---|
108 | f[index].getName(), f[index]); |
---|
109 | } |
---|
110 | } |
---|
111 | catch (IllegalArgumentException e) { e.printStackTrace(); throw new RuntimeException("Unexpected Exception: " + e); } |
---|
112 | catch (IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException("Unexpected Exception: " + e); } |
---|
113 | } |
---|
114 | |
---|
115 | public ReflectedObject[] getFields() |
---|
116 | { |
---|
117 | try |
---|
118 | { |
---|
119 | if (obj == null || type.isPrimitive()) return new ReflectedObject[0]; |
---|
120 | else if (type.isArray()) |
---|
121 | { |
---|
122 | int len = Array.getLength(obj); |
---|
123 | ReflectedObject[] ref = new ReflectedObject[len]; |
---|
124 | for(int x = 0; x < len; x++) |
---|
125 | ref[x] = new ReflectedObject(Array.get(obj,x), type.getComponentType(),""+x,""+x); |
---|
126 | return ref; |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | Field[] f = type.getFields(); |
---|
131 | int len = f.length; |
---|
132 | ReflectedObject[] ref = new ReflectedObject[len]; |
---|
133 | for(int x=0;x<len; x++) |
---|
134 | ref[x] = new ReflectedObject(f[x].get(obj), |
---|
135 | (f[x].get(obj) == null || f[x].getType().isPrimitive() ? f[x].getType() : f[x].get(obj).getClass()), |
---|
136 | f[x].getName(), f[x]); |
---|
137 | return ref; |
---|
138 | } |
---|
139 | } |
---|
140 | catch (IllegalArgumentException e) { e.printStackTrace(); throw new RuntimeException("Unexpected Exception: " + e); } |
---|
141 | catch (IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException("Unexpected Exception: " + e); } |
---|
142 | } |
---|
143 | |
---|
144 | public boolean equals(Object obj) |
---|
145 | { |
---|
146 | if (obj == null) return false; |
---|
147 | if (!(obj instanceof ReflectedObject)) return false; |
---|
148 | if (uniq == null && ((ReflectedObject)obj).uniq == null) return true; |
---|
149 | if (uniq == null || ((ReflectedObject)obj).uniq == null) return false; |
---|
150 | return ((ReflectedObject)obj).uniq.equals(uniq); |
---|
151 | } |
---|
152 | |
---|
153 | // tree model stuff |
---|
154 | public Object getRoot() { return this; } |
---|
155 | public Object getChild(Object parent, int index) |
---|
156 | { |
---|
157 | return ((ReflectedObject)parent).getChild(index); |
---|
158 | } |
---|
159 | public int getChildCount(Object parent) |
---|
160 | { |
---|
161 | return ((ReflectedObject)parent).getNumChildren(); |
---|
162 | } |
---|
163 | // This could get grotesquely expensive! |
---|
164 | public int getIndexOfChild(Object parent, Object child) |
---|
165 | { |
---|
166 | ReflectedObject[] children = ((ReflectedObject)parent).getChildren(); |
---|
167 | for(int x=0;x<children.length;x++) |
---|
168 | if (children[x].equals(child)) return x; |
---|
169 | throw new IndexOutOfBoundsException("No such child " + child + " in parent " + ((ReflectedObject)parent).toString()); |
---|
170 | } |
---|
171 | public boolean isLeaf(Object parent) |
---|
172 | { |
---|
173 | return getChildCount(parent) == 0; |
---|
174 | } |
---|
175 | public void valueForPathChanged(TreePath path, Object newValue) |
---|
176 | { |
---|
177 | // do nothing |
---|
178 | } |
---|
179 | public void addTreeModelListener(TreeModelListener l) |
---|
180 | { |
---|
181 | // do nothing |
---|
182 | } |
---|
183 | public void removeTreeModelListener(TreeModelListener l) |
---|
184 | { |
---|
185 | // do nothing |
---|
186 | } |
---|
187 | |
---|
188 | // Java Bean Properties |
---|
189 | int getNumProperties() |
---|
190 | { |
---|
191 | if (obj==null) return 0; |
---|
192 | int count = 0; |
---|
193 | |
---|
194 | // generate the properties |
---|
195 | try |
---|
196 | { |
---|
197 | Class c = obj.getClass(); |
---|
198 | Method[] m = (c.getMethods()); |
---|
199 | for(int x = 0 ; x < m.length; x++) |
---|
200 | { |
---|
201 | if (m[x].getName().startsWith("get") || m[x].getName().startsWith("is")) // corrrect syntax? |
---|
202 | { |
---|
203 | int modifier = m[x].getModifiers(); |
---|
204 | if (m[x].getParameterTypes().length == 0 && |
---|
205 | Modifier.isPublic(modifier) && |
---|
206 | m[x].getReturnType() != Void.TYPE) // no arguments, and public, non-void, non-abstract? |
---|
207 | { |
---|
208 | count++; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | catch (Exception e) |
---|
214 | { |
---|
215 | count = 0; |
---|
216 | e.printStackTrace(); |
---|
217 | } |
---|
218 | return count; |
---|
219 | } |
---|
220 | |
---|
221 | ReflectedObject getProperty(int index) |
---|
222 | { |
---|
223 | if (obj==null) return null; |
---|
224 | int count = 0; |
---|
225 | |
---|
226 | // generate the properties |
---|
227 | try |
---|
228 | { |
---|
229 | Class c = obj.getClass(); |
---|
230 | Method[] m = (c.getMethods()); |
---|
231 | for(int x = 0 ; x < m.length; x++) |
---|
232 | { |
---|
233 | if (m[x].getName().startsWith("get") || m[x].getName().startsWith("is")) // corrrect syntax? |
---|
234 | { |
---|
235 | int modifier = m[x].getModifiers(); |
---|
236 | if (m[x].getParameterTypes().length == 0 && |
---|
237 | Modifier.isPublic(modifier) && |
---|
238 | m[x].getReturnType() != Void.TYPE) // no arguments, and public, non-void, non-abstract? |
---|
239 | { |
---|
240 | if (count==index) |
---|
241 | { |
---|
242 | Object o = null; |
---|
243 | try |
---|
244 | { |
---|
245 | o = m[x].invoke(obj, new Object[0]); |
---|
246 | } |
---|
247 | catch (InvocationTargetException e) |
---|
248 | { |
---|
249 | return unknown; |
---|
250 | } |
---|
251 | return new ReflectedObject(o, o == null || m[x].getReturnType().isPrimitive() ? |
---|
252 | m[x].getReturnType() : o.getClass(), |
---|
253 | "Property " + m[x].getName(), "Property " + m[x].getName()); |
---|
254 | } |
---|
255 | count++; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | catch (Exception e) |
---|
261 | { |
---|
262 | e.printStackTrace(); |
---|
263 | } |
---|
264 | return unknown; |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | ReflectedObject[] getProperties() |
---|
269 | { |
---|
270 | if (obj==null) return new ReflectedObject[0]; |
---|
271 | int len = getNumProperties(); |
---|
272 | int count = 0; |
---|
273 | |
---|
274 | ReflectedObject[] refs = new ReflectedObject[len]; |
---|
275 | |
---|
276 | // generate the properties |
---|
277 | try |
---|
278 | { |
---|
279 | Class c = obj.getClass(); |
---|
280 | Method[] m = (c.getMethods()); |
---|
281 | for(int x = 0 ; x < m.length; x++) |
---|
282 | { |
---|
283 | if (m[x].getName().startsWith("get") || m[x].getName().startsWith("is")) // corrrect syntax? |
---|
284 | { |
---|
285 | int modifier = m[x].getModifiers(); |
---|
286 | if (m[x].getParameterTypes().length == 0 && |
---|
287 | Modifier.isPublic(modifier) && |
---|
288 | m[x].getReturnType() != Void.TYPE) // no arguments, and public, non-void, non-abstract? |
---|
289 | { |
---|
290 | Object o = null; |
---|
291 | try |
---|
292 | { |
---|
293 | o = m[x].invoke(obj, new Object[0]); |
---|
294 | refs[count] = new ReflectedObject(o, o == null || m[x].getReturnType().isPrimitive() ? |
---|
295 | m[x].getReturnType() : o.getClass(), |
---|
296 | "Property " + m[x].getName(), "Property " + m[x].getName()); |
---|
297 | } |
---|
298 | catch (InvocationTargetException e) |
---|
299 | { |
---|
300 | refs[count] = unknown; |
---|
301 | } |
---|
302 | count++; |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | catch (Exception e) |
---|
308 | { |
---|
309 | e.printStackTrace(); |
---|
310 | } |
---|
311 | return refs; |
---|
312 | } |
---|
313 | |
---|
314 | } |
---|