Support proxied objects in ReflectionTestUtils

This commit adds support for unwrapping proxies in the setField() and
getField() methods in ReflectionTestUtils.

Instead of always accessing fields directly on the supplied
targetObject (which may be a proxy), AopTestUtils is now used to obtain
the potential ultimateTargetObject which is then used for accessing
fields.

Issue: SPR-14050
This commit is contained in:
Marten Deinum
2016-03-14 15:54:50 +01:00
committed by Sam Brannen
parent d2c0885e29
commit 8d3ec50e87
2 changed files with 42 additions and 4 deletions

View File

@@ -159,8 +159,10 @@ public class ReflectionTestUtils {
Assert.isTrue(targetObject != null || targetClass != null,
"Either targetObject or targetClass for the field must be specified");
Object ultimateTargetObject = AopTestUtils.getUltimateTargetObject(targetObject);
if (targetClass == null) {
targetClass = targetObject.getClass();
targetClass = ultimateTargetObject.getClass();
}
Field field = ReflectionUtils.findField(targetClass, name, type);
@@ -176,7 +178,7 @@ public class ReflectionTestUtils {
targetObject, targetClass, value));
}
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, targetObject, value);
ReflectionUtils.setField(field, ultimateTargetObject, value);
}
/**
@@ -234,8 +236,10 @@ public class ReflectionTestUtils {
Assert.isTrue(targetObject != null || targetClass != null,
"Either targetObject or targetClass for the field must be specified");
Object ultimateTargetObject = AopTestUtils.getUltimateTargetObject(targetObject);
if (targetClass == null) {
targetClass = targetObject.getClass();
targetClass = ultimateTargetObject.getClass();
}
Field field = ReflectionUtils.findField(targetClass, name);
@@ -250,7 +254,7 @@ public class ReflectionTestUtils {
targetObject, targetClass));
}
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, targetObject);
return ReflectionUtils.getField(field, ultimateTargetObject);
}
/**