- package cn.itcast.day3;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.util.Collection;
- public class ProxyTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- /**
- * 创建动态类及查看其方法列表信息,打印格式:
- * Constructor()
- * Constructor(InvocationHandler,int)
- */
- Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
- System.out.println(clazzProxy.getName());
-
- System.out.println("-----------starting constructors list-------------------");
- Constructor[] constructors = clazzProxy.getConstructors();
- for(Constructor constructor : constructors){
- String name = constructor.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] classParams = constructor.getParameterTypes();
- for(Class classParam : classParams){
- sBuilder.append(classParam.getName()).append(',');
- }
- if(classParams!=null && classParams.length!=0){
- sBuilder.deleteCharAt(sBuilder.length()-1);
- }
- sBuilder.append(')');
- System.out.println(sBuilder);
- }
-
-
- System.out.println("-----------starting methods list-------------------");
- Method[] methods = clazzProxy.getMethods();
- for(Method method : methods){
- String name = method.getName();
- StringBuilder sBuilder = new StringBuilder(name);
- sBuilder.append('(');
- Class[] classParams = method.getParameterTypes();
- for(Class classParam : classParams){
- sBuilder.append(classParam.getName()).append(',');
- }
- if(classParams!=null && classParams.length!=0){
- sBuilder.deleteCharAt(sBuilder.length()-1);
- }
- sBuilder.append(')');
- System.out.println(sBuilder);
- }
- }
- }
复制代码 |