45行返回值有问题了,需要返回值的。
我这里有自己理解的代码,我贴出来吧
- public class CreatProxy {
- public static void main(String[] args) throws Exception,
- NoSuchMethodException {
- Class clazzproxy = Proxy.getProxyClass(Collection.class
- .getClassLoader(), Collection.class);
- Constructor constructor = clazzproxy
- .getConstructor(InvocationHandler.class);
- class MyInvocationHandler implements InvocationHandler{
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- return null;
- }
- }
- Collection proxy1= (Collection) constructor.newInstance(new MyInvocationHandler());
-
- Collection proxy2= (Collection) constructor.newInstance(new InvocationHandler(){
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- 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 proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- List target =new ArrayList();
- long begintime =System.currentTimeMillis();
- Object retValue=method.invoke(target, args);
- long endtime =System.currentTimeMillis();
- System.out.println(method.getName()+" "+(endtime-begintime));
- return retValue;
- }
- });
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
- proxy3.add("dsfgsdf");
-
-
- }
- }
复制代码
|