怎么利用java放射机制进行对象的实例化等操作

大家看demo就明白了:

User.java:

package test;

/**
 * @编写人: yh.zeng
 * @编写时间:2017-7-10 下午9:26:18
 * @文件描述: todo
 */
public class User {

	private String userName; // 用户名
	public Integer age; // 年龄

	private String getUserName() {
		return userName;
	}

	private void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

Test.java:

package test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @编写人: yh.zeng
 * @编写时间:2017-7-11 下午1:08:05
 * @文件描述: todo
 */
public class Test {
	
	
	/**
	 * 获取类定义的非public字段(如private、protected等)
     * @param targetClass 目标类
	 * @param fieldName   字段名
	 * @return
	 * @throws Exception
	 */
	public static Field getPrivateField(Class targetClass, String fieldName) throws Exception{
		Field field = targetClass.getDeclaredField(fieldName);
		field.setAccessible(true); //不检查该字段的访问权限,否则使用Field.get()(即获取不到该字段数据的时候),可能会报错
		return field;
	}
	
	/***
	 * 获取类定义的pulic字段
	 * @param targetClass  目标类
	 * @param fieldName    字段名
	 * @return
	 * @throws Exception
	 */
	public static Field getPublicField(Class targetClass, String fieldName) throws Exception{
		return targetClass.getField(fieldName);
	}
	
	/**
	 * 获取类定义的非public方法(如private、protected等)
	 * @param targetClass     目标类
	 * @param methodName      方法名
	 * @param parameterTypes  方法参数类型
	 * @return
	 * @throws Exception
	 */
	public static Method getPrivateMethod(Class targetClass, String methodName, Class<?>... parameterTypes) throws Exception{
		Method method = targetClass.getDeclaredMethod(methodName, parameterTypes);
		method.setAccessible(true); //不检查该方法的访问权限,否则使用Method.invoke()(即调用实例对象的该方法的时候),可能会报错
		return method;
	}
	
	/**
	 * 获取类定义的public方法
	 * @param targetClass     目标类
	 * @param methodName      方法名
	 * @param parameterTypes  方法参数类型
	 * @return
	 * @throws Exception
	 */
	public static Method getPublicMethod(Class targetClass, String methodName, Class<?>... parameterTypes) throws Exception{
		return targetClass.getMethod(methodName, parameterTypes);
	}
	
	/**
	 * 获取类定义个非public构造方法,如(如private、protected等)
	 * @param targetClass     目标类
	 * @param parameterTypes  构造方法参数类型
	 * @return
	 * @throws Exception
	 */
	public static Constructor getPrivateConstructor(Class targetClass, Class<?>... parameterTypes) throws Exception{
		Constructor constructor = targetClass.getDeclaredConstructor(parameterTypes);
		constructor.setAccessible(true); //不检查该构造方法的访问权限,否则使用Constructor.newInstance()(即实例化对象的时候),可能会报错
	    return constructor;
	}
	
	/**
	 * 获取类定义个public构造方法
	 * @param targetClass     目标类
	 * @param parameterTypes  构造方法参数类型
	 * @return
	 * @throws Exception
	 */
	public static Constructor getPublicConstructor(Class targetClass, Class<?>... parameterTypes) throws Exception{
		return targetClass.getConstructor(parameterTypes);
	}
	
	public static void main(String args[]) throws Exception{
		Class clazz = Class.forName("test.User");
		Object instance = getPublicConstructor(clazz).newInstance();
		//获取字段值
		Field userNameField = getPrivateField(clazz, "userName");
		System.out.println("userName = " + userNameField.get(instance) );
		Field ageField = getPrivateField(clazz, "age");
		System.out.println("age = " + ageField.get(instance) );
		//调用相应方法设值
		Method setUserNameMehod = getPrivateMethod(clazz, "setUserName", String.class);
		setUserNameMehod.invoke(instance, "aa");
		Method setAge = getPrivateMethod(clazz, "setAge", Integer.class);
		setAge.invoke(instance, 18);
		//再次获取字段值
		System.out.println("userName = " + userNameField.get(instance) );
		System.out.println("age = " + ageField.get(instance) );
	}
}

Test.java程序运行结果如下:

userName = null
age = null
userName = aa
age = 18

备注:

  • 获取非public字段,就一定要使用Class.getDeclaredField来获取
  • 获取public字段,即可以使用Class.getField,也可以使用Class.getDeclaredField,所以推荐字段的获取一律使用Class.getDeclaredField
  • 获取非public方法,就一定要使用Class.getDeclaredMethod来获取
  • 获取public方法,即可以使用Class.getMethod,也可以使用Class.getDeclaredMethod,所以推荐方法的获取一律使用Class.getDeclaredMethod
  • 获取非public构造方法,就一定要使用Class.getDeclaredConstructor来获取
  • 获取public构造方法,即可以使用Class.getConstructor,也可以使用Class.getDeclaredConstructor,所以推荐方法的获取一律使用Class.getDeclaredConstructor
  • 字段、方法以及构造方法,一定要设置setAccessible(true),否则可能会报无权限访问的错误


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值