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