我代理自己随便写的一个接口,都实现了,但是,当我要代理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());
}
} |