这样和妻子解释:Java动态代理机制详解(JDK和CGLIB( 七 )

现在我们想将生成的代理类起名为“ElectricCarProxy” , 并保存在硬盘 , 应该使用以下语句:
ProxyUtils.generateClassFile(car.getClass(), "ElectricCarProxy");这样将在ElectricCar.class 同级目录下产生 ElectricCarProxy.class文件 。 用反编译工具如jd-gui.exe 打开 , 将会看到以下信息:
import com.foo.proxy.Rechargable;import com.foo.proxy.Vehicle;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.lang.reflect.UndeclaredThrowableException;/**生成的动态代理类的组织模式是继承Proxy类 , 然后实现需要实现代理的类上的所有接口 , 而在实现的过程中 , 则是通过将所有的方法都交给了InvocationHandler来处理 */public final class ElectricCarProxy extends Proxyimplements Rechargable, Vehicle{private static Method m1;private static Method m3;private static Method m4;private static Method m0;private static Method m2;public ElectricCarProxy(InvocationHandler paramInvocationHandler)throws{super(paramInvocationHandler);}public final boolean equals(Object paramObject)throws{try{ // 方法功能实现交给InvocationHandler处理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 recharge()throws{try{// 方法功能实现交给InvocationHandler处理this.h.invoke(this, m3, null);return;}catch (Error|RuntimeException localError){throw localError;}catch (Throwable localThrowable){throw new UndeclaredThrowableException(localThrowable);}}public final void drive()throws{try{// 方法功能实现交给InvocationHandler处理this.h.invoke(this, m4, null);return;}catch (Error|RuntimeException localError){throw localError;}catch (Throwable localThrowable){throw new UndeclaredThrowableException(localThrowable);}}public final int hashCode()throws{try{// 方法功能实现交给InvocationHandler处理return ((Integer)this.h.invoke(this, m0, null)).intValue();}catch (Error|RuntimeException localError){throw localError;}catch (Throwable localThrowable){throw new UndeclaredThrowableException(localThrowable);}}public final String toString()throws{try{// 方法功能实现交给InvocationHandler处理return (String)this.h.invoke(this, m2, null);}catch (Error|RuntimeException localError){throw localError;}catch (Throwable localThrowable){throw new UndeclaredThrowableException(localThrowable);}}static{try{//为每一个需要方法对象 , 当调用相应的方法时 , 分别将方法对象作为参数传递给InvocationHandler处理m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });m3 = Class.forName("com.foo.proxy.Rechargable").getMethod("recharge", new Class[0]);m4 = Class.forName("com.foo.proxy.Vehicle").getMethod("drive", 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]);return;}catch (NoSuchMethodException localNoSuchMethodException){throw new NoSuchMethodError(localNoSuchMethodException.getMessage());}catch (ClassNotFoundException localClassNotFoundException){throw new NoClassDefFoundError(localClassNotFoundException.getMessage());}}}


推荐阅读