copy(map-->class) - Java Reflection

Java examples for Reflection:Java Bean

Description

copy(map-->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 {

    @SuppressWarnings("rawtypes")
    public static void copyPropertiesMapArray(Map<String, String[]> map,
            Object target, boolean isCopyNull, String[] ignoreProperties) {
        Set set = map.keySet();/* w ww .j a va 2 s .co m*/
        for (Object object : set) {
            try {
                String name = object.toString();
                String value = map.get(name)[0];
                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 copyPropertiesMapArray(Map<String, String[]> map,
            Object target, boolean isCopyNull) {
        copyPropertiesMapArray(map, target, isCopyNull, null);
    }

    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 });
    }

    private static Field[] getFields(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        return fields;
    }

    @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