Created
July 6, 2016 08:57
-
-
Save y0an/4ecabfa7e54ece9be8c21cd401d60d73 to your computer and use it in GitHub Desktop.
Getting Target object behind a proxy (useful for mocking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Permet d'acceder a l'object wrapper par le proxy si l'object en est un. | |
* TODO make a recursive loop because target of proxy can be a proxy! | |
* @param object | |
* @return | |
* @throws Exception | |
*/ | |
@SuppressWarnings("unchecked") | |
protected <T> T getTargetObject(final Object object) throws Exception { | |
if (AopUtils.isJdkDynamicProxy(object)) { | |
return (T) ((Advised) object).getTargetSource().getTarget(); | |
} else { | |
return (T) object; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment