@@ -173,14 +173,14 @@ public static void setField(Object targetObject, Class<?> targetClass, String na
173
173
Field field = ReflectionUtils .findField (targetClass , name , type );
174
174
if (field == null ) {
175
175
throw new IllegalArgumentException (String .format (
176
- "Could not find field '%s' of type [%s] on target object [%s] or target class [%s]" , name , type ,
177
- ultimateTarget , targetClass ));
176
+ "Could not find field '%s' of type [%s] on %s or target class [%s]" , name , type ,
177
+ safeToString ( ultimateTarget ) , targetClass ));
178
178
}
179
179
180
180
if (logger .isDebugEnabled ()) {
181
181
logger .debug (String .format (
182
- "Setting field '%s' of type [%s] on target object [%s] or target class [%s] to value [%s]" , name , type ,
183
- ultimateTarget , targetClass , value ));
182
+ "Setting field '%s' of type [%s] on %s or target class [%s] to value [%s]" , name , type ,
183
+ safeToString ( ultimateTarget ) , targetClass , value ));
184
184
}
185
185
ReflectionUtils .makeAccessible (field );
186
186
ReflectionUtils .setField (field , ultimateTarget , value );
@@ -253,14 +253,13 @@ public static Object getField(Object targetObject, Class<?> targetClass, String
253
253
254
254
Field field = ReflectionUtils .findField (targetClass , name );
255
255
if (field == null ) {
256
- throw new IllegalArgumentException (
257
- String .format ("Could not find field '%s' on target object [%s] or target class [%s]" , name ,
258
- ultimateTarget , targetClass ));
256
+ throw new IllegalArgumentException (String .format ("Could not find field '%s' on %s or target class [%s]" ,
257
+ name , safeToString (ultimateTarget ), targetClass ));
259
258
}
260
259
261
260
if (logger .isDebugEnabled ()) {
262
- logger .debug (String .format ("Getting field '%s' from target object [%s] or target class [%s]" , name ,
263
- ultimateTarget , targetClass ));
261
+ logger .debug (String .format ("Getting field '%s' from %s or target class [%s]" , name ,
262
+ safeToString ( ultimateTarget ) , targetClass ));
264
263
}
265
264
ReflectionUtils .makeAccessible (field );
266
265
return ReflectionUtils .getField (field , ultimateTarget );
@@ -327,13 +326,16 @@ public static void invokeSetterMethod(Object target, String name, Object value,
327
326
method = ReflectionUtils .findMethod (target .getClass (), setterMethodName , paramTypes );
328
327
}
329
328
if (method == null ) {
330
- throw new IllegalArgumentException ("Could not find setter method '" + setterMethodName +
331
- "' on target [" + target + "] with parameter type [" + type + "]" );
329
+ throw new IllegalArgumentException (String .format (
330
+ "Could not find setter method '%s' on %s with parameter type [%s]" , setterMethodName ,
331
+ safeToString (target ), type ));
332
332
}
333
333
334
334
if (logger .isDebugEnabled ()) {
335
- logger .debug ("Invoking setter method '" + setterMethodName + "' on target [" + target + "]" );
335
+ logger .debug (String .format ("Invoking setter method '%s' on %s with value [%s]" , setterMethodName ,
336
+ safeToString (target ), value ));
336
337
}
338
+
337
339
ReflectionUtils .makeAccessible (method );
338
340
ReflectionUtils .invokeMethod (method , target , value );
339
341
}
@@ -372,12 +374,12 @@ public static Object invokeGetterMethod(Object target, String name) {
372
374
method = ReflectionUtils .findMethod (target .getClass (), getterMethodName );
373
375
}
374
376
if (method == null ) {
375
- throw new IllegalArgumentException ("Could not find getter method '" + getterMethodName +
376
- "' on target [" + target + "]" );
377
+ throw new IllegalArgumentException (String . format (
378
+ "Could not find getter method '%s' on %s" , getterMethodName , safeToString ( target )) );
377
379
}
378
380
379
381
if (logger .isDebugEnabled ()) {
380
- logger .debug ("Invoking getter method '" + getterMethodName + " ' on target [" + target + "]" );
382
+ logger .debug (String . format ( "Invoking getter method '%s ' on %s" , getterMethodName , safeToString ( target )) );
381
383
}
382
384
ReflectionUtils .makeAccessible (method );
383
385
return ReflectionUtils .invokeMethod (method , target );
@@ -412,8 +414,8 @@ public static <T> T invokeMethod(Object target, String name, Object... args) {
412
414
methodInvoker .prepare ();
413
415
414
416
if (logger .isDebugEnabled ()) {
415
- logger .debug ("Invoking method '" + name + " ' on target [" + target + "] with arguments [" +
416
- ObjectUtils .nullSafeToString (args ) + "]" );
417
+ logger .debug (String . format ( "Invoking method '%s ' on %s with arguments %s" , name , safeToString ( target ),
418
+ ObjectUtils .nullSafeToString (args )) );
417
419
}
418
420
419
421
return (T ) methodInvoker .invoke ();
@@ -424,4 +426,14 @@ public static <T> T invokeMethod(Object target, String name, Object... args) {
424
426
}
425
427
}
426
428
429
+ private static String safeToString (Object target ) {
430
+ try {
431
+ return String .format ("target object [%s]" , target );
432
+ }
433
+ catch (Exception ex ) {
434
+ return String .format ("target of type [%s] whose toString() method threw [%s]" ,
435
+ (target != null ? target .getClass ().getName () : "unknown" ), ex );
436
+ }
437
+ }
438
+
427
439
}
0 commit comments