Java设计模式——代理模式 (2)

下面这个类是测试没有实现ITarget接口的代理

/** * @description: JDK动态代理对象 * @author: lmc * @create: 2019-06-12 17:00 **/ public class JdkProxy1 implements InvocationHandler { /** * 被代理对象的引用 */ private Target target; /** * @description: 获取代理之后的实例对象 * @param target 被代理对象 * @return com.lmc.gp12380.pattern.proxy.ITarget * @date 2019/6/12 19:55 * @author lmc */ public Target getProxyInstance(Target target){ this.target = target; Class<?> clazz = target.getClass(); return (Target) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { beforeBehavior(); //通过反编译工具可以查看源代码 byte [] bytes = ProxyGenerator.generateProxyClass("$Proxy0",new Class[]{ITarget.class}); FileOutputStream os = new FileOutputStream("E://$Proxy0.class"); os.write(bytes); os.close(); Object obj = method.invoke(this.target,args); afterBehavior(); return obj; } /** * @description: 被代理之前的增强行为 * @date 2019/6/12 15:56 * @author lmc */ private void beforeBehavior(){ System.out.println("执行代理之前需要做的一些事情。"); } /** * @description: 被代理之后的增强行为 * @date 2019/6/12 15:57 * @author lmc */ private void afterBehavior(){ System.out.println("执行代理之后需要做的一些事情。"); } }

下面这个类是Jdk-proxy代理生成的class字节码反编译之后的java类

public final class $Proxy0 extends Proxy implements ITarget { 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); } return false; } public final String toString() { try { return (String) super.h.invoke(this, m2, null); } catch (Error _ex) { } catch (Throwable throwable) { throw new UndeclaredThrowableException(throwable); } return null; } public final void behavior() { try { super.h.invoke(this, m3, null); return; } 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); } return 0; } private static Method m1; private static Method m2; 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]); m3 = Class.forName("com.lmc.gp12380.pattern.proxy.ITarget").getMethod("behavior", new Class[0]); 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()); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxjjs.html