copy(class-->class) - Java Reflection

Java examples for Reflection:Java Bean

Description

copy(class-->class)

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
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 = "java2s.com";
        Object target = "java2s.com";
        boolean isCopyNull = true;
        copyProperties(source, target, isCopyNull);
    }/*from  ww w .  j  a  v a 2s  .co m*/

    public static void copyProperties(Object source, Object target,
            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 (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 getGetMethod(Class objectClass, String fieldName)
            throws NoSuchMethodException, SecurityException {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        return objectClass.getMethod(sb.toString());
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static Method getSetMethod(Class objectClass, String fieldName)
            throws NoSuchFieldException, SecurityException,
            NoSuchMethodException {
        Method method = null;
        Class[] parameterTypes = new Class[1];
        Field field = objectClass.getDeclaredField(fieldName);
        parameterTypes[0] = field.getType();
        StringBuffer sb = new StringBuffer();
        sb.append("set");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        method = objectClass.getMethod(sb.toString(), parameterTypes);
        return method;
    }
}

Related Tutorials