读书人

施用DynamicProxy和Reflect记录Object

发布时间: 2012-10-27 10:42:26 作者: rapoo

使用DynamicProxy和Reflect记录Object的修改历史
可以用于增量的序列化对象,但是通过记录历史会有冗余,更好的方法是对比Object或者Class字节码。




package com.alzhang.proxy;import java.lang.reflect.Proxy;public class ProxyTest {public static void consumer(Interface iface){iface.doSomething();iface.somethingElse("banaba");}/** * @param args * @throws Exception  */public static void main(String[] args) throws Exception {RealObject real = new RealObject();Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(), new Class[]{Interface.class}, new DynamicProxyHandler(real));consumer(proxy);RealObject real2 = new RealObject();real2.stack();real2.history = real.history;real2.invoke();real2.stack();}}


读书人网 >编程

热点推荐