import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Demo23_proxy {
public static void main(String[] args) {
ArrayList<Integer> al=new ArrayList<>();
MyInvocationHandler mih=new MyInvocationHandler(al);
List<Integer> list= (List<Integer>) Proxy.newProxyInstance
(al.getClass().getClassLoader(), al.getClass().getInterfaces(), mih);
list.add(132);
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
public class MyInvocationHandler implements InvocationHandler {
private Object ob;
public MyInvocationHandler(Object ob) {
super();
this.ob = ob;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long start=System.currentTimeMillis();
TimeUnit.MILLISECONDS.sleep(10);
method.invoke(ob, args);
long end=System.currentTimeMillis();
System.out.println("方法运行时间为:"+(end-start)+"毫秒");
return null;
}
}
运行错误空指针异常,NullPointerException。哪位大神帮忙看一下。 |
|