/Users/lyon/j4p/src/classUtils/reflection/TypeUtil.java

1    package classUtils.reflection; 
2     
3    import java.lang.reflect.Field; 
4    import java.lang.reflect.Method; 
5    import java.util.Vector; 
6     
7    public class TypeUtil { 
8        private ReflectUtil ru; 
9        private Vector allClasses = new Vector(); 
10    
11       public void add(Class c[]) { 
12           for (int i = 0; i < c.length; i++) 
13               if (allClasses.contains(c[i])) 
14                   continue; 
15               else 
16                   allClasses.addElement(c[i]); 
17       } 
18    
19       public TypeUtil(Object instance) { 
20           ru = new ReflectUtil(instance); 
21       } 
22    
23    
24       public void initAllFriends() { 
25           Class[] classes = addClassesAndInterfaces(); 
26           addFieldRefs(classes); 
27           Method[] methods = addReturnTypes(classes); 
28           addParameterTypes(methods); 
29    
30       } 
31    
32       public Class[] addClassesAndInterfaces() { 
33           Class[] classes = initSuperClasses(); 
34           addInterfaces(classes); 
35           return classes; 
36       } 
37    
38       private void addInterfaces(Class[] classes) { 
39           add(ru.getSuperInterfaces(classes)); 
40       } 
41    
42       private void addParameterTypes(Method[] methods) { 
43           Class pclasses [] = getReferenceParameterTypes(methods); 
44           add(pclasses); 
45       } 
46    
47       private Method[] addReturnTypes(Class[] classes) { 
48           Method methods[] = getMethods(classes); 
49           Class mclasses [] = getReferenceReturnTypes(methods); 
50           add(mclasses); 
51           return methods; 
52       } 
53    
54       private void addFieldRefs(Class[] classes) { 
55           add(getReferenceFieldTypes(classes)); 
56       } 
57    
58       private Class[] initSuperClasses() { 
59           Class classes[] = ru.getSuperClasses(); 
60           add(classes); 
61           return classes; 
62       } 
63    
64       public Class[] getAllClasses() { 
65           Class classArray[] = new Class[allClasses.size()]; 
66           allClasses.copyInto(classArray); 
67           return classArray; 
68       } 
69    
70       private Class[] getReferenceFieldTypes(Class p_classes[]) { 
71           Vector v = new Vector(); 
72           for (int i = 0; i < p_classes.length; i++) { 
73               ReflectUtil temp_refUtil = new ReflectUtil(p_classes[i]); 
74               Field fields[] = temp_refUtil.getFields(); 
75               for (int j = 0; j < fields.length; j++) { 
76                   Class c = fields[j].getType(); 
77                   if (!c.isPrimitive()) 
78                       v.addElement(c); 
79               } 
80           } 
81           Class answer[] = new Class[v.size()]; 
82           v.copyInto(answer); 
83           return answer; 
84       } 
85    
86       private Method[] getMethods(Class p_classes[]) { 
87           Vector v = new Vector(); 
88           for (int i = 0; i < p_classes.length; i++) { 
89               ReflectUtil tru = new ReflectUtil(p_classes[i]); 
90               Method mthds[] = tru.getMethods(); 
91               for (int j = 0; j < mthds.length; j++) { 
92                   v.addElement(mthds[j]); 
93               } 
94           } 
95           Method answer[] = new Method[v.size()]; 
96           v.copyInto(answer); 
97           return answer; 
98       } 
99    
100      private Class[] getReferenceReturnTypes(Method p_methods[]) { 
101          Vector v = new Vector(); 
102          for (int i = 0; i < p_methods.length; i++) { 
103              Class c = p_methods[i].getReturnType(); 
104              if (c.isPrimitive()) continue; 
105              v.addElement(c); 
106          } 
107          Class answer[] = new Class[v.size()]; 
108          v.copyInto(answer); 
109          return answer; 
110      } 
111   
112      private Class[] getReferenceParameterTypes(Method p_methods[]) { 
113          Vector v = new Vector(); 
114          for (int i = 0; i < p_methods.length; i++) { 
115              Class cl[] = p_methods[i].getParameterTypes(); 
116              for (int j = 0; j < cl.length; j++) { 
117                  if (cl[j].isPrimitive()) continue; 
118                  v.addElement(cl[j]); 
119              } 
120          } 
121          Class answer[] = new Class[v.size()]; 
122          v.copyInto(answer); 
123          return answer; 
124      } 
125   
126      public static void println(Class o[]) { 
127          for (int i = 0; i < o.length; i++) 
128              System.out.println(getTypeName(o[i])); 
129      } 
130   
131      private void removeDuplicates(Class p_classes[]) { 
132          for (int i = 0; i < p_classes.length; i++) 
133              if (!allClasses.contains(p_classes[i])) 
134                  allClasses.addElement(p_classes[i]); 
135      } 
136   
137      public static String getTypeName(Class type) { 
138   
139          if (!type.isArray()) 
140              return type.getName(); 
141   
142          Class cl = type; 
143          int dimensions = 0; 
144          while (cl.isArray()) { 
145              dimensions++; 
146              cl = cl.getComponentType(); 
147          } 
148          StringBuffer sb = new StringBuffer(); 
149          sb.append(cl.getName()); 
150   
151          for (int i = 0; i < dimensions; i++) 
152              sb.append("[]"); 
153   
154          return sb.toString(); 
155      } 
156   
157      public static void main(String args[]) { 
158          TypeUtil tu = new TypeUtil(new java.util.Date()); 
159          tu.initAllFriends(); 
160          TypeUtil.println(tu.getAllClasses()); 
161      } 
162   
163  }