黑马程序员技术交流社区
标题:
代理问题
[打印本页]
作者:
杨曾荣
时间:
2012-2-27 10:46
标题:
代理问题
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Object> proxy = (List<Object>) getProxy(new ArrayList<Object>());
proxy.add("helen");
proxy.add("never");
proxy.add("for");
proxy.remove(1);
System.out.println(proxy);
}
运行到这句List<Object> proxy = (List<Object>) getProxy(new ArrayList<Object>());
就出错了;错误代码如下:Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to java.util.List at cn.heima.test.proxy.ProxyTest.main(ProxyTest.java:12)
getProxy方法
private static Object getProxy(final Object target){
Object proxy=Proxy.newProxyInstance(
target.getClass().getClassLoader()
, target.getClass().getDeclaredClasses()
, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
long beginTime=System.currentTimeMillis();
Object retValue=method.invoke(target, args);
long endTime=System.currentTimeMillis();
System.out.println(method.getName()+"运行时间"+(endTime-beginTime));
return retValue;
}
});
return proxy;
}
}
作者:
tanlei200620
时间:
2012-2-27 15:40
本以为等我吃完饭就没有我的分了 ,看来还是我回答,,呵呵代码如下:
修改后:
package hh;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
//if(100<sum&&sum<=999&&sum/100%2==1&&sum/10%2==1&&sum%2==1
class Hello{
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Object> proxy1 =new ArrayList<Object>();
Collection proxy = (Collection) 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[] {Collection.class}
, new myInvocationHandler(target));
return proxy;
}
}
作者:
tanlei200620
时间:
2012-2-27 15:44
忘了,Collection 替换为 List
作者:
tanlei200620
时间:
2012-2-27 15:48
太激动了,还是再发一遍吧
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;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2