RTU.setField() shouldn't call toString() on target

ReflectionTestUtils.setField() implicitly calls toString() on the target
object when arguments for a call to Assert.notNull() are built. This can
have undesirable side effects, for example if the toString() invocation
results in a thrown exception or access to an external system (e.g., a
database).

This commit addresses this issue by inlining the Assert.notNull() code,
thereby avoiding accidental invocation of toString() on a non-null
target.

Issue: SPR-9571
This commit is contained in:
Sam Brannen
2012-08-10 16:44:55 +02:00
parent 5710cf5ed2
commit df961a938e
3 changed files with 72 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -104,10 +104,18 @@ public class ReflectionTestUtils {
public static void setField(Object target, String name, Object value, Class<?> type) {
Assert.notNull(target, "Target object must not be null");
Field field = ReflectionUtils.findField(target.getClass(), name, type);
Assert.notNull(field, "Could not find field [" + name + "] on target [" + target + "]");
// SPR-9571: inline Assert.notNull() in order to avoid accidentally invoking
// toString() on a non-null target.
if (field == null) {
throw new IllegalArgumentException(String.format("Could not find field [%s] of type [%s] on target [%s]",
name, type, target));
}
if (logger.isDebugEnabled()) {
logger.debug("Setting field [" + name + "] on target [" + target + "]");
logger.debug(String.format("Setting field [%s] of type [%s] on target [%s] to value [%s]", name, type,
target,
value));
}
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, target, value);