166 lines
4.5 KiB
Java
166 lines
4.5 KiB
Java
package com.cm.common.utils;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.beans.BeanInfo;
|
|
import java.beans.Introspector;
|
|
import java.beans.PropertyDescriptor;
|
|
import java.lang.reflect.Method;
|
|
import java.util.Enumeration;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
/**
|
|
* @author wenc
|
|
* @className hashMap 工具类
|
|
* @description hashMap 工具类
|
|
* @date 2018年2月2日 下午4:59:38
|
|
*/
|
|
public class HashMapUtil {
|
|
|
|
/**
|
|
* 请求参数转Map
|
|
*
|
|
* @param request
|
|
* @return
|
|
*/
|
|
public static Map<String, Object> requestParamsToMap(HttpServletRequest request) {
|
|
Enumeration<String> requestNames = request.getParameterNames();
|
|
Map<String, Object> params = new HashMap<>();
|
|
while (requestNames.hasMoreElements()) {
|
|
String name = requestNames.nextElement();
|
|
String value = request.getParameter(name);
|
|
if (value.isEmpty()) {
|
|
continue;
|
|
}
|
|
params.put(name, value);
|
|
}
|
|
return params;
|
|
}
|
|
|
|
/**
|
|
* 类转Map
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public static Map<String, Object> objectToMap(Object object) {
|
|
Map<String, Object> result = null;
|
|
if (object instanceof Map) {
|
|
result = mapObjToMap(object);
|
|
} else {
|
|
try {
|
|
result = beanToMap(object);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 类转Map<String,String>
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public static Map<String, String> objectToMapString(Object object) {
|
|
Map<String, String> result = null;
|
|
if (object instanceof Map) {
|
|
result = mapObjToMapString(object);
|
|
} else {
|
|
try {
|
|
result = beanToMapString(object);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* map转map<String, String>
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
private static Map<String, String> mapObjToMapString(Object object) {
|
|
Map<String, String> result = new HashMap<>();
|
|
Map<?, ?> map = (Map<?, ?>) object;
|
|
for (Entry<?, ?> entry : map.entrySet()) {
|
|
String key = entry.getKey().toString();
|
|
Object value = entry.getValue();
|
|
result.put(key, value.toString());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* map转map
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
private static Map<String, Object> mapObjToMap(Object object) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
Map<?, ?> map = (Map<?, ?>) object;
|
|
for (Entry<?, ?> entry : map.entrySet()) {
|
|
String key = entry.getKey().toString();
|
|
Object value = entry.getValue();
|
|
result.put(key, value);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* bean转map
|
|
*
|
|
* @param object
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
private static Map<String, Object> beanToMap(Object object) throws Exception {
|
|
Map<String, Object> result = null;
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
|
|
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
|
|
for (PropertyDescriptor property : properties) {
|
|
if (null == result) {
|
|
result = new HashMap<>();
|
|
}
|
|
Method method = property.getReadMethod();
|
|
String name = property.getName();
|
|
if ("class".equals(name)) {
|
|
continue;
|
|
}
|
|
result.put(name, method.invoke(object));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* bean转Map<String, String>
|
|
*
|
|
* @param object
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
private static Map<String, String> beanToMapString(Object object) throws Exception {
|
|
Map<String, String> result = null;
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
|
|
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
|
|
for (PropertyDescriptor property : properties) {
|
|
if (null == result) {
|
|
result = new HashMap<>();
|
|
}
|
|
Method method = property.getReadMethod();
|
|
String name = property.getName();
|
|
if ("class".equals(name)) {
|
|
continue;
|
|
}
|
|
result.put(name, method.invoke(object).toString());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|