1
2
3
4
5
6
7
8
9
10
11 package com.legstar.coxb.util;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.Collection;
16 import java.util.List;
17
18
19
20
21
22 public final class ClassUtil {
23
24
25 private ClassUtil() {
26
27 }
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public static Class < ? > loadClass(final String packageName,
44 final String className) throws ClassNotFoundException {
45 return loadClass(toQualifiedClassName(packageName, className));
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static Class < ? > loadClass(final String qualifiedClassName)
62 throws ClassNotFoundException {
63 ClassLoader contextClassLoader = Thread.currentThread()
64 .getContextClassLoader();
65 if (contextClassLoader == null) {
66 return Class.forName(qualifiedClassName);
67 }
68 try {
69 return contextClassLoader.loadClass(qualifiedClassName);
70 } catch (ClassNotFoundException e) {
71 return Class.forName(qualifiedClassName);
72 }
73 }
74
75
76
77
78
79
80
81
82
83
84 public static Object newObject(final String packageName,
85 final String className) throws ClassLoadingException {
86 return newObject(toQualifiedClassName(packageName, className));
87
88 }
89
90
91
92
93
94
95
96
97
98 public static Object newObject(final String qualifiedClassName)
99 throws ClassLoadingException {
100
101 try {
102 Class < ? > clazz = loadClass(qualifiedClassName);
103 return clazz.newInstance();
104 } catch (InstantiationException e) {
105 throw new ClassLoadingException(e);
106 } catch (IllegalAccessException e) {
107 throw new ClassLoadingException(e);
108 } catch (ClassNotFoundException e) {
109 throw new ClassLoadingException(e);
110 }
111 }
112
113
114
115
116
117
118
119
120 public static String toQualifiedClassName(final String packageName,
121 final String className) {
122 return (packageName == null) ? className : packageName + '.'
123 + className;
124 }
125
126
127
128
129
130
131
132 public static ClassName toClassName(final String qualifiedClassName) {
133 ClassName className = new ClassName();
134 int pos = qualifiedClassName.lastIndexOf('.');
135 className.packageName = (pos == -1) ? null : qualifiedClassName
136 .substring(0, pos);
137 className.className = (pos == -1) ? qualifiedClassName
138 : qualifiedClassName.substring(pos + 1);
139 return className;
140 }
141
142
143
144
145
146 public static class ClassName {
147
148 public String className;
149
150 public String packageName;
151 }
152
153
154
155
156
157
158
159
160
161
162 public static Method getCreatorMethod(final Object objectFactory,
163 final String jaxbTypeName) throws ClassMethodException {
164 try {
165 Method creator = objectFactory.getClass().getMethod(
166 "create" + toPropertyType(jaxbTypeName));
167 return creator;
168 } catch (NoSuchMethodException e) {
169 throw (new ClassMethodException(e));
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182 public static Method getGetterMethod(final Object parentObject,
183 final String getterPrefix, final String jaxbName)
184 throws ClassMethodException {
185 String getterName = getGetterName(getterPrefix, jaxbName);
186 try {
187 Method getter = parentObject.getClass().getMethod(getterName);
188 return getter;
189 } catch (NoSuchMethodException e) {
190 throw (new ClassMethodException(e));
191 }
192 }
193
194
195
196
197
198
199
200
201 public static String getGetterName(final String getterPrefix,
202 final String fieldName) {
203 return getterPrefix + Character.toUpperCase(fieldName.charAt(0))
204 + fieldName.substring(1);
205 }
206
207
208
209
210
211
212
213
214
215
216 public static Method getSetterMethod(final Object parentObject,
217 final String jaxbName, final Class < ? > jaxbType)
218 throws ClassMethodException {
219 String setterName = getSetterMethodName(jaxbName);
220 try {
221 Class < ? >[] param = { jaxbType };
222 Method setter = parentObject.getClass()
223 .getMethod(setterName, param);
224 return setter;
225 } catch (NoSuchMethodException e) {
226 throw (new ClassMethodException(e));
227 }
228 }
229
230
231
232
233
234
235
236 public static String getSetterMethodName(final String jaxbName) {
237 return "set" + NameUtil.upperFirstChar(jaxbName)
238 + jaxbName.substring(1);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public static String toPropertyType(final String genericType) {
258
259 String type;
260 int lp = genericType.lastIndexOf('.');
261 if (lp == -1) {
262 type = genericType;
263 } else {
264 type = genericType.substring(lp + 1);
265 if (type.charAt(type.length() - 1) == '>') {
266 type = type.substring(0, type.length() - 1);
267 }
268 }
269
270
271 if (type.compareTo("byte") == 0) {
272 return "Byte";
273 } else if (type.compareTo("[B") == 0) {
274 return "byte[]";
275 } else if (type.compareTo("short") == 0) {
276 return "Short";
277 } else if (type.compareTo("int") == 0) {
278 return "Integer";
279 } else if (type.compareTo("long") == 0) {
280 return "Long";
281 } else if (type.compareTo("float") == 0) {
282 return "Float";
283 } else if (type.compareTo("double") == 0) {
284 return "Double";
285 } else if (type.compareTo("boolean") == 0) {
286 return "Boolean";
287 }
288 return type;
289 }
290
291
292
293
294
295
296
297
298
299
300
301 public static Object invokeGetProperty(final Object parentObject,
302 final String jaxbName) throws ClassInvokeException {
303 Object result;
304 try {
305
306
307
308
309 Method getter = getGetterMethod(parentObject, "get", jaxbName);
310 result = getter.invoke(parentObject);
311 } catch (IllegalAccessException e) {
312 throw (new ClassInvokeException(e));
313 } catch (InvocationTargetException e) {
314 throw (new ClassInvokeException(e));
315 } catch (ClassMethodException e) {
316 throw (new ClassInvokeException(e));
317 }
318 return result;
319 }
320
321
322
323
324
325
326
327
328
329
330 public static Object invokeGetProperty(final Object parentObject,
331 final String jaxbName, final Class < ? > jaxbType,
332 final int maxOccurs) throws ClassInvokeException {
333 Object result;
334 try {
335
336
337
338
339 Method getter = getGetterMethod(parentObject,
340 getGetterPrefix(jaxbType, maxOccurs), jaxbName);
341 result = getter.invoke(parentObject);
342 } catch (IllegalAccessException e) {
343 throw (new ClassInvokeException(e));
344 } catch (InvocationTargetException e) {
345 throw (new ClassInvokeException(e));
346 } catch (ClassMethodException e) {
347 throw (new ClassInvokeException(e));
348 }
349 return result;
350 }
351
352
353
354
355
356
357
358
359 public static String getGetterPrefix(final Class < ? > fieldType,
360 final int maxOccurs) {
361 String getterPrefix = "get";
362 if (maxOccurs <= 1) {
363 if (fieldType == boolean.class) {
364 getterPrefix = "is";
365 } else if (fieldType == Boolean.class) {
366 getterPrefix = "is";
367 }
368 }
369 return getterPrefix;
370 }
371
372
373
374
375
376
377
378
379
380
381 @SuppressWarnings({ "rawtypes" })
382 public static void invokeSetProperty(final Object parentObject,
383 final String jaxbName, final Object value,
384 final Class < ? > javaType) throws ClassInvokeException {
385
386 try {
387
388
389
390
391 if (value instanceof List) {
392 Method getter = getGetterMethod(parentObject, "get", jaxbName);
393 Class < ? > listClass = getter.getReturnType();
394 Method addAll = listClass.getMethod("addAll", Collection.class);
395 addAll.invoke(getter.invoke(parentObject), (Collection) value);
396 } else {
397 Method setter = getSetterMethod(parentObject, jaxbName,
398 javaType);
399 setter.invoke(parentObject, value);
400 }
401 } catch (IllegalAccessException e) {
402 throw (new ClassInvokeException(e));
403 } catch (NoSuchMethodException e) {
404 throw (new ClassInvokeException(e));
405 } catch (InvocationTargetException e) {
406 throw (new ClassInvokeException(e));
407 } catch (ClassMethodException e) {
408 throw (new ClassInvokeException(e));
409 }
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426 public static Object addComplexProperty(final Object objectFactory,
427 final Object parentObject, final String jaxbName,
428 final String jaxbTypeName) throws ClassInvokeException {
429
430 try {
431 Method creator = getCreatorMethod(objectFactory, jaxbTypeName);
432 Object result = creator.invoke(objectFactory);
433
434
435
436
437
438 Method setter = getSetterMethod(parentObject, jaxbName,
439 creator.getReturnType());
440 setter.invoke(parentObject, result);
441 return result;
442 } catch (IllegalAccessException e) {
443 throw new ClassInvokeException(e);
444 } catch (InvocationTargetException e) {
445 throw new ClassInvokeException(e);
446 } catch (ClassMethodException e) {
447 throw new ClassInvokeException(e);
448 }
449 }
450
451
452
453
454
455
456
457
458
459
460
461 public static Object invokeGetIndexedProperty(final Object parentObject,
462 final int index) throws ClassInvokeException {
463
464 Object result;
465 try {
466
467
468
469
470 Method getter = parentObject.getClass().getMethod("get", int.class);
471 result = getter.invoke(parentObject, index);
472 } catch (IllegalAccessException e) {
473 throw new ClassInvokeException(e);
474 } catch (NoSuchMethodException e) {
475 throw new ClassInvokeException(e);
476 } catch (InvocationTargetException e) {
477
478 if (e.getTargetException().getClass().getName()
479 .compareTo("java.lang.IndexOutOfBoundsException") == 0) {
480 return null;
481 } else {
482 throw new ClassInvokeException(e);
483 }
484 }
485 return result;
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499
500 public static Object addComplexIndexedProperty(final Object objectFactory,
501 final Object parentObject, final String jaxbTypeName,
502 final int index) throws ClassInvokeException {
503
504 try {
505 Method creator = getCreatorMethod(objectFactory, jaxbTypeName);
506 Object result = creator.invoke(objectFactory);
507
508
509
510
511
512 Class < ? >[] param = { int.class, Object.class };
513 Method add = parentObject.getClass().getMethod("add", param);
514 add.invoke(parentObject, index, result);
515 return result;
516 } catch (IllegalAccessException e) {
517 throw new ClassInvokeException(e);
518 } catch (NoSuchMethodException e) {
519 throw new ClassInvokeException(e);
520 } catch (InvocationTargetException e) {
521 throw new ClassInvokeException(e);
522 } catch (ClassMethodException e) {
523 throw new ClassInvokeException(e);
524 }
525 }
526
527
528
529
530
531
532
533
534
535
536 public static void addSimpleIndexedProperty(final Object parentObject,
537 final int index, final Object value) throws ClassInvokeException {
538
539 try {
540
541
542
543
544 Class < ? >[] param = { int.class, Object.class };
545 Method setter = parentObject.getClass().getMethod("add", param);
546 setter.invoke(parentObject, index, value);
547 } catch (IllegalAccessException e) {
548 throw new ClassInvokeException(e);
549 } catch (NoSuchMethodException e) {
550 throw new ClassInvokeException(e);
551 } catch (InvocationTargetException e) {
552 throw new ClassInvokeException(e);
553 }
554 }
555
556
557
558
559
560
561
562
563 public static boolean isEnum(final String type) {
564 if (type == null) {
565 return false;
566 }
567 if (type.compareToIgnoreCase("String") == 0) {
568 return false;
569 } else if (type.compareToIgnoreCase("[B") == 0) {
570 return false;
571 } else if (type.compareToIgnoreCase("byte[]") == 0) {
572 return false;
573 } else if (type.compareToIgnoreCase("short") == 0) {
574 return false;
575 } else if (type.compareToIgnoreCase("int") == 0) {
576 return false;
577 } else if (type.compareToIgnoreCase("Integer") == 0) {
578 return false;
579 } else if (type.compareToIgnoreCase("long") == 0) {
580 return false;
581 } else if (type.compareToIgnoreCase("float") == 0) {
582 return false;
583 } else if (type.compareToIgnoreCase("double") == 0) {
584 return false;
585 } else if (type.compareToIgnoreCase("BigInteger") == 0) {
586 return false;
587 } else if (type.compareToIgnoreCase("BigDecimal") == 0) {
588 return false;
589 } else if (type.compareToIgnoreCase("byte") == 0) {
590 return false;
591 } else if (type.compareToIgnoreCase("boolean") == 0) {
592 return false;
593 }
594 return true;
595 }
596
597 }