利用发反射对对象进行赋值

public class Demo1 {


    public static Object getObejctParamater(String className,HashMap<String,String> map) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException, ParseException {
        Class<?> myclass= Class.forName(className);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Object object = myclass.newInstance();
        for(Map.Entry<String,String> entry:map.entrySet()){
            Field field = myclass.getDeclaredField(entry.getKey());
            field.setAccessible(true);

            if("Boolean".equals(field.getType().getSimpleName())){
                field.set(object,entry.getValue().equals("true"));
            }else if("BigDecimal".equals(field.getType().getSimpleName())){
                field.set(object, new BigDecimal(entry.getValue()));
            }else if("Date".equals(field.getType().getSimpleName())){
                field.set(object, simpleDateFormat.parse(entry.getValue()));
            }else if("Timestamp".equals(field.getType().getSimpleName())){
                field.set(object, Timestamp.valueOf(entry.getValue()));
            }else if(field.getType().isEnum()){
                field.set(object,Enum.valueOf((Class<Enum>) field.getType(), entry.getValue()));
            }else if("int".equals(field.getType().getSimpleName())||"Integer".equals(field.getType().getSimpleName())){
                field.set(object, Integer.parseInt(entry.getValue()));
            }else if ("String".equals(field.getType().getSimpleName())){
                field.set(object,entry.getValue());
            }
//            System.out.println(field.get(object));

        }
        return object;
    }

    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException, ParseException {

    /*    try {
            Class<?> myclass= Class.forName("com.lu.entities.Group");

            Method method=myclass.getDeclaredMethod("setGroup_name", String.class);
            method.invoke(myclass.newInstance(),"testname");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/


        HashMap<String, String> map = new HashMap<String, String>();
        map.put("group_name", "test_groupname");
        map.put("group_desc", "test_group_desc");
        map.put("id", "test_id");
        map.put("test_field", "mytest_field");
        map.put("flag", "false");
        map.put("price", "3000");
        map.put("brithday", "1990-12-11 15:00:00");
        map.put("updated_at", "1990-12-11 15:00:00");
        map.put("colorEnum", "BLACK");
        map.put("age", "11");
//        map.put("count", "9");


        Object group = getObejctParamater("com.my.practise.reflection.Student", map);
        System.out.println(group.toString());








    /*    Class c = Class.forName("com.my.practise.reflection.Student");
        Field[] fs = c.getDeclaredFields();

        //定义可变长的字符串,用来存储属性
        StringBuffer sb = new StringBuffer();
        //通过追加的方法,将每个属性拼接到此字符串中
        //最外边的public定义
        sb.append(Modifier.toString(c.getModifiers()) + " class " + c.getSimpleName() +"{\n");
        //里边的每一个属性
        for(Field field:fs){
            sb.append("\t");//空格
            sb.append(Modifier.toString(field.getModifiers())+" ");//获得属性的修饰符,例如public,static等等
            sb.append(field.getType() + " ");//属性的类型的名字
            sb.append(field.getName()+";\n");//属性的名字+回车
        }

        sb.append("}");

        System.out.println(sb);*/


    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值