如何反射获取类的全部信息?(java代码)

什么是反射?

反射是 Java 提供的一种机制,允许在运行时动态地获取类的信息(如类的名称、方法、字段等),以及创建对象和调用方法。反射利用了 java.lang.reflect 包中的类,如 Class、Method、Field 和 Constructor。

本文用以下代码,通过反射机制实现对类结构的可视化。

当用户输入任意类名时,程序会打印出该类的构造函数、方法及字段的信息,没有找到目标类会输出ClassNotFoundException到控制台。

具体步骤:

1、输入类名:

传入我们想要去获取哪个类的信息

2、加载类:

使用 Class.forName(name) 动态加载指定的类,并获取 Class 对象。获取该类的父类 supercl,如果它的父类不是Object就给它标上继承自哪里。

Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers + " ");
System.out.print("class " + name);
if(supercl !=null && supercl != Object.class) System.out.print(" extends "+ supercl.getName());

3、打印类信息:

        (1.打印类的构造函数
	public static void printConstructors(Class cl){
		Constructor [] constructors = cl.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(");");
		}
	}
        (2.打印类的方法
public static void printMethods(Class cl){
		Method[] methods = cl.getDeclaredMethods();
		for (Method m : methods){
			Class retType = 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(retType. getName() + " "+ name + "(");

			Class [] paramTypes = m.getParameterTypes();
			for (int j = 0; j < paramTypes.length; j++){
			if (j > 0) System.out.print(", ");
				System.out.print(paramTypes[j].getName());
			}
			System.out.println(");");
		}
	}
        (3.打印类的所有字段
	public static void printFields(Class cl){
		Field[] fields = cl.getDeclaredFields();
		for (Field f : fields){
			Class type = f.getType();
			String name = f.getName();
			System.out.print("   ");
			String modifiers = Modifier.toString(f.getModifiers());
			if (modifiers.length() > 0) System.out.print(modifiers + " ");
			System.out.println(type.getName() + " " + name + ";");
		}
	}

4、异常处理:

如果输入的指定类找不到,会抛出ClassNotFoundException异常,捕获并打印出来,方便后续调试。

catch (ClassNotFoundException e)
	{
		//System.out.println("输入的类没有找到");
		e.printStackTrace();
	}

java代码:

public class ReflectionTest {
	public static void main(String[] args) {
		String name;
		if(args.length>0) name = args[0];
		else {
			Scanner in = new Scanner(System.in);
			System.out.println("请输入类名 (例如: java.util.Date");
			name = in.next();
		}
		
		try {
			Class cl = Class.forName(name);
			Class supercl = cl.getSuperclass();
			String modifiers = Modifier.toString(cl.getModifiers());
			if(modifiers.length()>0) System.out.print(modifiers + " ");
			System.out.print("class " + name);
			if(supercl !=null && supercl != Object.class) System.out.print(" extends "+ supercl.getName());
			System.out.println("\n{\n");
			printConstructors(cl);//打印构造函数
			System.out.println();
			printMethods(cl);//打印方法
			System.out.println();
			printFields(cl);//打印字段
			System.out.println("}");
		}
		catch (ClassNotFoundException e)
		{
			//System.out.println("输入的类不存在");
			e.printStackTrace();
		}
		System.exit(0);
	}
	public static void printConstructors(Class cl){
		Constructor [] constructors = cl.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 cl){
		Method[] methods = cl.getDeclaredMethods();
		for (Method m : methods){
			Class retType = 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(retType. getName() + " "+ name + "(");

			Class [] paramTypes = m.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 printFields(Class cl){
		Field[] fields = cl.getDeclaredFields();
		for (Field f : fields){
			Class type = f.getType();
			String name = f.getName();
			System.out.print("   ");
			String modifiers = Modifier.toString(f.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、付费专栏及课程。

余额充值