复制豆

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

581
复制豆


//package com.nowjava;
/* 来自 时   代    Java - nowjava.com*/

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;


import java.util.HashMap;


public class Main {

    /**

     * 

     * @param dest

     * @param source

     * @throws SecurityException

     * @throws NoSuchFieldException

     * @throws IllegalArgumentException

     * @throws IllegalAccessException

     * @throws InvocationTargetException

     */

    public static void copyBean(Object dest, Object source)// from nowjava.com - 时  代  Java

            throws SecurityException, NoSuchFieldException,

            IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        Method[] destMethods = dest.getClass().getDeclaredMethods();

        Method[] sourceMethods = source.getClass().getDeclaredMethods();

        HashMap<String, Method> destMethodMap = new HashMap<>();

        HashMap<String, Method> sourceMethodMap = new HashMap<>();


        if (null != sourceMethods && null != destMethods) {

            for (Method m : sourceMethods) {

                sourceMethodMap.put(m.getName(), m);

            }

            for (Method m : destMethods) {

                destMethodMap.put(m.getName(), m);

            }

        } else {

            return;

        }

        for (Field field : source.getClass().getDeclaredFields()) {

            if (Modifier.isStatic(field.getModifiers())

                    || Modifier.isFinal(field.getModifiers())) {

                continue;

            }

            String getMethodName = "get"

                    + field.getName().substring(0, 1).toUpperCase()

                    + field.getName().substring(1);

            String setMethodName = "set"

                    + field.getName().substring(0, 1).toUpperCase()

                    + field.getName().substring(1);

            if (destMethodMap.get(setMethodName) == null

                    || sourceMethodMap.get(getMethodName) == null) {

                setValueByFieldName(dest, field.getName(),

                        getValueByFieldName(source, field.getName()));

            } else {

                Method destMethod = (Method) destMethodMap

                        .get(setMethodName);

                Method sourceMethod = (Method) sourceMethodMap

                        .get(getMethodName);

                destMethod.invoke(dest, sourceMethod.invoke(source));

            }

        }

    }


    /**

     * 

     * @param obj

     * @param fieldName

     * @param value

     * @throws SecurityException

     * @throws NoSuchFieldException

     * @throws IllegalArgumentException

     * @throws IllegalAccessException

     */

    public static void setValueByFieldName(Object obj, String fieldName,

            Object value) throws SecurityException, NoSuchFieldException,

            IllegalArgumentException, IllegalAccessException {

        Field field = obj.getClass().getDeclaredField(fieldName);

        if (field.isAccessible()) {

            field.set(obj, value);

        } else {

            field.setAccessible(true);

            field.set(obj, value);

            field.setAccessible(false);

        }

    }


    /**

     * 

     * @param obj

     * @param fieldName

     * @return

     * @throws SecurityException

     * @throws NoSuchFieldException

     * @throws IllegalArgumentException

     * @throws IllegalAccessException

     */

    public static Object getValueByFieldName(Object obj, String fieldName)

            throws SecurityException, NoSuchFieldException,

            IllegalArgumentException, IllegalAccessException {

        Field field = getFieldByFieldName(obj, fieldName);

        Object value = null;

        if (field != null) {

            if (field.isAccessible()) {

               
展开阅读全文