JDK动态代理笔记


1、

/**
* 被代理的接口
*/
public interface IBiz {
    void fun();
}


2、

/**
* 被代理的接口实现
*/
public class BizImpl implements IBiz{
    public void fun() {
        System.out.println(">>>>BizImpl fun()");
    }
}

3、

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

/**
 * 生成代理类的类
 */
public class ProxyBuilder implements InvocationHandler {

    /**
     * 被代理对象
     */
    Object target;

    private ProxyBuilder(Object target) {
        this.target = target;
    }

    /**
     * 创建代理对象
     */
    public static Object getProxy(Object target){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new ProxyBuilder(target));
    }

    /**
     * 代理处理逻辑
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        System.out.println(">>>>start:" + System.currentTimeMillis());
        //执行被代理对象的方法
        result = method.invoke(target, args);
        System.out.println(">>>>end:" + System.currentTimeMillis());
        return result;
    }
}


4、

/**
 * 使用动态代理
 */
public class Test {
    public static void main(String[] args) {
        IBiz biz = (IBiz) ProxyBuilder.getProxy(new BizImpl());
        biz.fun();
    }
}



5、

    /**
    * 将生成的代理类写到class文件
    */
    public static void createProxyClassFile() {
        String name = "BizProxy";
        byte[] data = ProxyGenerator.generateProxyClass(name, new Class[]{IBiz.class});
        try {
            FileOutputStream out = new FileOutputStream(name + ".class");
            out.write(data);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





6、生成的代理类

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import proxy.IBiz;

public final class BizProxy extends Proxy implements IBiz {
    private static Method m1;
    private static Method m3;
    private static Method m0;
    private static Method m2;

    public BizProxy(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue();
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final void fun() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {
            return ((Integer)super.h.invoke(this, m0, (Object[])null)).intValue();
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
            m3 = Class.forName("proxy.IBiz").getMethod("fun", new Class[0]);
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值