java动态代理

最近复习了一下java动态代理

jdk中提供了java.lang.reflect.Proxy方法来为接口创建动态代理(Proxy只能为接口生成动态代理)

参与成员:

自己定义的接口类

接口类的实现类

定义一个类实现java.lang.reflect.InvocationHandler接口

,这个类应该有一个接口类的成员变量

实现invoke方法,做特定操作,并调用实际的接口类实现对象的方法

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      System.out.println("enter into " + method.getName());
      Object result = null;
      if(method!=null){
          result=method.invoke(foo,args) ;
      }
    System.out.println("exit the method"+method.getName());
    return result;
}
调用类
Foo newFoo= new FooImpl(10);
LogInvocationHandler handler = new LogInvocationHandler(newFoo);
Class<?>[] interfaces=new Class<?>[]{Foo.class};
Foo proxy= (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),interfaces,handler);
proxy.add(2);
proxy.调用方法newProxyInstance为接口创建一个动态的代理类
产生这个类字节码的是sun.misc.ProxyGenerator类,通过调用其generateProxyClass来产生这个字节码
如果像查看运行过程中产生的类字节码需要设置
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
这样在com/sun/proxy下可以见到其字节码文件,使用jd-gui对其反编译,可以见到其所有的方法都是转调用handler的invoke方法
public final class $Proxy0 extends Proxy
  implements Foo
{
  private static Method m1;
  private static Method m5;
  private static Method m3;
  private static Method m2;
  private static Method m4;
  private static Method m0;


  public $Proxy0(InvocationHandler paramInvocationHandler)
    throws 
  {
    super(paramInvocationHandler);
  }


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


  public final void minus(int paramInt)
    throws 
  {
    try
    {
      this.h.invoke(this, m5, new Object[] { Integer.valueOf(paramInt) });
      return;
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }


  public final void add(int paramInt)
    throws 
  {
    try
    {
      this.h.invoke(this, m3, new Object[] { Integer.valueOf(paramInt) });
      return;
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }


  public final String toString()
    throws 
  {
    try
    {
      return (String)this.h.invoke(this, m2, null);
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }


  public final int getValue()
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m4, null)).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }


  public final int hashCode()
    throws 
  {
    try
    {
      return ((Integer)this.h.invoke(this, m0, null)).intValue();
    }
    catch (Error|RuntimeException localError)
    {
      throw localError;
    }
    catch (Throwable localThrowable)
    {
      throw new UndeclaredThrowableException(localThrowable);
    }
  }


  static
  {
    try
    {
      m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
      m5 = Class.forName("*.Foo").getMethod("minus", new Class[] { Integer.TYPE });
      m3 = Class.forName("*.Foo").getMethod("add", new Class[] { Integer.TYPE });
      m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
      m4 = Class.forName("*.Foo").getMethod("getValue", new Class[0]);
      m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
      return;
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值