01: package com.jofti.introspect;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import com.jofti.model.ComparableBoolean;
07:
08: /**
09: * A set of util methods for dealing with Class type issues.
10: * @author steve woodcock (steve@jofti.com)
11: *
12: */
13: public class ClassUtils {
14:
15: /**
16: * Iterates through a Class hierachy - starting at the Class passed in and retrieves
17: * all interfaces that are declared as part of the hierachy.
18: * @param clazz
19: * @return an Array of Interface Class objects
20: */
21: public Class[] getInterfaces(Class clazz) {
22: List temp = new ArrayList();
23: temp = getAllInterfaces(temp, clazz);
24: return (Class[]) temp.toArray(new Class[temp.size()]);
25: }
26:
27: /**
28: * Iterates through a Class hierachy - starting at the Class passed in and retrieves
29: * all Classes that are declared as part of the hierachy.
30: * @param clazz
31: * @return an Array of Class objects
32: */
33: public Class[] getClasses(Class clazz) {
34: List temp = new ArrayList();
35: temp.add(clazz);
36: Class super Clazz = clazz.getSuperclass();
37: while (super Clazz != null) {
38: temp.add(super Clazz);
39: super Clazz = super Clazz.getSuperclass();
40: }
41: return (Class[]) temp.toArray(new Class[temp.size()]);
42: }
43:
44: private List getAllInterfaces(List list, Class clazz) {
45: Class[] classes = clazz.getInterfaces();
46: for (int i = 0; i < classes.length; i++) {
47: list.add(classes[i]);
48: if (classes[i].getInterfaces() != null) {
49: list = getAllInterfaces(list, classes[i]);
50: }
51: }
52: return list;
53: }
54:
55: /**
56: * Wraps a Boolean object in a ComparableBoolean so it can be used in an Index.
57: *
58: * @param tempObj
59: * @return Either a ComparableBoolean wrapping a Boolean or the original Object if not Boolean.
60: */
61: public Object wrapObject(Object tempObj) {
62: if (tempObj instanceof Boolean) {
63: tempObj = new ComparableBoolean((Boolean) tempObj);
64: }
65: return tempObj;
66: }
67:
68: }
|