- 例子
动态代理ArrayList
List<String> list = new ArrayList<String>();
List<String> proxyList = (List<String>)Proxy.newProxyInstance(list.getClass().getClassLoader(),
list.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// something before
Object obj = method.invoke(list, args);
// something after
return obj;
}
});
proxyList.add("test");
System.out.println(list.get(0));
网友评论