@@ -72,10 +72,8 @@ public class ReflectionTestUtils {
72
72
/**
73
73
* Set the {@linkplain Field field} with the given {@code name} on the
74
74
* provided {@code targetObject} to the supplied {@code value}.
75
- *
76
75
* <p>This method delegates to {@link #setField(Object, String, Object, Class)},
77
76
* supplying {@code null} for the {@code type} argument.
78
- *
79
77
* @param targetObject the target object on which to set the field; never {@code null}
80
78
* @param name the name of the field to set; never {@code null}
81
79
* @param value the value to set
@@ -87,10 +85,8 @@ public static void setField(Object targetObject, String name, Object value) {
87
85
/**
88
86
* Set the {@linkplain Field field} with the given {@code name}/{@code type}
89
87
* on the provided {@code targetObject} to the supplied {@code value}.
90
- *
91
88
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
92
89
* supplying {@code null} for the {@code targetClass} argument.
93
- *
94
90
* @param targetObject the target object on which to set the field; never {@code null}
95
91
* @param name the name of the field to set; may be {@code null} if
96
92
* {@code type} is specified
@@ -105,10 +101,8 @@ public static void setField(Object targetObject, String name, Object value, Clas
105
101
/**
106
102
* Set the static {@linkplain Field field} with the given {@code name} on
107
103
* the provided {@code targetClass} to the supplied {@code value}.
108
- *
109
104
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
110
105
* supplying {@code null} for the {@code targetObject} and {@code type} arguments.
111
- *
112
106
* @param targetClass the target class on which to set the static field;
113
107
* never {@code null}
114
108
* @param name the name of the field to set; never {@code null}
@@ -123,10 +117,8 @@ public static void setField(Class<?> targetClass, String name, Object value) {
123
117
* Set the static {@linkplain Field field} with the given
124
118
* {@code name}/{@code type} on the provided {@code targetClass} to
125
119
* the supplied {@code value}.
126
- *
127
120
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
128
121
* supplying {@code null} for the {@code targetObject} argument.
129
- *
130
122
* @param targetClass the target class on which to set the static field;
131
123
* never {@code null}
132
124
* @param name the name of the field to set; may be {@code null} if
@@ -144,12 +136,10 @@ public static void setField(Class<?> targetClass, String name, Object value, Cla
144
136
* Set the {@linkplain Field field} with the given {@code name}/{@code type}
145
137
* on the provided {@code targetObject}/{@code targetClass} to the supplied
146
138
* {@code value}.
147
- *
148
139
* <p>This method traverses the class hierarchy in search of the desired
149
140
* field. In addition, an attempt will be made to make non-{@code public}
150
141
* fields <em>accessible</em>, thus allowing one to set {@code protected},
151
142
* {@code private}, and <em>package-private</em> fields.
152
- *
153
143
* @param targetObject the target object on which to set the field; may be
154
144
* {@code null} if the field is static
155
145
* @param targetClass the target class on which to set the field; may
@@ -171,19 +161,18 @@ public static void setField(Object targetObject, Class<?> targetClass, String na
171
161
if (targetClass == null ) {
172
162
targetClass = targetObject .getClass ();
173
163
}
174
- Field field = ReflectionUtils .findField (targetClass , name , type );
175
164
176
- // Inline Assert.notNull() to avoid invoking toString() on a non-null target.
165
+ Field field = ReflectionUtils . findField ( targetClass , name , type );
177
166
if (field == null ) {
178
167
throw new IllegalArgumentException (String .format (
179
- "Could not find field [%s] of type [%s] on target object [%s] or target class [%s]" , name , type ,
180
- targetObject , targetClass ));
168
+ "Could not find field '%s' of type [%s] on target object [%s] or target class [%s]" , name , type ,
169
+ targetObject , targetClass ));
181
170
}
182
171
183
172
if (logger .isDebugEnabled ()) {
184
173
logger .debug (String .format (
185
- "Setting field [%s] of type [%s] on target object [%s] or target class [%s] to value [%s]" , name , type ,
186
- targetObject , targetClass , value ));
174
+ "Setting field '%s' of type [%s] on target object [%s] or target class [%s] to value [%s]" , name , type ,
175
+ targetObject , targetClass , value ));
187
176
}
188
177
ReflectionUtils .makeAccessible (field );
189
178
ReflectionUtils .setField (field , targetObject , value );
@@ -192,10 +181,8 @@ public static void setField(Object targetObject, Class<?> targetClass, String na
192
181
/**
193
182
* Get the value of the {@linkplain Field field} with the given {@code name}
194
183
* from the provided {@code targetObject}.
195
- *
196
184
* <p>This method delegates to {@link #getField(Object, Class, String)},
197
185
* supplying {@code null} for the {@code targetClass} argument.
198
- *
199
186
* @param targetObject the target object from which to get the field;
200
187
* never {@code null}
201
188
* @param name the name of the field to get; never {@code null}
@@ -209,16 +196,14 @@ public static Object getField(Object targetObject, String name) {
209
196
/**
210
197
* Get the value of the static {@linkplain Field field} with the given
211
198
* {@code name} from the provided {@code targetClass}.
212
- *
213
199
* <p>This method delegates to {@link #getField(Object, Class, String)},
214
200
* supplying {@code null} for the {@code targetObject} argument.
215
- *
216
201
* @param targetClass the target class from which to get the static field;
217
202
* never {@code null}
218
203
* @param name the name of the field to get; never {@code null}
219
204
* @return the field's current value
220
- * @see #getField(Object, String)
221
205
* @since 4.2
206
+ * @see #getField(Object, String)
222
207
*/
223
208
public static Object getField (Class <?> targetClass , String name ) {
224
209
return getField (null , targetClass , name );
@@ -227,24 +212,22 @@ public static Object getField(Class<?> targetClass, String name) {
227
212
/**
228
213
* Get the value of the {@linkplain Field field} with the given {@code name}
229
214
* from the provided {@code targetObject}/{@code targetClass}.
230
- *
231
215
* <p>This method traverses the class hierarchy in search of the desired
232
216
* field. In addition, an attempt will be made to make non-{@code public}
233
217
* fields <em>accessible</em>, thus allowing one to get {@code protected},
234
218
* {@code private}, and <em>package-private</em> fields.
235
- *
236
219
* @param targetObject the target object from which to get the field; may be
237
220
* {@code null} if the field is static
238
221
* @param targetClass the target class from which to get the field; may
239
222
* be {@code null} if the field is an instance field
240
223
* @param name the name of the field to get; never {@code null}
241
224
* @return the field's current value
225
+ * @since 4.2
242
226
* @see #getField(Object, String)
243
227
* @see #getField(Class, String)
244
228
* @see ReflectionUtils#findField(Class, String, Class)
245
229
* @see ReflectionUtils#makeAccessible(Field)
246
- * @see ReflectionUtils#getField(Field, Object, Object)
247
- * @since 4.2
230
+ * @see ReflectionUtils#getField(Field, Object)
248
231
*/
249
232
public static Object getField (Object targetObject , Class <?> targetClass , String name ) {
250
233
Assert .isTrue (targetObject != null || targetClass != null ,
@@ -253,18 +236,17 @@ public static Object getField(Object targetObject, Class<?> targetClass, String
253
236
if (targetClass == null ) {
254
237
targetClass = targetObject .getClass ();
255
238
}
256
- Field field = ReflectionUtils .findField (targetClass , name );
257
239
258
- // Inline Assert.notNull() to avoid invoking toString() on a non-null target.
240
+ Field field = ReflectionUtils . findField ( targetClass , name );
259
241
if (field == null ) {
260
242
throw new IllegalArgumentException (
261
- String .format ("Could not find field [%s] on target object [%s] or target class [%s]" , name ,
262
- targetObject , targetClass ));
243
+ String .format ("Could not find field '%s' on target object [%s] or target class [%s]" , name ,
244
+ targetObject , targetClass ));
263
245
}
264
246
265
247
if (logger .isDebugEnabled ()) {
266
- logger .debug (String .format ("Getting field [%s] from target object [%s] or target class [%s]" , name ,
267
- targetObject , targetClass ));
248
+ logger .debug (String .format ("Getting field '%s' from target object [%s] or target class [%s]" , name ,
249
+ targetObject , targetClass ));
268
250
}
269
251
ReflectionUtils .makeAccessible (field );
270
252
return ReflectionUtils .getField (field , targetObject );
@@ -273,17 +255,14 @@ public static Object getField(Object targetObject, Class<?> targetClass, String
273
255
/**
274
256
* Invoke the setter method with the given {@code name} on the supplied
275
257
* target object with the supplied {@code value}.
276
- *
277
258
* <p>This method traverses the class hierarchy in search of the desired
278
259
* method. In addition, an attempt will be made to make non-{@code public}
279
260
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
280
261
* {@code private}, and <em>package-private</em> setter methods.
281
- *
282
262
* <p>In addition, this method supports JavaBean-style <em>property</em>
283
263
* names. For example, if you wish to set the {@code name} property on the
284
264
* target object, you may pass either "name" or
285
265
* "setName" as the method name.
286
- *
287
266
* @param target the target object on which to invoke the specified setter
288
267
* method
289
268
* @param name the name of the setter method to invoke or the corresponding
@@ -300,17 +279,14 @@ public static void invokeSetterMethod(Object target, String name, Object value)
300
279
/**
301
280
* Invoke the setter method with the given {@code name} on the supplied
302
281
* target object with the supplied {@code value}.
303
- *
304
282
* <p>This method traverses the class hierarchy in search of the desired
305
283
* method. In addition, an attempt will be made to make non-{@code public}
306
284
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
307
285
* {@code private}, and <em>package-private</em> setter methods.
308
- *
309
286
* <p>In addition, this method supports JavaBean-style <em>property</em>
310
287
* names. For example, if you wish to set the {@code name} property on the
311
288
* target object, you may pass either "name" or
312
289
* "setName" as the method name.
313
- *
314
290
* @param target the target object on which to invoke the specified setter
315
291
* method
316
292
* @param name the name of the setter method to invoke or the corresponding
@@ -324,41 +300,41 @@ public static void invokeSetterMethod(Object target, String name, Object value)
324
300
public static void invokeSetterMethod (Object target , String name , Object value , Class <?> type ) {
325
301
Assert .notNull (target , "Target object must not be null" );
326
302
Assert .hasText (name , "Method name must not be empty" );
327
- Class <?>[] paramTypes = (type != null ? new Class <?>[] { type } : null );
303
+ Class <?>[] paramTypes = (type != null ? new Class <?>[] {type } : null );
328
304
329
305
String setterMethodName = name ;
330
306
if (!name .startsWith (SETTER_PREFIX )) {
331
307
setterMethodName = SETTER_PREFIX + StringUtils .capitalize (name );
332
308
}
309
+
333
310
Method method = ReflectionUtils .findMethod (target .getClass (), setterMethodName , paramTypes );
334
311
if (method == null && !setterMethodName .equals (name )) {
335
312
setterMethodName = name ;
336
313
method = ReflectionUtils .findMethod (target .getClass (), setterMethodName , paramTypes );
337
314
}
338
- Assert .notNull (method , "Could not find setter method [" + setterMethodName + "] on target [" + target
339
- + "] with parameter type [" + type + "]" );
315
+ if (method == null ) {
316
+ throw new IllegalArgumentException ("Could not find setter method '" + setterMethodName +
317
+ "' on target [" + target + "] with parameter type [" + type + "]" );
318
+ }
340
319
341
320
if (logger .isDebugEnabled ()) {
342
- logger .debug ("Invoking setter method [ " + setterMethodName + "] on target [" + target + "]" );
321
+ logger .debug ("Invoking setter method ' " + setterMethodName + "' on target [" + target + "]" );
343
322
}
344
323
ReflectionUtils .makeAccessible (method );
345
- ReflectionUtils .invokeMethod (method , target , new Object [] { value } );
324
+ ReflectionUtils .invokeMethod (method , target , value );
346
325
}
347
326
348
327
/**
349
328
* Invoke the getter method with the given {@code name} on the supplied
350
329
* target object with the supplied {@code value}.
351
- *
352
330
* <p>This method traverses the class hierarchy in search of the desired
353
331
* method. In addition, an attempt will be made to make non-{@code public}
354
332
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
355
333
* {@code private}, and <em>package-private</em> getter methods.
356
- *
357
334
* <p>In addition, this method supports JavaBean-style <em>property</em>
358
335
* names. For example, if you wish to get the {@code name} property on the
359
336
* target object, you may pass either "name" or
360
337
* "getName" as the method name.
361
- *
362
338
* @param target the target object on which to invoke the specified getter
363
339
* method
364
340
* @param name the name of the getter method to invoke or the corresponding
@@ -381,10 +357,13 @@ public static Object invokeGetterMethod(Object target, String name) {
381
357
getterMethodName = name ;
382
358
method = ReflectionUtils .findMethod (target .getClass (), getterMethodName );
383
359
}
384
- Assert .notNull (method , "Could not find getter method [" + getterMethodName + "] on target [" + target + "]" );
360
+ if (method == null ) {
361
+ throw new IllegalArgumentException ("Could not find getter method '" + getterMethodName +
362
+ "' on target [" + target + "]" );
363
+ }
385
364
386
365
if (logger .isDebugEnabled ()) {
387
- logger .debug ("Invoking getter method [ " + getterMethodName + "] on target [" + target + "]" );
366
+ logger .debug ("Invoking getter method ' " + getterMethodName + "' on target [" + target + "]" );
388
367
}
389
368
ReflectionUtils .makeAccessible (method );
390
369
return ReflectionUtils .invokeMethod (method , target );
@@ -393,12 +372,10 @@ public static Object invokeGetterMethod(Object target, String name) {
393
372
/**
394
373
* Invoke the method with the given {@code name} on the supplied target
395
374
* object with the supplied arguments.
396
- *
397
375
* <p>This method traverses the class hierarchy in search of the desired
398
376
* method. In addition, an attempt will be made to make non-{@code public}
399
377
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
400
378
* {@code private}, and <em>package-private</em> methods.
401
- *
402
379
* @param target the target object on which to invoke the specified method
403
380
* @param name the name of the method to invoke
404
381
* @param args the arguments to provide to the method
@@ -421,17 +398,16 @@ public static <T> T invokeMethod(Object target, String name, Object... args) {
421
398
methodInvoker .prepare ();
422
399
423
400
if (logger .isDebugEnabled ()) {
424
- logger .debug ("Invoking method [ " + name + "] on target [" + target + "] with arguments ["
425
- + ObjectUtils .nullSafeToString (args ) + "]" );
401
+ logger .debug ("Invoking method ' " + name + "' on target [" + target + "] with arguments [" +
402
+ ObjectUtils .nullSafeToString (args ) + "]" );
426
403
}
427
404
428
405
return (T ) methodInvoker .invoke ();
429
406
}
430
- catch (Exception e ) {
431
- ReflectionUtils .handleReflectionException (e );
407
+ catch (Exception ex ) {
408
+ ReflectionUtils .handleReflectionException (ex );
409
+ throw new IllegalStateException ("Should never get here" );
432
410
}
433
-
434
- throw new IllegalStateException ("Should never get here" );
435
411
}
436
412
437
413
}
0 commit comments