I\'m using Spring, at one point I would like to cast the object to its actual runtime implementation.
Example:
Class MyClass extends NotMyClass {
Now you can use
AopTestUtils.getTargetObject(proxy)
.
The implementation is the same of @siulkilulki sugestion, but it's on Spring helper
Basically when you use AOP in Spring, Spring build a Proxy for you. You have two option:
The key point here is that when an aspect is applied on your bean Spring will instance a proxy and if you try to perform a cast you will get an exception.
I hope tha this can help you
For me version from EDIT 2 didn't worked. Below one worked:
@SuppressWarnings({"unchecked"})
protected <T> T getTargetObject(Object proxy) throws Exception {
while( (AopUtils.isJdkDynamicProxy(proxy))) {
return (T) getTargetObject(((Advised)proxy).getTargetSource().getTarget());
}
return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
}
Usage:
UserServicesImpl serviceImpl = getTargetObject(serviceProxy);
serviceImpl.setUserDao(userDAO);