1 package junitx.ddtunit.util;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class PrivilegedAccessor {
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public static Object getFieldValue(Object instance, String fieldName)
45 throws IllegalAccessException, NoSuchFieldException {
46 Field field = getField(instance.getClass(), fieldName);
47 field.setAccessible(true);
48
49 return field.get(instance);
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public static void setFieldValue(Object instance, String fieldName,
67 Object value) throws IllegalAccessException, NoSuchFieldException {
68 Field field = getField(instance.getClass(), fieldName);
69 field.setAccessible(true);
70 String fieldType = field.getType().getName();
71
72 if (value == null) {
73 field.set(instance, value);
74 } else {
75 String valueType = value.getClass().getName();
76 if (fieldType.equals(valueType)
77 || (!fieldType.startsWith("[") && !valueType
78 .startsWith("["))) {
79 field.set(instance, value);
80 } else {
81
82 Object valueArray = ClassAnalyser.createPrimitiveArray(value);
83 field.set(instance, valueArray);
84 }
85 }
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public static Object invokeMethod(Object instance, String methodName,
107 Object arg) throws NoSuchMethodException, IllegalAccessException,
108 InvocationTargetException {
109 Object[] args = new Object[1];
110 args[0] = arg;
111
112 return invokeMethod(instance, methodName, args);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public static Object invokeMethod(Object instance, String methodName,
134 Object[] args) throws NoSuchMethodException,
135 IllegalAccessException, InvocationTargetException {
136 Class[] classTypes = null;
137
138 if (args != null) {
139 classTypes = new Class[args.length];
140
141 for (int i = 0; i < args.length; i++) {
142 if (args[i] != null) {
143 classTypes[i] = args[i].getClass();
144 }
145 }
146 }
147
148 return getMethod(instance, methodName, classTypes).invoke(instance,
149 args);
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 public static Method getMethod(Object instance, String methodName,
164 Class[] classTypes) throws NoSuchMethodException {
165 Method accessMethod = getMethod(instance.getClass(), methodName,
166 classTypes);
167 accessMethod.setAccessible(true);
168
169 return accessMethod;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183 private static Field getField(Class thisClass, String fieldName)
184 throws NoSuchFieldException {
185 if (thisClass == null) {
186 throw new NoSuchFieldException("Invalid class field : " + fieldName);
187 }
188
189 Field[] fields = thisClass.getDeclaredFields();
190 Field selField = null;
191 boolean found = false;
192
193 for (int i = 0; i < fields.length; i++) {
194 selField = fields[i];
195
196 if (selField.getName().compareTo(fieldName) == 0) {
197 found = true;
198
199 break;
200 } else {
201 selField = null;
202 }
203 }
204
205 if (!found) {
206 return getField(thisClass.getSuperclass(), fieldName);
207 } else if (selField != null) {
208 return selField;
209 } else {
210 throw new NoSuchFieldException("Invalid field : " + fieldName
211 + " for class " + thisClass.getName());
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 private static Method getMethod(Class thisClass, String methodName,
230 Class[] classTypes) throws NoSuchMethodException {
231 if (thisClass == null) {
232 throw new NoSuchMethodException("Invalid method : " + methodName);
233 }
234 Method clazzMethod;
235 try {
236 clazzMethod = thisClass.getDeclaredMethod(methodName, classTypes);
237 } catch (NoSuchMethodException e) {
238 clazzMethod = getMethod(thisClass.getSuperclass(), methodName,
239 classTypes);
240 }
241 return clazzMethod;
242 }
243 }