Proxy基础---------获取collection接口的构造跟方法
1----查看proxy api
2------测试代码
1 package cn.proxy01; 2 import java.lang.reflect.Constructor; 3 import java.lang.reflect.Method; 4 import java.util.Collection; 5 public class Proxy { 6 public static void main(String[] args) { 7 Class clazzProxy = java.lang.reflect.Proxy.getProxyClass(Proxy.class.getClassLoader(), Collection.class); 8 System.err.println("代理名称:"+clazzProxy.getName()); 9 System.err.println("--------通过代理获取collection的构造 ---------------"); 10 //$Proxy0() 11 //$Proxy(参数名,参数类型) 12 Constructor[] constructors = clazzProxy.getConstructors(); 13 for(Constructor constructor : constructors){ 14 String constructionName = constructor.getName(); 15 StringBuilder builder = new StringBuilder(constructionName); 16 builder.append("("); 17 Class[] parameters = constructor.getParameterTypes(); 18 for(Class params : parameters){ 19 String param = params.getName(); 20 builder.append(param+","); 21 } 22 if(parameters.length != 0 || parameters != null){ 23 builder.deleteCharAt(builder.length()-1); 24 } 25 builder.append(")"); 26 System.err.println(builder.toString()); 27 } 28 System.err.println("--------通过代理获取collection的方法 ---------------"); 29 //remove(java.lang.Object) 30 Method[] methods = clazzProxy.getMethods(); 31 for(Method method : methods){ 32 String methodName = method.getName(); 33 StringBuilder builder = new StringBuilder(methodName); 34 builder.append("("); 35 Class[] parameters = method.getParameterTypes(); 36 for(Class params : parameters){ 37 String param = params.getName(); 38 builder.append(param+","); 39 } 40 if(parameters.length != 0 || parameters != null){ 41 builder.deleteCharAt(builder.length()-1); 42 } 43 builder.append(")"); 44 System.err.println(builder.toString()); 45 } 46 } 47 }
3----运行结果