Java反射机制

一、什么是反射
在运行状态中,对于任意一个类,都能够获取到这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性(包括私有的方法和属性),这种动态获取的信息以及动态调用对象的方法的功能就称为java语言的反射机制
简单讲,就是通过反射,该类对我们来说是完全透明的,想要获取任何东西都可以(通过类名或实例对象,在源文件或.class文件都可以)。

二、获取该类及属性、方法并使用

public class Main {
	public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
		//三种获取当前类的字节码文件对象方法(视不同情况使用不同的方法获取Class)
		getClass1();
		getClass2();
		getClass3(new User());
		//获取所有构造函数
		getConstructors();
		//获取成员变量并使用
		getField1();
		//获取所有成员变量
		getFields();
		//获取指定方法并使用
		getMethod1();
		//获取所有方法
		getMethods();
	}


	/**
	 * 通过全限定类名获取字节码文件对象,创建实例
	 * 通过Class类中的静态方法forName,直接获取到一个类的字节码文件对象,此时该类还是源文件阶段,并没有变为字节码文件。
	 *
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static void getClass1() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
		Class class1 = Class.forName("com.example.demo.User");
		User user1 = (User) class1.newInstance();
		user1.setName("测试名字");
		System.out.println(user1.getName());
	}

	/**
	 * 通过类名获取字节码文件对象,获取无参构造函数创建对象
	 * 当类被加载成.class文件时,此时User类变成了.class,再获取该字节码文件对象,也就是获取自己,该类处于字节码阶段。
	 *
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws InstantiationException
	 */
	public static void getClass2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class class2 = User.class;
		Constructor constructor = class2.getConstructor();
		User u = (User) constructor.newInstance();
		u.setName("我是xxx");
		System.out.println(u.getName());
	}

	/**
	 * 通过类的实例获取该类的字节码文件对象,获取有参构造函数创建对象
	 * 该类处于创建对象阶段
	 *
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws InstantiationException
	 */
	public static void getClass3(User user) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class class2 = user.getClass();
		Constructor constructor = class2.getConstructor(String.class);
		User u = (User) constructor.newInstance("cxn");
		System.out.println(u.getName());
	}


	/**
	 * 获取所有的构造参数
	 *
	 * @throws ClassNotFoundException
	 */
	public static void getConstructors() throws ClassNotFoundException {
		Class class1 = Class.forName("com.example.demo.User");
		Constructor[] constructors = class1.getConstructors();
		for (Constructor constructor : constructors) {
			System.out.println("构造器名字" + constructor.getName());
			Class[] parameterTypes = constructor.getParameterTypes();
			for (Class parameterType : parameterTypes) {
				System.out.println("参数类型" + parameterType.getName());
			}
		}
	}

	/**
	 * 获取成员变量并使用
	 * getDeclaredField(name)获取私有成员变量  getField(name)获取成员变量
	 * setAccessible要对私有成员变量操作 需要打开其操作权限
	 * @throws ClassNotFoundException
	 * @throws NoSuchFieldException
	 */
	public static void getField1() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
		Class class1 = Class.forName("com.example.demo.User");
		User user = (User) class1.newInstance();
		Field field = class1.getDeclaredField("name");
		field.setAccessible(true);
		field.set(user,"测试成员赋值");
		System.out.println(field.get(user));
	}


	/**
	 * 获取所有成员变量
	 * @throws ClassNotFoundException
	 */
	public static void getFields() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
		Class class1 = Class.forName("com.example.demo.User");
		User user = (User) class1.newInstance();
		user.setName("我叫cxn");
		Field[] fields = class1.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			System.out.println("成员变量名称"+field.get(user));
		}
	}

	/**
	 * 获取public无参方法/private带参方法 并使用
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 */
	public static void getMethod1() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
		Class class1 = Class.forName("com.example.demo.User");
		User user = (User) class1.newInstance();
		Method method = class1.getMethod("eat");
		method.invoke(user);

		Method method2 = class1.getDeclaredMethod("sleep",String.class);
		method2.setAccessible(true);
		method2.invoke(user,"小红");

	}

	/**
	 * 获取所有方法
	 */
	public static void getMethods() throws ClassNotFoundException {
		Class class1 = Class.forName("com.example.demo.User");
		Method[] methods = class1.getDeclaredMethods();
		for (Method method : methods) {
			method.setAccessible(true);
			System.out.println("方法名"+method.getName());
			Class[] parameterTypes = method.getParameterTypes();
			for (Class parameterType : parameterTypes) {
				System.out.println("参数类型"+parameterType.getName());
			}
		}
	}
}

三、反射的应用举栗
利用反射,在泛型为int的arryaList集合中存放一个String类型的对象

//原理:集合中的泛型只在编译器有效,而到了运行期,泛型则会失效

	public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
		List<Integer> list = new ArrayList<>();
		list.add(1);
		list.add(2);
//		list.add("QQQ");   在编译器中,泛型生效,插入字符串会报错

		Class class1 = list.getClass();
		Method method = class1.getMethod("add",Object.class);
		method.invoke(list,"qqq");

		System.out.println(list);

	}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值