本身就是ArrayList的代理类,但为什么不能用ArrayList- //为什么用ArrayList会报$Proxy0 cannot be cast to java.util.ArrayList异常
- // ArrayList<Object> arrayListProxy = (ArrayList<Object>)Proxy.newProxyInstance(
- List<Object> arrayListProxy = (List<Object>)Proxy.newProxyInstance(
- ArrayListProxy.class.getClassLoader(),
- new Class[]{Serializable.class,Cloneable.class,Iterable.class,Collection.class,List.class, RandomAccess.class},
- new InvocationHandler(){
- ArrayList<Object> target = new ArrayList<Object>();
- @Override
- public Object invoke(Object proxy, Method method,Object[] args) throws Throwable {
-
- long before = System.currentTimeMillis();
- // TimeUnit.SECONDS.sleep(1);
- Object obj = method.invoke(target, args);
- long after = System.currentTimeMillis();
-
- System.out.println("method ["+method.getName()+"] running time is :"+(after-before));
- return obj;
- }});
复制代码 |