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

com.moon.core.lang.reflect.ProxyUtil Maven / Gradle / Ivy

package com.moon.core.lang.reflect;

import com.moon.core.lang.ClassUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import static com.moon.core.lang.ThrowUtil.noInstanceError;

/**
 * @author moonsky
 */
public final class ProxyUtil {

    private final static ClassLoader LOADER = ProxyUtil.class.getClassLoader();

    private ProxyUtil() { noInstanceError(); }

    /**
     * 创建一个动态代理类
     *
     * @param obj     将要代理的对象
     * @param handler 代理拦截器
     * @param      代理目标类型
     *
     * @return 动态代理类
     */
    public static  T newProxyInstance(T obj, InvocationHandler handler) {
        Class[] classes = ClassUtil.getAllInterfacesArr(obj.getClass());
        return newProxyInstance(handler, classes);
    }

    public static  T newProxyInstance(InvocationHandler handler, Class... interfaces) {
        return (T) Proxy.newProxyInstance(LOADER, interfaces, handler);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy