jdk dynamic proxy

核心就是 method.invoke(obj, args); 其他一切 都是为拼凑这句话服务的。这句话所在的handler 也只是$Proxy0(extend Proxy)的一个成员属性,因为proxy得到了被代理类的类加载器和接口,

所以它动态生成的$Proxy0子类包含了被代理的暴露在外的接口 也就是method。所以只需要传入参数即可,说动态代理简单,其实并不简单,涉及到的底层只是还是相当多的。

另外jdk动态代理的缺点就是被代理类一定要实现接口。

@1
public interface Calculator {
    public Integer add(Integer num1, Integer num2);
    public Integer minus(Integer num1, Integer num2);
}

@2
public class CalculatorImpl implements Calculator {
 
    @Override
    public Integer add(Integer num1, Integer num2) {
        int ret = num1 + num2;
        System.out.println("in calculatorImpl, res: " + ret);
        return ret;
    }
     
    @Override
    public Integer minus(Integer num1, Integer num2) {
        int ret = num1 - num2;
        System.out.println("int calculatorImpl, res: " + ret);
        return ret;
    }
 
}

@3
public class CalculatorHandler implements InvocationHandler {
     
    private Object obj; //被代理类
     
    public CalculatorHandler(Object obj) {
        this.obj = obj;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("in calculatorhandler, before invocation");
         
        Object ret = method.invoke(obj, args);  //执行被代理类方法
         
        System.out.println("in calculationhandler, after invocation");
        return ret;
    }
 
}

@4
CalculatorImpl calculatorImpl = new CalculatorImpl();//被代理类
CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorImpl);
Calculator calculator = (Calculator)Proxy.newProxyInstance(calculatorImpl.getClass().getClassLoader(), calculatorImpl.getClass().getInterfaces(), calculatorHandler);
System.out.println(calculator.add(1,2));
System.out.println(calculator.minus(1, 2));

@5
public final class $Proxy0 extends Proxy implements Calculator
{
 
    public $Proxy0(InvocationHandler invocationhandler)
    {
        super(invocationhandler);
    }
 
    public final boolean equals(Object obj)
    {
        try
        {
            return ((Boolean)super.h.invoke(this, m1, new Object[] {
                obj
            })).booleanValue();
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }
 
    public final String toString()
    {
        try
        {
            return (String)super.h.invoke(this, m2, null);
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }
 
    public final Integer minus(Integer integer, Integer integer1)
    {
        try
        {
            return (Integer)super.h.invoke(this, m4, new Object[] { integer, integer1});
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }
 
    public final Integer add(Integer integer, Integer integer1)
    {
        try
        {
            return (Integer)super.h.invoke(this, m3, new Object[] {
                integer, integer1
            });
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }
 
    public final int hashCode()
    {
        try
        {
            return ((Integer)super.h.invoke(this, m0, null)).intValue();
        }
        catch(Error _ex) { }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }
 
    private static Method m1;
    private static Method m2;
    private static Method m4;
    private static Method m3;
    private static Method m0;
 
    static
    {
        try
        {
            m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {
                Class.forName("java.lang.Object")
            });
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m4 = Class.forName("com.langrx.mq.Calculator").getMethod("minus", new Class[] {
                Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
            });
            m3 = Class.forName("com.langrx.mq.Calculator").getMethod("add", new Class[] {
                Class.forName("java.lang.Integer"), Class.forName("java.lang.Integer")
            });
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
        }
        catch(NoSuchMethodException nosuchmethodexception)
        {
            throw new NoSuchMethodError(nosuchmethodexception.getMessage());
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            throw new NoClassDefFoundError(classnotfoundexception.getMessage());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/lnas01/p/11252178.html
今日推荐