你的代码不完整,无法知道具体什么错误,参考下面代码:
package hh;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
class Hello {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Object> proxy1 = new ArrayList<Object>();
List<Object> proxy = (List<Object>) Hello.getProxy(proxy1);
proxy.add("helen");
proxy.add("never");
proxy.add("for");
proxy.remove(1);
System.out.println(proxy);
}
private static Object getProxy(final Object target) {
class myInvocationHandler implements InvocationHandler {
private Object obj;
myInvocationHandler(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long beginTime = System.currentTimeMillis();
Object ret = method.invoke(obj, args);
long endTime = System.currentTimeMillis();
System.out.println(method.getName() + "运行时间"
+ (endTime - beginTime));
return ret;
}
}
Object proxy = Proxy.newProxyInstance(target.getClass()
.getClassLoader(), new Class[] { List.class },
new myInvocationHandler(target));
return proxy;
}
}
|