集册 Java实例教程 设置Java Bean对象的字段值

设置Java Bean对象的字段值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

511
设置Java Bean对象的字段值
/* from n o w    j a v a  . c o m*/

//package com.nowjava;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.text.SimpleDateFormat;

import java.util.Date;


import java.util.Locale;

import java.util.Map;


public class Main {


    public static void setFieldValue(Object bean, Map<String, String> valMap) {

        Class<?> cls = bean.getClass();


        Method[] methods = cls.getDeclaredMethods();

        Field[] fields = cls.getDeclaredFields();


        for (Field field : fields) {

            try {
//nowjava.com - 时代Java 提 供

                String fieldSetName = parSetName(field.getName());

                if (!checkSetMet(methods, fieldSetName)) {

                    continue;

                }

                Method fieldSetMet = cls.getMethod(fieldSetName,

                        field.getType());

                String value = valMap.get(field.getName());

                if (null != value && !"".equals(value)) {

                    String fieldType = field.getType().getSimpleName();

                    if ("String".equals(fieldType)) {

                        fieldSetMet.invoke(bean, value);

                    } else if ("Date".equals(fieldType)) {

                        Date temp = parseDate(value);

                        fieldSetMet.invoke(bean, temp);

                    } else if ("Integer".equals(fieldType)

                            || "int".equals(fieldType)) {

                        Integer intval = Integer.parseInt(value);

                        fieldSetMet.invoke(bean, intval);

                    } else if ("Long".equalsIgnoreCase(fieldType)) {

                        Long temp = Long.parseLong(value);

                        fieldSetMet.invoke(bean, temp);

                    } else if ("Double".equalsIgnoreCase(fieldType)) {

                        Double temp = Double.parseDouble(value);

                        fieldSetMet.invoke(bean, temp);

                    } else if ("Boolean".equalsIgnoreCase(fieldType)) {

                        Boolean temp = Boolean.parseBoolean(value);

                        fieldSetMet.invoke(bean, temp);

                    } else {

                        System.out.println("not supper type" + fieldType);

                    }

                }

            } catch (Exception e) {

                continue;

            }

        }


    }


    public static String parSetName(String fieldName) {

        if (null == fieldName || "".equals(fieldName)) {

            return null;

        }

        return "set" + fieldName.substring(0, 1).toUpperCase()

                + fieldName.substring(1);

    }


    public static boolean checkSetMet(Method[] methods, String fieldSetMet) {

        for (Method met : methods) {

            if (fieldSetMet.equals(met.getName())) {

                return true;

            }

        }

        return false;

    }


    public static Date parseDate(String datestr) {

        if (n
展开阅读全文