JAVABEAN 릴레이 MAP 구현

10147 단어 에세이
public class ObjectToMap{          public static Map objectToMap(Object obj){         try{             Class type = obj.getClass();             Map returnMap = new HashMap();             BeanInfo beanInfo = Introspector.getBeanInfo(type);
            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();             for (int i = 0; i< propertyDescriptors.length; i++) {                 PropertyDescriptor descriptor = propertyDescriptors[i];                 String propertyName = descriptor.getName();                 if (!propertyName.equals("class")) {                     Method readMethod = descriptor.getReadMethod();                     Object result = readMethod.invoke(obj, new Object[0]);                     if(result == null){                         continue; }//판단 기준: String, Boolean, Byte, Short, Integer, Long, Float, Double//판단 집합 여부, COLLECTION,MAP                                   if(result instanceof String                             || result instanceof Boolean                             || result instanceof Byte                             || result instanceof Short                             || result instanceof Integer                             || result instanceof Long                             || result instanceof Float                             || result instanceof Double                             || result instanceof Enum                             ){                         if (result != null) {                             returnMap.put(propertyName, result);                         }                     }else if(result instanceof Collection){                                                 Collection> lstObj = arrayToMap((Collection>)result);                         returnMap.put(propertyName, lstObj);                                                      }else if(result instanceof Map){                         Map lstObj = mapToMap((Map)result);                         returnMap.put(propertyName, lstObj);                     } else {                         Map mapResult = objectToMap(result);                         returnMap.put(propertyName, mapResult);                     }                                      }             }             return returnMap;         }catch(Exception e){             throw new RuntimeException(e);         }              }              private static Map mapToMap(Map orignMap) {         Map resultMap = new HashMap();         for(Entry entry:orignMap.entrySet()){             Object key = entry.getKey();             Object resultKey = null;             if(key instanceof Collection){                 resultKey = arrayToMap((Collection)key);             }else if(key instanceof Map){                 resultKey = mapToMap((Map)key);             }             else{                 if(key instanceof String                         || key instanceof Boolean                         || key instanceof Byte                         || key instanceof Short                         || key instanceof Integer                         || key instanceof Long                         || key instanceof Float                         || key instanceof Double                         || key instanceof Enum                         ){                     if (key != null) {                         resultKey = key;                     }                 }else{                     resultKey = objectToMap(key);                 }                             }             
            Object value = entry.getValue();             Object resultValue = null;             if(value instanceof Collection){                 resultValue = arrayToMap((Collection)value);             }else if(value instanceof Map){                 resultValue = mapToMap((Map)value);             }             else{                 if(value instanceof String                         || value instanceof Boolean                         || value instanceof Byte                         || value instanceof Short                         || value instanceof Integer                         || value instanceof Long                         || value instanceof Float                         || value instanceof Double                         || value instanceof Enum                         ){                     if (value != null) {                         resultValue = value;                     }                 }else{                     resultValue = objectToMap(value);                 }                             }                          resultMap.put(resultKey, resultValue);         }                 return resultMap;     }
    private static Collection arrayToMap(Collection lstObj){         ArrayList arrayList = new ArrayList();                  for (Object t : lstObj) {             if(t instanceof Collection){                 Collection result = arrayToMap((Collection)t);                 arrayList.add(result);             }else if(t instanceof Map){                 Map result = mapToMap((Map)t);                 arrayList.add(result);             } else {                 if(t instanceof String                         || t instanceof Boolean                         || t instanceof Byte                         || t instanceof Short                         || t instanceof Integer                         || t instanceof Long                         || t instanceof Float                         || t instanceof Double                         || t instanceof Enum                         ){                     if (t != null) {                         arrayList.add(t);                     }                 }else{                     Object result = objectToMap(t);                     arrayList.add(result);                     }                             }         }         return arrayList;     }      }
 
 
 
/**
 *  Object Map 
 * @param obj
 * @return
 * @throws Exception
 */
public static Object objectToMap(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }

    Map map = new HashMap();
    if (!isBaseType(obj)) {
        if (obj.getClass().equals(java.util.List.class) || obj.getClass().equals(java.util.ArrayList.class)) {
            List list=new ArrayList();
            for (int i = 0; i < ((List) obj).size(); i++) {
                Object tmp = ((List) obj).get(i);
                if (isBaseType(tmp)) {
                    list.add(tmp);
                } else {
                    list.add(objectToMap(tmp));
                }

            }
            return list;
        } else if(obj.getClass().equals(java.util.Map.class)|| obj.getClass().equals(java.util.LinkedHashMap.class) || obj.getClass().equals(java.util.HashMap.class)){
            for (Map.Entry entry : ((Map)obj).entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                if (isBaseType(entry.getValue())) {
                    map.put(entry.getKey() + "", entry.getValue());
                } else {
                    map.put(entry.getKey() + "", objectToMap(entry.getValue()));
                }
            }
        }else {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                Object tmp = getFieldValueByName(field.getName(), obj);
                if (isBaseType(tmp)) {
                    field.setAccessible(true);
                    map.put(field.getName(), field.get(obj));
                } else {
                    map.put(field.getName(), objectToMap(tmp));

                }
            }
            map.put("class", obj.getClass().getName());
        }
    }else{
        return obj;
    }
    return map;
}

public static boolean isBaseType(Object object) {
    if (object != null) {
        Class className = object.getClass();
        if (className.equals(java.lang.Integer.class) ||
                className.equals(java.lang.Byte.class) ||
                className.equals(java.lang.Long.class) ||
                className.equals(java.lang.Double.class) ||
                className.equals(java.lang.Float.class) ||
                className.equals(java.lang.Character.class) ||
                className.equals(java.lang.Short.class) ||
                className.equals(java.lang.Boolean.class)||
                className.equals(java.math.BigDecimal.class)||
                className.equals(java.lang.String.class)||
                className.equals(java.lang.Integer.class)) {
            return true;
        }
        return false;
    }
    return true;
}

/**
 *     *
 */
private static Object getFieldValueByName(String fieldName, Object o) {
    String firstLetter = fieldName.substring(0, 1).toUpperCase();
    String getter = "get" + firstLetter + fieldName.substring(1);
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        getFieldValueByIsName(fieldName, o);
    }
    return value;
}

/**
 *     *
 */
private static Object getFieldValueByIsName(String fieldName, Object o) {
    String firstLetter = fieldName.substring(0, 1).toUpperCase();
    String getter = "is" + firstLetter + fieldName.substring(1);
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        getFieldValueByGetName(fieldName, o);
    }
    return value;
}

/**
 *     *
 */
private static Object getFieldValueByGetName(String fieldName, Object o) {
    String getter = "get" + fieldName;
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

좋은 웹페이지 즐겨찾기