java如何使用反射来获取某一个类的全部信息

反射是Java的一大特性,可以通过反射来获取某一个类的全部域、构造方法和成员方法。


原理介绍

  • 在java.lang.reflect包中有三个类Field、Method、Constructor分别用于描述类的域、方法和构造器。
  • 这三个类都有一个叫做getName的方法,用来返回项目的名称
  • Field类有一个getType方法,用来返回描述域所属的Class对象
  • Method和Constructor类有一个getParameterTypes方法,返回参数类型的Class对象数组
  • 这三个类都有一个getModifiers方法,它返回一个整型数值,用不同的位开关描述public和static这样的修饰符使用情况,并用Modifiers类中toString方法将修饰符打印出来
  • Class类中的getFields、getMethods、getConstructors方法将分别返回类提供的public域、方法和构造方法数组,其中包括父类的公有成员
  • Class类的getDeclareFields、getDeclareMethods和getDeclaredConstructors方法将返回类中生命的全部域、方法和构造方法数组,其中包括私有成员和受保护成员,但不包括父类的成员

代码示例

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 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.print("Enter Class name (e.g 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.print("\n{\n");
		
			printFields(cl);
			System.out.println();
			printConstructors(cl);
			System.out.println();
			printMethods(cl);
			System.out.print("}\n");
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public static void printConstructors(Class cl) {
		Constructor[] constructors = cl.getConstructors();
		for (Constructor c:constructors) {
			System.out.print("  ");
			String name = c.getName();
			String modifiers = Modifier.toString(c.getModifiers());
			if (modifiers.length() > 0 )System.out.print(modifiers + " ");
			System.out.print(name+" (");
			Class[] paramType = c.getParameterTypes();
			for (int j = 0; j < paramType.length; j++) {
				if (j > 0) System.out.print(", ");
				System.out.print(paramType[j].getName());
			}
			System.out.print(")");
		}
		System.out.println();
	}
	public static void printMethods(Class cl) {
		Method[] methods = cl.getDeclaredMethods(); 
		for (Method m:methods) {
			System.out.print("  ");
			String name = m.getName();
			String modifiers = Modifier.toString(m.getModifiers());
			if(modifiers.length()>0) System.out.print(modifiers+" ");
			Class returnType =m.getReturnType();
			System.out.print(returnType.getName()+" ");
			System.out.print(name+" (");
			Class[] paramType = m.getParameterTypes();
			for (int j = 0; j < paramType.length; j++) {
				if (j > 0) System.out.print(", ");
				System.out.print(paramType[j].getName());
			}
			System.out.print(")");
			System.out.println();
		}
		System.out.println();
	}
	public static void printFields(Class cl) {
		Field[] fields = cl.getDeclaredFields();
		for (Field f:fields) {
			System.out.print("  ");
			String name = f.getName();
			Class type = f.getType();
			String modifiers = Modifier.toString(f.getModifiers());
			if(modifiers.length()>0) System.out.print(modifiers+" ");
			System.out.println(type.getName()+" "+name+";");
		}
	}
}

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大格橘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值