ReflectionTestUtils does not require spring-aop on the classpath

Issue: SPR-15757
This commit is contained in:
Juergen Hoeller 2017-07-10 20:43:34 +02:00
parent 3714e7b044
commit dd43b6aabe
2 changed files with 20 additions and 13 deletions

View File

@ -1008,11 +1008,12 @@ project("spring-test") {
dependencies {
compile(project(":spring-core"))
optional(project(":spring-aop"))
optional(project(":spring-beans"))
optional(project(":spring-context"))
optional(project(":spring-jdbc"))
optional(project(":spring-tx"))
optional(project(":spring-orm"))
optional(project(":spring-tx"))
optional(project(":spring-web"))
optional(project(":spring-webflux"))
optional(project(":spring-webmvc"))

View File

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MethodInvoker;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
@ -71,6 +72,9 @@ public class ReflectionTestUtils {
private static final Log logger = LogFactory.getLog(ReflectionTestUtils.class);
private static final boolean springAopPresent = ClassUtils.isPresent(
"org.springframework.aop.framework.Advised", ReflectionTestUtils.class.getClassLoader());
/**
* Set the {@linkplain Field field} with the given {@code name} on the
@ -169,26 +173,27 @@ public class ReflectionTestUtils {
Assert.isTrue(targetObject != null || targetClass != null,
"Either targetObject or targetClass for the field must be specified");
Object ultimateTarget = (targetObject != null ? AopTestUtils.getUltimateTargetObject(targetObject) : null);
if (targetObject != null && springAopPresent) {
targetObject = AopTestUtils.getUltimateTargetObject(targetObject);
}
if (targetClass == null) {
targetClass = ultimateTarget.getClass();
targetClass = targetObject.getClass();
}
Field field = ReflectionUtils.findField(targetClass, name, type);
if (field == null) {
throw new IllegalArgumentException(String.format(
"Could not find field '%s' of type [%s] on %s or target class [%s]", name, type,
safeToString(ultimateTarget), targetClass));
safeToString(targetObject), targetClass));
}
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Setting field '%s' of type [%s] on %s or target class [%s] to value [%s]", name, type,
safeToString(ultimateTarget), targetClass, value));
safeToString(targetObject), targetClass, value));
}
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, ultimateTarget, value);
ReflectionUtils.setField(field, targetObject, value);
}
/**
@ -253,24 +258,25 @@ public class ReflectionTestUtils {
Assert.isTrue(targetObject != null || targetClass != null,
"Either targetObject or targetClass for the field must be specified");
Object ultimateTarget = (targetObject != null ? AopTestUtils.getUltimateTargetObject(targetObject) : null);
if (targetObject != null && springAopPresent) {
targetObject = AopTestUtils.getUltimateTargetObject(targetObject);
}
if (targetClass == null) {
targetClass = ultimateTarget.getClass();
targetClass = targetObject.getClass();
}
Field field = ReflectionUtils.findField(targetClass, name);
if (field == null) {
throw new IllegalArgumentException(String.format("Could not find field '%s' on %s or target class [%s]",
name, safeToString(ultimateTarget), targetClass));
name, safeToString(targetObject), targetClass));
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Getting field '%s' from %s or target class [%s]", name,
safeToString(ultimateTarget), targetClass));
safeToString(targetObject), targetClass));
}
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, ultimateTarget);
return ReflectionUtils.getField(field, targetObject);
}
/**