Skip to content

Instantly share code, notes, and snippets.

@geowarin
Created May 7, 2015 17:49

Revisions

  1. geowarin created this gist May 7, 2015.
    23 changes: 23 additions & 0 deletions ProxyUtils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    package masterspringmvc4.utils;

    import org.springframework.aop.TargetSource;
    import org.springframework.aop.framework.Advised;
    import org.springframework.aop.support.AopUtils;

    public class ProxyUtils {
    public static <T> T getProxyTarget(Object proxy) {
    if (!AopUtils.isAopProxy(proxy)) {
    throw new IllegalStateException("Target must be a proxy");
    }
    TargetSource targetSource = ((Advised) proxy).getTargetSource();
    return getTarget(targetSource);
    }

    private static <T> T getTarget(TargetSource targetSource) {
    try {
    return (T) targetSource.getTarget();
    } catch (Exception e) {
    throw new IllegalStateException(e);
    }
    }
    }