本帖最后由 杨增坤 于 2013-9-22 21:33 编辑
- package cn.os.du;
- import java.lang.reflect.*;
- import java.util.ArrayList;
- import java.util.Collection;
- public class ProxyTest {
- public static void main(String[] args) throws Exception
- {
- Class clazzProxy1=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
- System.out.println(clazzProxy1.getName());
- System.out.println("---------------begin constructors list------------");
- Constructor[] constructors=clazzProxy1.getConstructors();
- for(Constructor constructor:constructors)
- {
- String name=constructor.getName();
- StringBuilder sBuilder=new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams=constructor.getParameterTypes();
- for(Class clazzParam:clazzParams)
- {
- sBuilder.append(clazzParam.getName()).append(',');
-
- }
- if(clazzParams!=null && clazzParams.length!=0)
- sBuilder.deleteCharAt(sBuilder.length()-1);
- sBuilder.append(')');
- System.out.println(sBuilder.toString());
- }
- System.out.println("---------------begin methods list------------");
-
- Method[] methods=clazzProxy1.getMethods();
- for(Method method:methods)
- {
- String name1=method.getName();
- StringBuilder sBuilder1=new StringBuilder(name1);
- sBuilder1.append('(');
- Class[] clazzParams1=method.getParameterTypes();
- for(Class clazzParam:clazzParams1)
- {
- sBuilder1.append(clazzParam.getName()).append(',');
-
- }
- if(clazzParams1!=null && clazzParams1.length!=0)
- sBuilder1.deleteCharAt(sBuilder1.length()-1);
- sBuilder1.append(')');
- System.out.println(sBuilder1.toString());
-
- }
- System.out.println("---------------begin create instance object------------");
- Constructor constructor=clazzProxy1.getConstructor(InvocationHandler.class);
- class MyInvoctionHander1 implements InvocationHandler
- {
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- return null;
- }
- }
- Collection proxy1=(Collection)constructor.newInstance(new MyInvoctionHander1());
- System.out.println(proxy1);
- proxy1.clear();
- //proxy1.size();
-
- Collection proxy2=(Collection)constructor.newInstance(new InvocationHandler() {
-
- @Override
- public Object invoke(Object arg0, Method arg1, Object[] arg2)
- throws Throwable {
- // TODO Auto-generated method stub
- return null;
- }
- });
- //第三个代理对象
- Collection proxy3=(Collection)Proxy.newProxyInstance(Collection.class.getClassLoader(),
- new Class[]{Collection.class},
- new InvocationHandler() {
-
- @Override
- public Object invoke(Object arg0, Method arg1, Object[] arg2)
- throws Throwable {
- ArrayList target=new ArrayList();
- long beginTime=System.currentTimeMillis();
- Object retVal=method.invoke(target,args);//这一行什么意思,不太懂
- long endTime=System.currentTimeMillis();
- System.out.println(method.getName()+"run time of"+(endTime-beginTime));
- return retVal;
- }
- });
- proxy3.add("xxxx");
- proxy3.add("sdf");
- proxy3.add("sffgf");
- System.out.println(proxy3.size());
- }
- }
-
复制代码 第84,86这两行怎么老有错,还有84行什么意思呢
|