【BeanUtils】自己写的一个BeanUtils-代码方法详解(2)

简介: 【BeanUtils】自己写的一个BeanUtils-代码方法详解

最终版:

MyBeanUtils

package cn.hncu.beanUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
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 InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
        T obj = null;
        //1、用类反射new出对象
        obj = cls.newInstance();
        //2 再用类反射对新new的对象设置属性值(必须遵守Java设置规范)--即通过setter方法设置
        //2.1遍历出所有该类声明的属性
        Field flds[] = cls.getDeclaredFields();//getDeclaredFields()返回Class中所有的字段,包括私有字段;
        for(Field fld:flds){
            //获取该fld对象所代表的属性名
            String fldName = fld.getName();
            //根据属性名,到map中去读取数据,只有数据非空才需要给该属性设置值 
            Object value = map.get(fldName);
            if(value==null){//如果map中不存在对应的属性数据,我们在这里给出提示信息
                System.out.println(fldName+"的数据为空");
            }else{
                //如果map中存在对应的属性数据,则由属性名得出它的setter方法的名字
                String mothodName = "set"+fldName.substring(0, 1).toUpperCase()+fldName.substring(1);
                 //根据方法名和参数的数据类型(其实就是属性的类型),获得Method对象
                Class<?> paramTypes[] = new Class[1];
                paramTypes[0] = fld.getType();
                Method method = cls.getDeclaredMethod(mothodName, paramTypes);
                //调用该method对象所代表的方法
                Object args[] = new Object[1];
                args[0]=value;
                method.invoke(obj, args);
            }
        }
        return obj;
    }
}

测试方法:

@Test
    @SuppressWarnings("unchecked")
    public void test() {
        Map<String, Object> map = new HashMap();
        map.put("uuid", "001");
        map.put("name", "Jack");
        map.put("age", 20);
        Map<String, Object> map2 = new HashMap();
        map2.put("uuid", "001");
        map2.put("name", "红楼梦");
        map2.put("inPrice", 20.5);
        //数据可能不全
        map2.put("num", 123);
        try {
            User user = MyBeanUtils.populate(User.class, map);
            System.out.println(user);
            Book book = MyBeanUtils.populate(Book.class, map2);
            System.out.println(book);
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
        }
    }

测试结果:

image.png

完整项目源码链接:

–>点击访问本系列源码–

目录
相关文章
|
Java Spring
解决Spring工具类BeanUtils copyProperties方法复制null的问题
解决Spring工具类BeanUtils copyProperties方法复制null的问题
864 0
|
4月前
|
Java Apache
BeanUtils.copyProperties()用法总结
BeanUtils.copyProperties()用法总结
|
4月前
|
前端开发 Java 数据处理
BeanUtils.copyProperties的用法
BeanUtils.copyProperties的用法
|
3月前
|
Java Apache 开发者
介绍BeanUtils.copyProperties方法
介绍BeanUtils.copyProperties方法
|
4月前
|
Java API
beanutils.copyproperties
beanutils.copyproperties
|
Java Apache 数据库
Spring的BeanUtils的copyProperties方法
项目中遇到的情况是: 文件解析完之后将文件放在一个pojo里面
120 0
|
Java 测试技术 Spring
为什么不推荐使用 BeanUtils ?
为什么不推荐使用 BeanUtils ?
313 0
为什么不推荐使用 BeanUtils ?
BeanUtils.copyProperties 使用注意
BeanUtils.copyProperties 使用注意
408 0
BeanUtils.copyProperties 使用注意