1 package com.silvermindsoftware.hitch.reflect;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.List;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class ClassInfo {
28
29
30 public List<Class> classList = new ArrayList<Class>();
31
32
33 Field[] fields;
34
35
36 Method[] getters;
37 Method[] setters;
38 Method[] methods;
39
40 public ClassInfo(Class clazz) {
41 collectInfo(clazz);
42 }
43
44 private void collectInfo(Class clazz) {
45 classList.add(clazz);
46 getSuperClass(clazz);
47 collectFields();
48 collectMethods();
49 }
50
51 private void collectFields() {
52
53 List<FieldInfo> fieldInfoList = new ArrayList<FieldInfo>();
54 List<Field> fieldList = new ArrayList<Field>();
55
56 for (Class clazz : classList) {
57 for (Field field : clazz.getDeclaredFields()) {
58 FieldInfo fieldInfo = new FieldInfo(field);
59 if (!fieldInfoList.contains(fieldInfo)) {
60 fieldInfoList.add(fieldInfo);
61 fieldList.add(fieldInfo.getField());
62 }
63 }
64 }
65
66 fields = fieldList.toArray(new Field[]{});
67
68 }
69
70 private void collectMethods() {
71
72 List<MethodInfo> setterInfoList = new ArrayList<MethodInfo>();
73 List<MethodInfo> getterInfoList = new ArrayList<MethodInfo>();
74 List<MethodInfo> methodsInfoList = new ArrayList<MethodInfo>();
75
76 List<Method> setterList = new ArrayList<Method>();
77 List<Method> getterList = new ArrayList<Method>();
78 List<Method> methodList = new ArrayList<Method>();
79
80 for (Class clazz : classList) {
81 for (Method method : clazz.getDeclaredMethods()) {
82 if (method.getName().startsWith("set")) {
83 MethodInfo methodInfo = new MethodInfo(method);
84 if (!setterInfoList.contains(methodInfo)) {
85 setterInfoList.add(methodInfo);
86 setterList.add(methodInfo.getMethod());
87 }
88 } else
89 if ((method.getName().startsWith("get") || method.getName().startsWith("is"))
90 && !method.getName().equals("getClass")
91 && !method.getName().equals("get")) {
92 MethodInfo methodInfo = new MethodInfo(method);
93 if (!getterInfoList.contains(methodInfo)) {
94 getterInfoList.add(methodInfo);
95 getterList.add(methodInfo.getMethod());
96 }
97 } else {
98 MethodInfo methodInfo = new MethodInfo(method);
99 if (!methodsInfoList.contains(methodInfo)) {
100 methodsInfoList.add(methodInfo);
101 methodList.add(methodInfo.getMethod());
102 }
103 }
104 }
105 }
106
107 getters = getterList.toArray(new Method[]{});
108 setters = setterList.toArray(new Method[]{});
109 methods = methodList.toArray(new Method[]{});
110
111 }
112
113 private void getSuperClass(Class clazz) {
114 Class superClazz = clazz.getSuperclass();
115 if (superClazz != null) {
116 classList.add(superClazz);
117 getSuperClass(superClazz);
118 }
119 }
120
121 private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
122 if (a1 == null) {
123 return a2 == null || a2.length == 0;
124 }
125
126 if (a2 == null) {
127 return a1.length == 0;
128 }
129
130 if (a1.length != a2.length) {
131 return false;
132 }
133
134 for (int i = 0; i < a1.length; i++) {
135 if (a1[i] != a2[i]) {
136 return false;
137 }
138 }
139
140 return true;
141 }
142
143
144
145
146
147
148
149
150
151 public Field getField(String name) throws NoSuchFieldException {
152
153 if (name == null || name.trim().equals("")) {
154 throw new IllegalArgumentException("Name cannot be null or empty");
155 }
156 ;
157
158 Field retField = null;
159
160 for (Field field : fields) {
161 if (field.getName().equals(name)) {
162 retField = field;
163 break;
164 }
165 }
166
167 if (retField == null) {
168 throw new NoSuchFieldException("No method found for method named " + name);
169 }
170
171 return retField;
172 }
173
174
175
176
177
178
179
180
181 public Method getSetterMethod(String name, Class[] parameterTypes) throws NoSuchMethodException {
182 if (name == null || name.trim().equals(""))
183 throw new IllegalArgumentException("Name cannot be null or empty");
184
185 Method retSetter = null;
186
187 for (Method setter : setters) {
188 if (setter.getName().equals(name) &&
189 arrayContentsEq(parameterTypes, setter.getParameterTypes())) {
190 retSetter = setter;
191 break;
192 }
193 }
194
195 if (retSetter == null)
196 throw new NoSuchMethodException("No method found for method named " + name);
197
198 return retSetter;
199 }
200
201
202
203
204
205
206
207
208 public Method getGetterMethod(String name) throws NoSuchMethodException {
209 if (name == null || name.trim().equals(""))
210 throw new IllegalArgumentException("Name cannot be null or empty");
211
212 Method retGetter = null;
213
214 for (Method getter : getters) {
215 if (getter.getName().equals(name) && getter.getParameterTypes().length == 0) {
216 retGetter = getter;
217 break;
218 }
219 }
220
221 if (retGetter == null)
222 throw new NoSuchMethodException("No method found for method named " + name);
223
224 return retGetter;
225 }
226
227
228
229
230
231
232
233
234
235 public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException {
236 if (name == null || name.trim().equals(""))
237 throw new IllegalArgumentException("Name cannot be null or empty");
238
239 Method retMethod = null;
240
241 for (Method method : methods) {
242 if (method.getName().equals(name) &&
243 arrayContentsEq(parameterTypes, method.getParameterTypes())) {
244 retMethod = method;
245 break;
246 }
247 }
248
249 if (retMethod == null)
250 throw new NoSuchMethodException("No method found for method named " + name);
251
252 return retMethod;
253 }
254
255
256
257
258
259
260
261
262 public Field[] getFields() {
263 return fields;
264 }
265
266
267
268
269
270
271
272
273
274 public Method[] getSetters() {
275 return setters;
276 }
277
278
279
280
281
282
283
284
285
286 public Method[] getGetters() {
287 return getters;
288 }
289
290
291
292
293
294
295
296
297
298 public Method[] getMethods() {
299 return getters;
300 }
301 }