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

matrix.boot.based.utils.SpringProxyUtil Maven / Gradle / Ivy

There is a newer version: 2.1.10
Show newest version
package matrix.boot.based.utils;

import org.springframework.aop.Advisor;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.target.EmptyTargetSource;
import org.springframework.util.CollectionUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;

/**
 * Spring代理工具
 * @author wangcheng
 * 2021/8/19
 **/
public class SpringProxyUtil {

    /**
     * 获取注解
     * @param method 方法
     * @param targetClass 目标类
     * @param annotationClass 注解类
     * @param  泛型
     * @return 注解
     */
    public static  T getAnnotation(Method method, Class targetClass, Class annotationClass) {
        return Optional.ofNullable(method).map(m -> m.getAnnotation(annotationClass))
                .orElse(Optional.ofNullable(targetClass).map(t -> t.getAnnotation(annotationClass)).orElse(null));
    }

    /**
     * 是否存在注解
     * @param classes 类信息
     * @param annotationClass 注解类
     * @param  泛型
     * @return 是否存在
     */
    public static  boolean existsAnnotation(Class[] classes, Class annotationClass) {
        if (!CollectionUtils.isEmpty(Arrays.asList(classes))) {
            for (Class clazz : classes) {
                if (clazz == null) {
                    continue;
                }
                T t = clazz.getAnnotation(annotationClass);
                if (t != null) {
                    return true;
                }
                Method[] methods = clazz.getMethods();
                for (Method method : methods) {
                    t = method.getAnnotation(annotationClass);
                    if (t != null) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Find target class class.
     *
     * @param proxy the proxy
     * @return the class
     * @throws Exception the exception
     */
    public static Class findTargetClass(Object proxy) throws Exception {
        if (AopUtils.isAopProxy(proxy)) {
            AdvisedSupport advised = getAdvisedSupport(proxy);
            if (AopUtils.isJdkDynamicProxy(proxy)) {
                TargetSource targetSource = advised.getTargetSource();
                return targetSource instanceof EmptyTargetSource ? getFirstInterfaceByAdvised(advised)
                        : targetSource.getTargetClass();
            }
            Object target = advised.getTargetSource().getTarget();
            return findTargetClass(target);
        } else {
            return proxy == null ? null : proxy.getClass();
        }
    }

    public static Class[] findInterfaces(Object proxy) throws Exception {
        if (AopUtils.isJdkDynamicProxy(proxy)) {
            AdvisedSupport advised = getAdvisedSupport(proxy);
            return getInterfacesByAdvised(advised);
        } else {
            return new Class[]{};
        }
    }

    private static Class getFirstInterfaceByAdvised(AdvisedSupport advised) {
        Class[] interfaces = advised.getProxiedInterfaces();
        if (interfaces.length > 0) {
            return interfaces[0];
        } else {
            throw new IllegalStateException("Find the jdk dynamic proxy class that does not implement the interface");
        }
    }

    /**
     * 将advisor添加进去
     * @param advised AdvisedSupport
     * @param advisor 拦截器
     */
    public static void addAdvisor(AdvisedSupport advised, Advisor[] advisor) {
        for (Advisor avr : advisor) {
            advised.addAdvisor(0, avr);
        }
    }

    /**
     * Gets advised support.
     *
     * @param proxy the proxy
     * @return the advised support
     * @throws Exception the exception
     */
    public static AdvisedSupport getAdvisedSupport(Object proxy) throws Exception {
        Field h;
        if (AopUtils.isJdkDynamicProxy(proxy)) {
            h = proxy.getClass().getSuperclass().getDeclaredField("h");
        } else {
            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);
    }

    private static Class[] getInterfacesByAdvised(AdvisedSupport advised) {
        Class[] interfaces = advised.getProxiedInterfaces();
        if (interfaces.length > 0) {
            return interfaces;
        } else {
            throw new IllegalStateException("Find the jdk dynamic proxy class that does not implement the interface");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy