黑马程序员技术交流社区
标题:
关于动态代理的问题?
[打印本页]
作者:
张宏祚
时间:
2014-2-13 20:23
标题:
关于动态代理的问题?
我代理自己随便写的一个接口,都实现了,但是,当我要代理List 的时候, 总是抱错说unkown source, 具体代码如下: 首先是invocation handler 的类 , 然后是程序的main 方法
public class handler implements InvocationHandler{
private List arrayList = new ArrayList();
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("before");
arg1.invoke(arrayList, arg2);
System.out.println("after");
return null;
}
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
handler handler = new handler();
Class clazzProxyArrayList = Proxy.getProxyClass(List.class.getClassLoader(), List.class);
Constructor constructor = clazzProxyArrayList.getConstructor(InvocationHandler.class);
List shit = (List) constructor.newInstance(handler);
System.out.println(shit.size());
}
}
作者:
孤独的天奇
时间:
2014-2-13 22:08
你的invoke方法,放回的是null,但是你调用的size方法返回的是int。
好好看看视频,上面都有的
作者:
孤独的天奇
时间:
2014-2-13 22:14
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
System.out.println(((List) Proxy.newProxyInstance(
List.class.getClassLoader(),
new Class[] { List.class }, new Handler())).size());
}
main方法简化
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2