如果是下图的代码运行,则会报:java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.util.ArrayList,但是换成注释行的,就可以通过。我想写的代理类是想代理ArralList的,。里面的参数都是和ArrayList相关的,但是最后返回的类型却不能是ArrayList,而是Collection?哪位知道原因,麻烦告诉我下,谢谢。
ArrayListcl = (ArrayList)Proxy.newProxyInstance(
// Collection cl = (Collection)Proxy.newProxyInstance(
ArrayList.class.getClassLoader(),
ArrayList.class.getInterfaces(),
new InvocationHandler(){
ArrayList al = new ArrayList();
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
long begintime = System.currentTimeMillis();
Object ob = method.invoke(al, args);
long endtime = System.currentTimeMillis();
System.out.println("运行的时间是:"+ String.valueOf(endtime - begintime));
return ob;
}});
cl.add("谢谢");
cl.add("麻烦了");
|
|