1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.webservice.generator;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Field;
15 import java.lang.reflect.Method;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19
20
21
22
23
24 class UtilClassCollector {
25
26
27
28
29 private static final Logger LOG
30 = LoggerFactory.getLogger(UtilClassCollector.class);
31
32
33
34
35 public UtilClassCollector () {
36
37 }
38
39
40
41
42
43
44
45
46
47
48 static Set<Class> visitClassCollector(
49 Set<Class> result, Class clazz) {
50
51 if (skipClassColletor(result, clazz)) {
52
53 LOG.debug("DON'T Collect[" + clazz + "]");
54
55 } else {
56
57 List<Class> types = null;
58
59
60 if (clazz.isArray()) {
61 LOG.debug("Found an array:" + clazz);
62
63 result = visitClassCollector(result, clazz.getComponentType());
64
65 } else {
66 result.add(clazz);
67 LOG.debug("Collect[" + clazz + "]");
68
69 types = extractTypes(clazz);
70
71 for (Class currentType : types) {
72 result = visitClassCollector(result, currentType);
73 }
74 }
75
76 }
77
78 return result;
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 protected static boolean skipClassColletor(Set<Class> result, Class clazz) {
94 if (clazz == null) {
95 return true;
96 }
97
98
99 if (contains(result, clazz)) {
100 return true;
101 }
102
103 String name = clazz.getCanonicalName();
104 if (name.startsWith("java.") || name.startsWith("javax.")
105 || name.startsWith("org.omg.")) {
106 return true;
107 }
108
109 if (clazz.isPrimitive()) {
110 return true;
111 }
112
113 return false;
114 }
115
116
117
118
119
120
121
122 protected static boolean contains(Set<Class> result, Class clazz) {
123 if (result == null) {
124 return false;
125 }
126
127 String className = (clazz == null) ? null : clazz.getCanonicalName();
128
129 for (Class item : result) {
130 String itemClassName = item.getCanonicalName();
131
132 if (itemClassName.equals(className)) {
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140
141
142
143
144
145
146
147
148 public static List<Class> extractTypes(Class clazz) {
149 List<Class> types = new ArrayList<Class>();
150
151
152 types = extractTypesFromMethods(clazz, types);
153 types = extractTypesFromFields(clazz, types);
154
155 return types;
156 }
157
158
159
160
161
162
163
164 private static List<Class> exctraxtTypesFromConstructor(Class clazz,
165 List<Class> types) {
166
167 Constructor [] constructors = clazz.getConstructors();
168 if (constructors == null) {
169 return types;
170 }
171
172 for (Constructor currConstructor : constructors) {
173 types = extractFromArray(currConstructor.getParameterTypes(), types);
174 }
175
176 return types;
177 }
178
179
180
181
182
183
184
185
186
187
188 private static List<Class> extractTypesFromFields(Class clazz,
189 List<Class> types) {
190
191 Field [] fields = clazz.getFields();
192 if (fields == null) {
193 return types;
194 }
195
196 for (Field currField : fields) {
197 Class ft = currField.getType();
198 if (! types.contains(ft)) {
199 types.add(ft);
200 }
201 }
202
203 return types;
204 }
205
206
207
208
209
210
211
212 private static List<Class> extractTypesFromMethods(Class clazz,
213 List<Class> types) {
214
215 Method [] methods = clazz.getMethods();
216 if (methods == null) {
217 return types;
218 }
219
220
221
222 for (Method currentMethod : methods) {
223
224 Class [] paramTypes = currentMethod.getParameterTypes();
225 types = extractFromArray(paramTypes, types);
226
227
228 Class [] exTypes = currentMethod.getExceptionTypes();
229 types = extractFromArray(exTypes, types);
230
231
232 Class ret = currentMethod.getReturnType();
233 if ((ret != null) && (! types.contains(ret))) {
234 types.add(ret);
235 }
236 }
237
238 return types;
239 }
240
241
242
243
244
245
246
247 private static List<Class> extractFromArray(Class [] array,
248 List<Class> types) {
249
250 if (array == null) {
251 return types;
252 }
253
254 for (Class currentType : array) {
255 if (! types.contains(currentType)) {
256 types.add(currentType);
257 }
258 }
259 return types;
260 }
261
262 }