package com.cgm.invokelist;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class InvokeList {
public static void main(String[] args) {
final List list=new ArrayList();
//代理类拦截list
Object oo=Proxy.newProxyInstance(InvokeList.class.getClassLoader(),
new Class[]{ //实现的很多接口
List.class
}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println(method.getName()); //执行哪个方法,就被拦截
if (args!=null) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
Object retValue=method.invoke(list, args);
return retValue;
}
});
List list2=(List) oo;
list2.add("jack");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class InvokeList {
public static void main(String[] args) {
final List list=new ArrayList();
//代理类拦截list
Object oo=Proxy.newProxyInstance(InvokeList.class.getClassLoader(),
new Class[]{ //实现的很多接口
List.class
}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println(method.getName()); //执行哪个方法,就被拦截
if (args!=null) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
Object retValue=method.invoke(list, args);
return retValue;
}
});
List list2=(List) oo;
list2.add("jack");
}
}