练习(模拟Java内省的功能)

★ 准备工作

定义一个Model类,里面所有的属性都是private的,然后为每个属性提供getter和setter方法;
再准备一个Map,map的key值都是类里面的属性字段的字符串表示,值任意。 

★ 真正的工作 

设计一个方法Object getModel(Map map,Class cls),传入一个包含所有值的Map,然后再传入Model类的class,

那么返回Model类的实例,这个实例里面已经包含好了所有相关的数据。

也就是把Map中的数据通过反射,设置回到Model类实例中。 

1)核心代码---模仿spring

package reflect2.spring;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class SpringDaoUtil {
	/**
	 * 给定Map<K, V> map与Class<C> cls,
	 * 返回一个值对象。
	 * 目的:模仿spring的工作原理
	 */
	public static <V, C> C getModel(Map<String, V> map, Class<C> cls)
			throws InstantiationException, IllegalAccessException,
			SecurityException, NoSuchMethodException, IllegalArgumentException,
			InvocationTargetException {
		C instan = cls.newInstance();
		Field[] flds = cls.getDeclaredFields();
		for (Field f : flds) {
			String key = f.getName();
			V v = map.get(key);
			if (v == null) {
				System.out.println("字段 " + key + "  为空!");
			} else {
				// 定义形参
				String methodName = "set" + key.substring(0, 1).toUpperCase()
						+ key.substring(1);
				@SuppressWarnings("rawtypes")
				Class[] parameterTypes = new Class[] { f.getType() };
				Method m = cls.getDeclaredMethod(methodName, parameterTypes);
				// 传递实参
				Object[] objs = new Object[] { v };
				m.invoke(instan, objs);
			}
		}
		return instan;
	}
}


2)测试代码


测试图片

package reflect2.spring;

import java.util.HashMap;
import java.util.Map;

import reflect2.spring.vo.Person;
import reflect2.spring.vo.UserModel;

public class Client {
	public static void main(String[] args) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("uuid", "001");
			map.put("name", "Lucy");
			map.put("type", Integer.valueOf(3));
			map.put("pwd", "222");
			UserModel user = SpringDaoUtil.getModel(map, UserModel.class);
			System.out.println(user);
			System.out.println("-------测试事件分割线----------");

			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("name", "Lily");
			Person p = SpringDaoUtil.getModel(map2, Person.class);
			System.out.println(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



3)测试数据(值对象)-----JavaBean

package reflect2.spring.vo;

public class Person {
	private String name;
	public int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public Person(){
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

/**
 * 
 */
package reflect2.spring.vo;

public class UserModel{
	private String uuid;
	private String name;
	private int type;
	private String pwd;
	
	public UserModel(String uuid, String name, int type, String pwd) {
		this.uuid = uuid;
		this.name = name;
		this.type = type;
		this.pwd = pwd;
	}

	public UserModel(String uuid, int type){
		this.uuid = uuid;
		this.type = type;
	}
	public UserModel(){
	}
	private UserModel(String uuid){
		this.uuid = uuid;
	}
	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String toString() {
		return "{"+uuid+","+name+","+type+","+pwd+"}";
	}
	
}











------


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值