- public class ProxyTest {
- [code]
- <P>public class ProxyTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //创建一个动态类代理获得其字节码并实例化返回一个字节码类对象.jvm虚拟机要动态生成API必须要提供接口和类加载器两个参数信息
- Class clazzProxy1=Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
- System.out.print(clazzProxy1.getName());
- /*$$$$proxy0()
- $$$$proxy0(invocationHandler,int i)*/
- System.out.println("----------begin constructors list----------");
- Constructor[] constructors=clazzProxy1.getConstructors();
- for(Constructor constructor : constructors)//返回Proxy对象的构造方法并循环
- {
- 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());//打印出proxy对象所映射的所有带参数的构造方法列表
- }
- //获取返回实现Proxy类的接口和父类的所有方法列表!
- System.out.println("----------begin methods list----------");
- Method[] methods=clazzProxy1.getMethods();
- for(Method method : methods)
- {
- String name=method.getName();
- StringBuilder sBuilder=new StringBuilder(name);
- sBuilder.append('(');
- Class[] clazzParams=method.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());
- }
- }
- }</P>
- <P>输出结果:</P>
- <P>$$$$Proxy0----------begin constructors list----------
- $$$$Proxy0(java.lang.reflect.InvocationHandler)
- ----------begin methods list----------
- hashCode()
- add(java.lang.Object)
- clear()
- equals(java.lang.Object)
- toString()
- contains(java.lang.Object)
- addAll(java.util.Collection)
- iterator()
- size()
- toArray()
- toArray([Ljava.lang.Object;)
- remove(java.lang.Object)
- isEmpty()
- containsAll(java.util.Collection)
- removeAll(java.util.Collection)
- retainAll(java.util.Collection)
- isProxyClass(java.lang.Class)
- getProxyClass(java.lang.ClassLoader,[Ljava.lang.Class;)
- getInvocationHandler(java.lang.Object)
- newProxyInstance(java.lang.ClassLoader,[Ljava.lang.Class;,java.lang.reflect.InvocationHandler)
- getClass()
- wait()
- wait(long,int)
- wait(long)
- notify()
- notifyAll()
- </P>
复制代码 |
|