1 |
|
package com.wikihouse.wildcats0201.jdbcchart.reflectionutil; |
2 |
|
|
3 |
|
import java.lang.reflect.Constructor; |
4 |
|
import java.lang.reflect.InvocationTargetException; |
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
public final class ReflectionUtil { |
13 |
|
|
14 |
0 |
private ReflectionUtil() { |
15 |
0 |
} |
16 |
|
|
17 |
|
public static ClassWrapper forName(String className) { |
18 |
0 |
Class clazz = null; |
19 |
|
try { |
20 |
0 |
clazz = Class.forName(className); |
21 |
0 |
} catch (ClassNotFoundException e) { |
22 |
0 |
throw new RuntimeException(getExceptionName(e) + "/" + className); |
23 |
|
} |
24 |
0 |
return new ClassWrapper(clazz); |
25 |
|
} |
26 |
|
|
27 |
|
public static ConstructorWrapper getConstructor(ClassWrapper clazz, |
28 |
|
ClassArrayWrapper args) { |
29 |
0 |
Constructor constructor = null; |
30 |
|
try { |
31 |
0 |
constructor = clazz.get().getConstructor(args.get()); |
32 |
0 |
} catch (SecurityException e) { |
33 |
0 |
throw new RuntimeException(getExceptionName(e) + "/" + clazz + "/" |
34 |
0 |
+ args); |
35 |
0 |
} catch (NoSuchMethodException e) { |
36 |
0 |
throw new RuntimeException(getExceptionName(e) + "/" + clazz + "/" |
37 |
0 |
+ args); |
38 |
|
} |
39 |
0 |
return new ConstructorWrapper(constructor); |
40 |
|
} |
41 |
|
|
42 |
|
public static Object newInstance(ConstructorWrapper constructor, |
43 |
|
ObjectArrayWrapper objects) { |
44 |
0 |
Object obj = null; |
45 |
|
try { |
46 |
0 |
obj = constructor.get().newInstance(objects.get()); |
47 |
0 |
} catch (IllegalArgumentException e) { |
48 |
0 |
throw new RuntimeException(e); |
49 |
0 |
} catch (InstantiationException e) { |
50 |
0 |
throw new RuntimeException(e); |
51 |
0 |
} catch (IllegalAccessException e) { |
52 |
0 |
throw new RuntimeException(e); |
53 |
0 |
} catch (InvocationTargetException e) { |
54 |
0 |
throw new RuntimeException(e.getTargetException()); |
55 |
|
} |
56 |
0 |
return obj; |
57 |
|
} |
58 |
|
|
59 |
|
private static String getExceptionName(Exception e) { |
60 |
0 |
return e.getClass().getName(); |
61 |
|
} |
62 |
|
|
63 |
|
} |