JAVABEAN 릴레이 MAP 구현
10147 단어 에세이
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
시간 형식 변환, "2018-07-12T07:45:0.000Z"와 유사 = > 2018-07-11 15:45:29정의: 호출 tip: 제가 vue에서 사용한 것도 시간 뒤에 추가할 수 있습니다.split(‘T’)[0]...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.