All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.orion.spring.ProxyTargets Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package com.orion.spring;

import org.springframework.aop.SpringProxy;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;

/**
 * spring获取源对象
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2020/2/10 17:35
 */
public class ProxyTargets {

    private ProxyTargets() {
    }

    /**
     * 判断是否为spring的代理类
     *
     * @param o 对象
     * @return true springProxy
     */
    public static boolean isSpringProxy(Object o) {
        return o instanceof SpringProxy;
    }

    /**
     * 判断是否为代理类
     *
     * @param o 对象
     * @return true proxy
     */
    public static boolean isProxy(Object o) {
        return o instanceof SpringProxy || (o != null && Proxy.isProxyClass(o.getClass()));
    }

    /**
     * 获取 目标对象
     *
     * @param proxy 代理对象
     * @return 目标对象
     * @throws Exception e
     */
    public static Object getTarget(Object proxy) throws Exception {
        if (!AopUtils.isAopProxy(proxy)) {
            return proxy;
        }
        if (AopUtils.isJdkDynamicProxy(proxy)) {
            return getJdkDynamicProxyTargetObject(proxy);
        } else {
            return getCglibProxyTargetObject(proxy);
        }
    }

    private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
        Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
        h.setAccessible(true);
        Object dynamicAdvisedInterceptor = h.get(proxy);
        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
    }

    private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
        Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
        h.setAccessible(true);
        AopProxy aopProxy = (AopProxy) h.get(proxy);
        Field advised = aopProxy.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy