地图

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

488
地图-> Bean
/*n o w j a v a . c o m - 时代Java 提供*/


import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import java.math.BigDecimal;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;


public class Main{

    private static String FIELD_DATE_FORMAT = "yyyy-MM-dd";

    private static String[] FIELD_TYPE_SIMPLE = { "java.lang.String",

            "java.lang.Integer", "int", "java.util.Date",

            "java.math.BigDecimal" };

    private static String FIELD_TYPE_INTEGER = "java.lang.Integer,int";/** 来自 时代Java**/

    private static String FIELD_TYPE_DATE = "java.util.Date";

    private static String FIELD_TYPE_BIGDECIMAL = "java.math.BigDecimal";

    public static void copyProperties(Map srcMap, Object targetObj)

            throws Exception {

        copyProperties(srcMap, targetObj, false);

    }

    

    public static void copyProperties(Map srcMap, Object targetBean,

            boolean isToCamel) {

        try {

            PropertyDescriptor[] propertyDescriptors = Introspector

                    .getBeanInfo(targetBean.getClass())

                    .getPropertyDescriptors();

            for (PropertyDescriptor prop : propertyDescriptors) {

                Method writeMethod = prop.getWriteMethod();

                if (writeMethod != null) {

                    for (Object obj : srcMap.keySet()) {

                        String mapKey = (String) obj;

                        String mapkeyCamel = isToCamel ? toCamel(mapKey

                                .toLowerCase()) : mapKey;

                        String beanPropName = isToCamel ? toCamel(prop

                                .getName()) : prop.getName();

                        if (mapkeyCamel.equals(beanPropName)) {

                            if (!Modifier.isPublic(writeMethod

                                    .getDeclaringClass().getModifiers())) {

                                writeMethod.setAccessible(true);

                            }

                            Object value = srcMap.get(mapKey);

                            if (value == null) {

                                break;

                            }

                            if (!(prop.getPropertyType().getName()

                                    .equals(value.getClass().getName()))) {

                                value = parseByType(prop.getPropertyType(),

                                        value.toString());

                            }

                            writeMethod.invoke((Object) targetBean,

                                    new Object[] { value });

                            break;

                        }

                    }

                }

            }

        } catch (Exception e) {

            throw new RuntimeException("Map->Bean copy .", e);

        }

    }

    public static void copyProperties(Object source, Object target) {

        copyProperties(source, target, false);

    }

    

    public static void copyProperties(Object source, Object target,

            boolean isToCamel) {

        try {

            PropertyDescriptor[] targetProps = Introspector.getBeanInfo(

                    target.getClass()).getPropertyDescriptors();

            for (PropertyDescriptor targetProp : targetProps) {

                Method writeMethod = targetProp.getWriteMethod();

                if (writeMethod != null) {

                    PropertyDescriptor[] srcProps = Introspector

                            .getBeanInfo(source.getClass())

                            .getPropertyDescriptors();

                    for (PropertyDescriptor srcProp : srcProps) {

                        String srcPropName = isToCamel ? toCamel(srcProp

                                .getName()) : srcProp.getName();

                        String targetPropName = isToCamel ? toCamel(targetProp

                                .getName()) : targetProp.getName();

                        if (srcPropName.equals(targetPropName)) {

                            Method readMethod = srcProp.getReadMethod();

                            if (!Modifier.isPublic(readMethod

                                    .getDeclaringClass().getModifiers())) {

                                readMethod.setAccessible(true);

                            }

                            Object value = readMethod.invoke(source,

                                    new Object[0]);

                            if (value == null) {

                                break;

                            }

                            if (!Modifier.isPublic(writeMethod

                                    .getDeclaringClass().getModifiers())) {

                                writeMethod.setAccessible(true);

                            }

                            if (!(targetProp.getPropertyType().getName()

                                    .equals(value.getClass().getName()))) {

                                value = parseByType(

                                        targetProp.getPropertyType(), value);

                            }


                            writeMethod.invoke((Object) target,

                                    new Object[] { value });

                            break;

                        }

                    }

                }

            }

        } catch (Exception e) {

            throw new RuntimeException("Bean->Bean copy error.", e);

        }

    }

    private static String toCamel(String srcStr) {


        StringBuilder sb = new StringBuilder();

        boolean match = false;

        for (int i = 0; i < srcStr.length(); i++) {

            char ch = srcStr.charAt(i);

            if (match && ch >= 97 && ch <= 122)

                ch -= 32;

            if (ch != '_') {

                match = false;

                sb.append(ch);

            } else {

                match = true;

            }

        }

        return sb.toString();

    }

    private static Object parseByType(Class targetClazz, Object srcObj)

            throws ParseException, InstantiationException,

            IllegalAccessException, SecurityException,

            IllegalArgumentException, NoSuchMethodException,

            InvocationTargetException {

        Object obj = "";

        String clazzName = targetClazz.getName().trim();

        if (isSimpleType(clazzName)) {

            String srcStr = srcObj.toString();

            if (FIELD_TYPE_INTEGER.contains(clazzName)) {

                obj = parseInteger(srcStr);

            } else if (FIELD_TYPE_DATE.contains(clazzName)) {

                obj = parseDate(srcStr);

            } else if (FIELD_TYPE_BIGDECIMAL.contains(clazzName)) {

                obj = parseBigDecimal(srcStr);

            } else {

                obj = srcStr;

            }

        } else {

            obj = srcObj;

        }

        return obj;

    }

    private static boolean isSimpleType(String 
展开阅读全文