集册 Java实例教程 复制(类

复制(类

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

451
复制(class-> class)


//package com.nowjava;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;
/* 
*来 自
 时 代 J a v a - nowjava.com
*/

import java.lang.reflect.Method;

import java.util.Map;

import java.util.Set;


public class Main {

    public static void main(String[] argv) throws Exception {

        Object source = "nowjava.com";

        Object target = "nowjava.com";

        boolean isCopyNull = true;

        copyProperties(source, target, isCopyNull);

    }


    public static void copyProperties(Object source, Object target,

            boolean isCopyNull, String[] ignoreProperties) {

        Field[] fields = getFields(source);//来自 时 代 J     a    v  a - nowjava.com

        for (Field field : fields) {

            String name = field.getName();

            Object value = null;

            try {

                value = invokeGet(source, name);


                if (null != ignoreProperties

                        && isHasStr(ignoreProperties, name)) {

                    continue;

                }

                if (isHasField(target, name)) {

                    if (null != value) {

                        invokeSet(target, name, value);

                    } else if (isCopyNull) {

                        invokeSet(target, name, value);

                    }

                }

            } catch (Exception e) {

                continue;

            }

        }

    }


    public static void copyProperties(Object source, Object target,

            boolean isCopyNull) {

        copyProperties(source, target, isCopyNull, null);

    }


    @SuppressWarnings("rawtypes")

    public static void copyProperties(Map<String, Object> map,

            Object target, boolean isCopyNull, String[] ignoreProperties) {

        Set set = map.keySet();

        for (Object object : set) {

            try {

                String name = object.toString();

                Object value = map.get(name);

                if (null != ignoreProperties

                        && isHasStr(ignoreProperties, name)) {

                    continue;

                }

                if (isHasField(target, name)) {

                    if (null != value) {

                        invokeSet(target, name, value);

                    } else if (isCopyNull) {

                        invokeSet(target, name, value);

                    }

                }

            } catch (Exception e) {

                continue;

            }

        }

    }


    public static void copyProperties(Map<String, Object> map,

            Object target, boolean isCopyNull) {

        copyProperties(map, target, isCopyNull, null);

    }


    @SuppressWarnings({ "rawtypes", "unchecked" })

    public static void copyProperties(Object source, Map map,

            boolean isCopyNull, String[] ignoreProperties) {

        Field[] fields = getFields(source);

        for (Field field : fields) {

            String name = field.getName();

            Object value = null;

            try {

                value = invokeGet(source, name);


                if (null != ignoreProperties

                        && isHasStr(ignoreProperties, name)) {

                    continue;

                }

                if (null != value) {

                    map.put(name, value);

                } else if (isCopyNull) {

                    map.put(name, value);

                }

            } catch (Exception e) {

                continue;

            }

        }

    }


    @SuppressWarnings("rawtypes")

    public static void copyProperties(Object source, Map map,

            boolean isCopyNull) {

        copyProperties(source, map, isCopyNull, null);

    }


    private static Field[] getFields(Object o) {

        Field[] fields = o.getClass().getDeclaredFields();

        return fields;

    }


    private static Object invokeGet(Object o, String fieldName)

            throws IllegalAccessException, IllegalArgumentException,

            InvocationTargetException, NoSuchMethodException,

            SecurityException {

        Method method = getGetMethod(o.getClass(), fieldName);

        return method.invoke(o, new Object[0]);

    }


    private static boolean isHasStr(String[] ignoreProperties, String str) {

        for (String string : ignoreProperties) {

            if (string.equals(str)) {

                return true;

            }

        }

        return false;

    }


    private static boolean isHasField(Object o, String fieldName) {

        Field[] fields = getFields(o);

        for (Field field : fields) {

            if (field.getName().equals(fieldName)) {

                return true;

            }

        }

        return false;

    }


    private static void invokeSet(Object o, String fieldName, Object value)

            throws IllegalAccessException, IllegalArgumentException,

            InvocationTargetException, NoSuchFieldException,

            SecurityException, NoSuchMethodException {

        Method method = getSetMethod(o.getClass(), fieldName);

        method.invoke(o, new Object[] { value });

    }


    @SuppressWarnings({ "unchecked", "rawtypes" })

    private static Method getGetMetho
展开阅读全文