1 |
|
package com.wikihouse.wildcats0201.jdbcchart.reflectionutil; |
2 |
|
|
3 |
|
import java.lang.reflect.Constructor; |
4 |
|
import java.lang.reflect.Method; |
5 |
|
import java.util.Arrays; |
6 |
|
import java.util.Iterator; |
7 |
|
import java.util.LinkedList; |
8 |
|
import java.util.List; |
9 |
|
|
10 |
|
import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.ClassNotFoundExceptionWrapper; |
11 |
|
import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.IllegalAccessExceptionWrapper; |
12 |
|
import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.InstantiationExceptionWrapper; |
13 |
|
import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.NoSuchMethodExceptionWrapper; |
14 |
|
import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.SecurityExceptionWrapper; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
public class ClassWrapper { |
24 |
|
|
25 |
|
private Class clazz; |
26 |
|
|
27 |
0 |
private ClassWrapper() { |
28 |
0 |
} |
29 |
|
|
30 |
|
public ClassWrapper(String className) { |
31 |
6 |
this(getClass(className)); |
32 |
3 |
} |
33 |
|
|
34 |
|
private static Class getClass(String className) { |
35 |
6 |
Class clazz = null; |
36 |
|
try { |
37 |
6 |
clazz = Class.forName(className); |
38 |
3 |
} catch (java.lang.ClassNotFoundException e) { |
39 |
3 |
throw new ClassNotFoundExceptionWrapper(e); |
40 |
|
} |
41 |
3 |
return clazz; |
42 |
|
} |
43 |
|
|
44 |
39 |
public ClassWrapper(Class clazz) { |
45 |
39 |
this.clazz = clazz; |
46 |
39 |
} |
47 |
|
|
48 |
|
public Class get() { |
49 |
6 |
return this.clazz; |
50 |
|
} |
51 |
|
|
52 |
|
public Object newInstance() { |
53 |
6 |
Object obj = null; |
54 |
|
try { |
55 |
6 |
obj = clazz.newInstance(); |
56 |
3 |
} catch (java.lang.InstantiationException e) { |
57 |
3 |
throw new InstantiationExceptionWrapper(e); |
58 |
|
} catch (java.lang.IllegalAccessException e) { |
59 |
0 |
throw new IllegalAccessExceptionWrapper(e); |
60 |
|
} |
61 |
3 |
return obj; |
62 |
|
} |
63 |
|
|
64 |
|
private ConstructorWrapper getConstructorWrapper(Constructor constructor) { |
65 |
6 |
return new ConstructorWrapper(constructor); |
66 |
|
} |
67 |
|
|
68 |
|
public ConstructorWrapper getConstructor(Class clazz) { |
69 |
6 |
return getConstructor(new Class[] { clazz }); |
70 |
|
} |
71 |
|
|
72 |
|
public ConstructorWrapper getConstructor(Class[] classes) { |
73 |
12 |
Constructor constructor = null; |
74 |
|
try { |
75 |
12 |
constructor = this.clazz.getConstructor(classes); |
76 |
6 |
} catch (java.lang.SecurityException e) { |
77 |
0 |
throw getSecurityException(e); |
78 |
|
} catch (java.lang.NoSuchMethodException e) { |
79 |
6 |
throw getNoSucheMethodException(e); |
80 |
|
} |
81 |
6 |
return getConstructorWrapper(constructor); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
public String toString() { |
88 |
0 |
return clazz.getName(); |
89 |
|
} |
90 |
|
|
91 |
|
public Method[] getMethods() { |
92 |
0 |
return this.clazz.getMethods(); |
93 |
|
} |
94 |
|
|
95 |
|
public Method[] getMethods(String methodName) { |
96 |
0 |
List list = new LinkedList(); |
97 |
0 |
for (Iterator it = Arrays.asList(this.getMethods()).iterator(); it |
98 |
0 |
.hasNext();) { |
99 |
0 |
Method method = (Method) it.next(); |
100 |
0 |
if (method.getName().equals(methodName)) { |
101 |
0 |
list.add(method); |
102 |
|
} |
103 |
|
} |
104 |
0 |
return (Method[]) list.toArray(new Method[0]); |
105 |
|
} |
106 |
|
|
107 |
|
public int getMethodCount(String methodName) { |
108 |
0 |
return getMethods(methodName).length; |
109 |
|
} |
110 |
|
|
111 |
|
public Method getMethod(String methodName, Class[] clazz) { |
112 |
0 |
Method method = null; |
113 |
|
try { |
114 |
0 |
method = this.clazz.getMethod(methodName, clazz); |
115 |
0 |
} catch (java.lang.SecurityException e) { |
116 |
0 |
throw getSecurityException(e); |
117 |
|
} catch (java.lang.NoSuchMethodException e) { |
118 |
0 |
throw getNoSucheMethodException(e); |
119 |
|
} |
120 |
0 |
return method; |
121 |
|
} |
122 |
|
|
123 |
|
private SecurityExceptionWrapper getSecurityException( |
124 |
|
java.lang.SecurityException exception) { |
125 |
0 |
return new SecurityExceptionWrapper(exception); |
126 |
|
} |
127 |
|
|
128 |
4 |
private NoSuchMethodExceptionWrapper getNoSucheMethodException( |
129 |
|
java.lang.NoSuchMethodException exception) { |
130 |
2 |
return new NoSuchMethodExceptionWrapper(exception); |
131 |
|
} |
132 |
|
} |