import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;public class demo4 {
public static void main(String[] args) {
ArrayList al = new ArrayList();
MyInvocationHandler my = new MyInvocationHandler(al);
ArrayList proxy = (ArrayList)Proxy.newProxyInstance(al.getClass().getClassLoader(), al.getClass().getInterfaces(),my);
proxy.add("hello");
proxy.add("world");
proxy.add("java");
proxy.add("javaee");
System.out.println(proxy.toString());
}
}
class MyInvocationHandler implements InvocationHandler{
private Object target;
public MyInvocationHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long start = System.currentTimeMillis();
Thread.sleep(10);
Object obj = method.invoke(target, args);
long end = System.currentTimeMillis();
System.out.println(method.getName()+"运行了"+(end-start));
return obj;
}
}