java--模拟BeanUtils.populate()功能
来源:互联网 发布:剑三成女女神捏脸数据 编辑:程序博客网 时间:2024/06/10 20:25
模拟Java内省的功能
准备工作
定义一个Model类,里面所有的属性都是private的,然后为每个属性提供getter和setter方法;
再准备一个Map,map的key值都是类里面的属性字段的字符串表示,值任意。
真正的工作
设计一个方法Object getModel(Map map,Class cls),传入一个包含所有值的Map,然后再传入Model类的class,那么返回Model类的实例,这个实例里面已经包含好了所有相关的数据。也就是把Map中的数据通过反射,设置回到Model类实例中。
public class UserModel { private String uuid,name,pwd;//用户编号,用户名称,用户密码 private int age;//用户类型 public UserModel() { } 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 String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserModel other = (UserModel) obj; if (uuid == null) { if (other.uuid != null) return false; } else if (!uuid.equals(other.uuid)) return false; return true; } @Override public String toString() { return uuid + "," + name +",age=" + age+","+pwd; }}
public class BookModel{ private String uuid,name; private double inPrice,salePrice; public BookModel(){ } 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 double getInPrice() { return inPrice; } public void setInPrice(double inPrice) { this.inPrice = inPrice; } public double getSalePrice() { return salePrice; } public void setSalePrice(double salePrice) { this.salePrice = salePrice; } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(inPrice); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BookModel other = (BookModel) obj; if (Double.doubleToLongBits(inPrice) != Double .doubleToLongBits(other.inPrice)) return false; return true; } @Override public String toString() { return uuid + ",《" + name + "》,进价:"+ inPrice + ",售价:" + salePrice; }}
import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.Map;public class myBeanUtils { public static<T> T populate(Class<T> cls, Map<String, Object> map) throws Exception { Object obj=cls.newInstance(); Field flds[]=cls.getDeclaredFields(); Object v=null; for(Field fld:flds){ v=map.get(fld.getName()); if(v==null){ System.out.println(fld.getName()+"值为空..."); continue; } String methodName="set"+fld.getName().substring(0, 1).toUpperCase()+fld.getName().substring(1); Method m=cls.getMethod(methodName, fld.getType()); m.invoke(obj, v); } return (T) obj; }}
import java.util.HashMap;import java.util.Map;public class client { public static void main(String[] args) { Map<String, Object> map=new HashMap<String, Object>(); map.put("name", "Jack"); map.put("uuid", "001"); map.put("age", 12);// map.put("pwd", "1234"); try { UserModel um = myBeanUtils.populate(UserModel.class,map);//um:001,Jack,age=12,null System.out.println("um:"+um); System.out.println("------------------------"); BookModel bm = myBeanUtils.populate(BookModel.class,map);//bm:001,《Jack》,进价:0.0,售价:0.0 System.out.println("bm:"+bm); } catch (Exception e) { e.printStackTrace(); } }}
0 0
- java--模拟BeanUtils.populate()功能
- BeanUtils.populate
- BeanUtils.populate
- BeanUtils.populate
- Beanutils.populate
- BeanUtils.populate的作用
- 错误信息BeanUtils.populate
- BeanUtils.populate(object1,object2);
- BeanUtils.populate的作用
- BeanUtils.populate的用法
- Struts1 BeanUtils.populate
- BeanUtils.populate的作用
- BeanUtils.populate的作用
- BeanUtils.populate的作用 !
- beanutils.populate用法
- BeanUtils.populate的作用
- BeanUtils.populate的作用
- BeanUtils.populate的用法
- Java容器类的深入理解
- Linux 修改主机名 和 ip 映射关系
- java解析json字符串
- Java虚拟机(五)——垃圾收集机制
- android之Fragment简介和使用实例
- java--模拟BeanUtils.populate()功能
- JAVA容器
- java中通过放射机制创建对象的两种方式
- RecyclerView详解之基础使用
- 关于meteor的几个槽点
- 操作系统基础
- git和github使用整理
- Linux开机自动获取IP
- TJU 1036 模拟栈