Skip to content

Instantly share code, notes, and snippets.

@y0an
Created July 6, 2016 08:57
Show Gist options
  • Save y0an/4ecabfa7e54ece9be8c21cd401d60d73 to your computer and use it in GitHub Desktop.
Save y0an/4ecabfa7e54ece9be8c21cd401d60d73 to your computer and use it in GitHub Desktop.
Getting Target object behind a proxy (useful for mocking)
/**
* 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