首先,代理类并非ArrayList的子类,虽然他们同样都是list的子类,但他们是兄弟。。。
另外,代理的实现方式是转移调用,或者说是拦截模式,你调用的是代理类的方法,而非ArrayList的方法,这种调用的转移是通过你的invocationhandler转发的。。。
不多说了,直接给你看下代理类的源代码你就知道了,这是我自己实现的动态代理,用的是内存中的动态编译技术- public class Proxy$0 implements java.util.List
- {
- public question3.ArrayListProxy_4.ProxyHandler h;
- public boolean add(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("add",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public void add(int arg0,java.lang.Object arg1)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("add",int.class,java.lang.Object.class),arg0,arg1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return;
- }
- public java.lang.Object get(int arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("get",int.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.lang.Object)obj;
- }
- public boolean equals(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("equals",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public int hashCode()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("hashCode",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Integer)obj;
- }
- public int indexOf(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("indexOf",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Integer)obj;
- }
- public void clear()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("clear",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return;
- }
- public boolean contains(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("contains",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public boolean isEmpty()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("isEmpty",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public int lastIndexOf(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("lastIndexOf",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Integer)obj;
- }
- public boolean addAll(int arg0,java.util.Collection arg1)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("addAll",int.class,java.util.Collection.class),arg0,arg1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public boolean addAll(java.util.Collection arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("addAll",java.util.Collection.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public java.util.Iterator iterator()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("iterator",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.util.Iterator)obj;
- }
- public int size()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("size",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Integer)obj;
- }
- public java.lang.Object[] toArray(java.lang.Object[] arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("toArray",java.lang.Object[].class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.lang.Object[])obj;
- }
- public java.lang.Object[] toArray()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("toArray",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.lang.Object[])obj;
- }
- public boolean remove(java.lang.Object arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("remove",java.lang.Object.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public java.lang.Object remove(int arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("remove",int.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.lang.Object)obj;
- }
- public java.lang.Object set(int arg0,java.lang.Object arg1)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("set",int.class,java.lang.Object.class),arg0,arg1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.lang.Object)obj;
- }
- public java.util.ListIterator listIterator(int arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("listIterator",int.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.util.ListIterator)obj;
- }
- public java.util.ListIterator listIterator()
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("listIterator",new Class<?>[]{}),new Object[]{});
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.util.ListIterator)obj;
- }
- public java.util.List subList(int arg0,int arg1)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("subList",int.class,int.class),arg0,arg1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (java.util.List)obj;
- }
- public boolean containsAll(java.util.Collection arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("containsAll",java.util.Collection.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public boolean removeAll(java.util.Collection arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("removeAll",java.util.Collection.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- public boolean retainAll(java.util.Collection arg0)
- {
- Object obj = null;
- try
- {
- obj = h.invoke(java.util.List.class.getMethod("retainAll",java.util.Collection.class),arg0);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return (Boolean)obj;
- }
- }
复制代码 |