View Javadoc

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    * Copyright 2007 Brandon Goodin
10   * <p/>
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   * <p/>
15   * http://www.apache.org/licenses/LICENSE-2.0
16   * <p/>
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   */
23  
24  /**
25   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/com/silvermindsoftware/hitch/reflect/ClassInfo.java $
26   */
27  public class ClassInfo {
28  
29  	/* Class Hierarchy */
30  	public List<Class> classList = new ArrayList<Class>();
31  
32  	/* Fields */
33  	Field[] fields;
34  
35  	/* Methods */
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 	 * Retrieves a single field (public, private, protected or package) from the provided class
146 	 * or one of its superclasses. The lowest subclass field is collected when there is a duplicate.
147 	 * A duplicate is defined as having the same field name and type.
148 	 *
149 	 * @return
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 	 * Retrieves a single getter method (public, private, protected or package) from the provided class
176 	 * or one of its superclasses. The lowest subclass method is collected when there is a duplicate.
177 	 * A duplicate is defined as having the same method name and parameter types.
178 	 *
179 	 * @return
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 	 * Retrieves a single setter method (public, private, protected or package) from the provided class
203 	 * or one of its superclasses. The lowest subclass method is collected when there is a duplicate.
204 	 * A duplicate is defined as having the same method name and parameter types.
205 	 *
206 	 * @return
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 	 * Retrieves a single method that is not a setter or getter (public, private, protected or package)
229 	 * from the provided class or one of its superclasses. Only the lowest subclass method is collected
230 	 * when there is a duplicate. A duplicate is defined as having the same method name and parameter
231 	 * types.
232 	 *
233 	 * @return
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 	 * Array of all fields (public, private, protected or package) for the provided class
257 	 * and all of its superclasses. The lowest subclass field is collected when there is
258 	 * a duplicate. A duplicate is defined as having the same field name and type .
259 	 *
260 	 * @return
261 	 */
262 	public Field[] getFields() {
263 		return fields;
264 	}
265 
266 	/**
267 	 * Array of all setter methods (public, private, protected or package) for the provided
268 	 * class and all of its superclasses. The lowest subclass method is collected when there
269 	 * is a duplicate. A duplicate is defined as having the same method name and parameter
270 	 * types.
271 	 *
272 	 * @return
273 	 */
274 	public Method[] getSetters() {
275 		return setters;
276 	}
277 
278 	/**
279 	 * Array of all getter methods (public, private, protected or package) for the provided
280 	 * class and all of its superclasses. The lowest subclass method is collected when there
281 	 * is a duplicate. A duplicate is defined as having the same method name and parameter
282 	 * types.
283 	 *
284 	 * @return
285 	 */
286 	public Method[] getGetters() {
287 		return getters;
288 	}
289 
290 	/**
291 	 * Array of all methods that are not setters or getters (public, private, protected or package)
292 	 * for the provided class and all of its superclasses. The lowest subclass method is collected
293 	 * when there is a duplicate. A duplicate is defined as having the same method name and parameter
294 	 * types.
295 	 *
296 	 * @return
297 	 */
298 	public Method[] getMethods() {
299 		return getters;
300 	}
301 }