使用反射打印类信息


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
/**
 * 使用反射打印类信息
 */
public class PrintClassByReflection {
 public static void main(String[] args) {
  // 从命令行参数读取一个类名或用户输入
  String className;
  if (args.length > 0) {
   className = args[0];
  } else {
   System.out.println("请输入类名 如:java.lang.Class");
   Scanner in = new Scanner(System.in);
   className = in.next();
  }
  try {
   Class clazz = Class.forName(className);
   Class superClass = clazz.getSuperclass();
   Class[] interfaces= clazz.getInterfaces();
   String modifiers = Modifier.toString(clazz.getModifiers());
   if (modifiers.length() > 0) {
    System.out.print(modifiers + " ");
   }
   System.out.print("Class " + className);
   //打印继承的父类
   if (superClass != null && superClass != Object.class) {
    System.out.print(" extends " + superClass.getName());
   }
   //打印实现的接口
   if(interfaces.length > 0)
    System.out.print(" implements ");
   for(Class cc : interfaces){
    System.out.print(cc.getName() + " ");
   }
   
   System.out.print("{\n");
   printConstructors(clazz);
   System.out.println();
   printMethods(clazz);
   System.out.println();
   printFields(clazz);
   System.out.println("}");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  System.exit(0);
 }
 /**
  * 打印类的所有构造函数
  */
 public static void printConstructors(Class clazz) {
  Constructor[] constructors = clazz.getDeclaredConstructors();
  for (Constructor c : constructors) {
   String name = c.getName();
   System.out.print("   ");
   String modifiers = Modifier.toString(c.getModifiers());
   if (modifiers.length() > 0) {
    System.out.print(modifiers + " ");
   }
   System.out.print(name + "(");
   // 打印参数列表
   Class[] paramTypes = c.getParameterTypes();
   for (int j = 0; j < paramTypes.length; j++) {
    if (j > 0) {
     System.out.print(", ");
    }
    System.out.print(paramTypes[j].getName());
   }
   System.out.println(");");
  }
 }
 /**
  * 打印所有方法
  */
 public static void printMethods(Class clazz) {
  Method[] methods = clazz.getDeclaredMethods();
  for (Method m : methods) {
   Class returnType = m.getReturnType();
   String name = m.getName();
   System.out.print("   ");
   String modifiers = Modifier.toString(m.getModifiers());
   if (modifiers.length() > 0) {
    System.out.print(modifiers + " ");
   }
   System.out.print(returnType.getName() + " " + name + "(");
   // 打印参数列表
   Class[] paramTypes = m.getParameterTypes();
   for (int i = 0; i < paramTypes.length; i++) {
    if (i > 0) {
     System.out.print(", ");
    }
    System.out.print(paramTypes[i].getName());
   }
   System.out.println("){");
   System.out.println("\t//方法体省略·····(没办法直接获得方法体,通过第三方反编译或许可以。)");
   System.out.println("   }");
  }
 }
 /**
  * 打印所有属性
  */
 public static void printFields(Class clazz) {
  Field[] fields = clazz.getDeclaredFields();
  for (Field ff : fields) {
   //字段类型
   Class type = ff.getType();
   //字段名称
   String name = ff.getName();
   System.out.print("   ");
   //字段访问级别 如:private、public等
   String modifiers = Modifier.toString(ff.getModifiers());
   if (modifiers.length() > 0) {
    System.out.print(modifiers + " ");
   }
   System.out.println(type.getName() + " " + name + ";");
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值