ReflectionTestUtils does not require spring-aop on the classpath
Issue: SPR-15757
This commit is contained in:
parent
3714e7b044
commit
dd43b6aabe
|
@ -1008,11 +1008,12 @@ project("spring-test") {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile(project(":spring-core"))
|
compile(project(":spring-core"))
|
||||||
|
optional(project(":spring-aop"))
|
||||||
optional(project(":spring-beans"))
|
optional(project(":spring-beans"))
|
||||||
optional(project(":spring-context"))
|
optional(project(":spring-context"))
|
||||||
optional(project(":spring-jdbc"))
|
optional(project(":spring-jdbc"))
|
||||||
optional(project(":spring-tx"))
|
|
||||||
optional(project(":spring-orm"))
|
optional(project(":spring-orm"))
|
||||||
|
optional(project(":spring-tx"))
|
||||||
optional(project(":spring-web"))
|
optional(project(":spring-web"))
|
||||||
optional(project(":spring-webflux"))
|
optional(project(":spring-webflux"))
|
||||||
optional(project(":spring-webmvc"))
|
optional(project(":spring-webmvc"))
|
||||||
|
|
|
@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.MethodInvoker;
|
import org.springframework.util.MethodInvoker;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
@ -71,6 +72,9 @@ public class ReflectionTestUtils {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(ReflectionTestUtils.class);
|
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
|
* 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,
|
Assert.isTrue(targetObject != null || targetClass != null,
|
||||||
"Either targetObject or targetClass for the field must be specified");
|
"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) {
|
if (targetClass == null) {
|
||||||
targetClass = ultimateTarget.getClass();
|
targetClass = targetObject.getClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
Field field = ReflectionUtils.findField(targetClass, name, type);
|
Field field = ReflectionUtils.findField(targetClass, name, type);
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(String.format(
|
||||||
"Could not find field '%s' of type [%s] on %s or target class [%s]", name, type,
|
"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()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format(
|
logger.debug(String.format(
|
||||||
"Setting field '%s' of type [%s] on %s or target class [%s] to value [%s]", name, type,
|
"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.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,
|
Assert.isTrue(targetObject != null || targetClass != null,
|
||||||
"Either targetObject or targetClass for the field must be specified");
|
"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) {
|
if (targetClass == null) {
|
||||||
targetClass = ultimateTarget.getClass();
|
targetClass = targetObject.getClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
Field field = ReflectionUtils.findField(targetClass, name);
|
Field field = ReflectionUtils.findField(targetClass, name);
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
throw new IllegalArgumentException(String.format("Could not find field '%s' on %s or target class [%s]",
|
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()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Getting field '%s' from %s or target class [%s]", name,
|
logger.debug(String.format("Getting field '%s' from %s or target class [%s]", name,
|
||||||
safeToString(ultimateTarget), targetClass));
|
safeToString(targetObject), targetClass));
|
||||||
}
|
}
|
||||||
ReflectionUtils.makeAccessible(field);
|
ReflectionUtils.makeAccessible(field);
|
||||||
return ReflectionUtils.getField(field, ultimateTarget);
|
return ReflectionUtils.getField(field, targetObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue