Java培训实战教程之将代理对象保存到本地文件

       我们知道,在Java中,常用的动态代理技术有JDK的动态代理和cglib动态代理,但是不管是哪种方式,代理对象都是在程序运行时,运用反射机制动态创建而成,而我们并不能直观的看到生成的代理对象对应的Java源代码。下面,我就提供一种方式,可以将通过动态代理创建的代理对象的字节码文件保存到磁盘,再通过反编译工具查看生成文件的内容,这样就可以加深我们对于代理对象的理解。

  JDK的动态代理依靠接口实现,如果有些类并没有实现接口,则不能使用JDK代理,这就要使用cglib动态代理了,在此,我们以JDK的动态代理为例。

  在JDK中提供了一个ProxyGenerator类,可以将代理对象的字节码得到,我们再通过一个输出流就可以将文件保存到磁盘了。

  首先定义一个接口:

  public class JdkProxyTest {

  public static void main(String[] args) throws Exception {

  final IUserDao target = new UserDaoImpl();

  IUserDao proxy = (IUserDao) Proxy.newProxyInstance(target.getClass()

  .getClassLoader(), target.getClass().getInterfaces(),

  new InvocationHandler() {

  public Object invoke(Object proxy, Method method,

  Object[] args) throws Throwable {

  System.out.println(method.getName());

  return method.invoke(target, args);

  }

  });

  byte[] proxyClass = ProxyGenerator.generateProxyClass(proxy.getClass()

  .getSimpleName(), proxy.getClass().getInterfaces());

  //将字节码文件保存到D盘,文件名为$Proxy0.class

  FileOutputStream outputStream = new FileOutputStream(new File(

  d:\\$Proxy0.class ));

  outputStream.write(proxyClass);

  outputStream.flush();

  outputStream.close();

  }

  import cn.itcast.jdkproxy.IUserDao;

  import java.lang.reflect.InvocationHandler;

  import java.lang.reflect.Method;

  import java.lang.reflect.Proxy;

  import java.lang.reflect.UndeclaredThrowableException;

  public final class $Proxy0 extends Proxy

  implements IUserDao

  {

  private static Method m3;

  private static Method m1;

  private static Method m0;

  private static Method m2;

  public $Proxy0(InvocationHandler paramInvocationHandler)

  throws

  {

  super(paramInvocationHandler);

  }

  public final void save()

  throws

  {

  try

  {

  this.h.invoke(this, m3, null);

  return;

  }

  catch (RuntimeException localRuntimeException)

  {

  throw localRuntimeException;

  }

  catch (Throwable localThrowable)

  {

  }

  throw new UndeclaredThrowableException(localThrowable);

  }

  public final boolean equals(Object paramObject)

  throws

  {

  try

  {

  return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();

  }

  catch (RuntimeException localRuntimeException)

  {

  throw localRuntimeException;

  }

  catch (Throwable localThrowable)

  {

  }

  throw new UndeclaredThrowableException(localThrowable);

  }

       叩丁狼Java培训实战教程之将代理对象保存到本地文件

  public final int hashCode()

  throws

  {

  try

  {

  return ((Integer)this.h.invoke(this, m0, null)).intValue();

  }

  catch (RuntimeException localRuntimeException)

  {

  throw localRuntimeException;

  }

  catch (Throwable localThrowable)

  {

  }

  throw new UndeclaredThrowableException(localThrowable);

  }

  public final String toString()

  throws

  {

  try

  {

  return (String)this.h.invoke(this, m2, null);

  }

  catch (RuntimeException localRuntimeException)

  {

  throw localRuntimeException;

  }

  catch (Throwable localThrowable)

  {

  }

  throw new UndeclaredThrowableException(localThrowable);

  }

  static

  {

  try

  {

  m3 = Class.forName( cn.itcast.jdkproxy.IUserDao ).getMethod( save , new Class[0]);

  m1 = Class.forName( java.lang.Object ).getMethod( equals , new Class[] { Class.forName( java.lang.Object ) });

  m0 = Class.forName( java.lang.Object ).getMethod( hashCode , new Class[0]);

  m2 = Class.forName( java.lang.Object ).getMethod( toString , new Class[0]);

  return;

  }

  catch (NoSuchMethodException localNoSuchMethodException)

  {

  throw new NoSuchMethodError(localNoSuchMethodException.getMessage());

  }

  catch (ClassNotFoundException localClassNotFoundException)

  {

  }

  throw new NoClassDefFoundError(localClassNotFoundException.getMessage());

  }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值