本帖最后由 早知道 于 2013-9-20 20:15 编辑
代码如下:
public class MyInvocationHandler implements InvocationHandler {
ArrayList target = new ArrayList();
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
long beginTime = System.currentTimeMillis();
Object retVal = method.invoke(target, args);
long endTime = System.currentTimeMillis();
long useTime = endTime-beginTime;
System.out.println(method.getName()+"time:"+useTime);
return retVal;
}
}
Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
// Constructor constructor = clazzProxy1.getClass().getConstructor(InvocationHandler.class);发现用这个方法的话会报错求解
Collection proxy1 = (Collection) constructor.newInstance(new MyInvocationHandler());
System.out.println(proxy1.getClass().getName());//不明白为什么不经过InvocationHandler的invoke方法
上面的报错提示:Exception in thread "main" java.lang.NoSuchMethodException: java.lang.Class.<init>(java.lang.reflect.InvocationHandler)
|