You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.3 KiB
Java
188 lines
5.3 KiB
Java
package weaver.interfaces.zlldjcws.cgxxjl.util;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import net.sf.cglib.beans.BeanMap;
|
|
import org.apache.commons.beanutils.BeanUtils;
|
|
|
|
import java.beans.BeanInfo;
|
|
import java.beans.Introspector;
|
|
import java.beans.PropertyDescriptor;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.stream.Stream;
|
|
|
|
public class CgxxUtil {
|
|
|
|
/**
|
|
* 将对象装换为map
|
|
*
|
|
* @param bean
|
|
* @return
|
|
*/
|
|
public <T> Map<String, Object> beanToMapNotNull(T bean) {
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
if (bean != null) {
|
|
BeanMap beanMap = BeanMap.create(bean);
|
|
for (Object key : beanMap.keySet()) {
|
|
String value = String.valueOf(beanMap.get(key));
|
|
if(!"".equals(value) && !"null".equalsIgnoreCase(value) && value !=null && value !=""){
|
|
map.put(String.valueOf(key).toLowerCase(), value);
|
|
}
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
|
|
public <T> Map<String, Object> beanToMap(T bean) {
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
if (bean != null) {
|
|
BeanMap beanMap = BeanMap.create(bean);
|
|
for (Object key : beanMap.keySet()) {
|
|
String value = String.valueOf(beanMap.get(key));
|
|
if(!"".equals(value) && !"null".equalsIgnoreCase(value) && value !=null && value !=""){
|
|
map.put(String.valueOf(key).toLowerCase(), value);
|
|
}
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
/***
|
|
*
|
|
* @param bean
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public Map<String, Object> bean2Map(Object bean) throws Exception {
|
|
|
|
Map map = new HashMap<String, Object>();
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),
|
|
Object.class);
|
|
|
|
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
|
|
for (PropertyDescriptor pd : pds) {
|
|
String name = pd.getName();
|
|
Method md = pd.getReadMethod();
|
|
map.put(name, md.invoke(bean));
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
|
|
// 把map转换为bean 并且为属性赋值
|
|
public <T> T map2Ban(Map<String, Object> beanMap, Class<T> beanType) throws Exception {
|
|
T obj = beanType.newInstance();
|
|
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(beanType,
|
|
Object.class);
|
|
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
|
|
|
|
for (PropertyDescriptor pd : pds) {
|
|
String name = pd.getName();
|
|
Object value = beanMap.get(name);
|
|
Method md = pd.getWriteMethod();
|
|
md.invoke(obj, value);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/***
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public Map<String, Object> beanToMap4Object(Object object) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
Field[] fields = object.getClass().getDeclaredFields();
|
|
Stream.of(fields).forEach(field -> {
|
|
try {
|
|
if (!"serialVersionUID".equals(field.getName())) {
|
|
field.setAccessible(true);
|
|
map.put(field.getName(), field.get(object));
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
return map;
|
|
}
|
|
|
|
/***
|
|
*
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public Map<String, Object> beanToMap4Object2(Object object) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
try {
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
|
|
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
|
|
Stream.of(descriptors).forEach(descriptor -> {
|
|
try {
|
|
if (!"class".equals(descriptor.getName())) {
|
|
Method getter = descriptor.getReadMethod();
|
|
map.put(descriptor.getName(), getter.invoke(object));
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return map;
|
|
}
|
|
|
|
|
|
/***
|
|
*
|
|
* @param bean
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public <T> Map<String, Object> beanToMap2(T bean) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map beanMap = null;
|
|
try {
|
|
beanMap = BeanUtils.describe(bean);
|
|
} catch (IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
} catch (InvocationTargetException e) {
|
|
e.printStackTrace();
|
|
} catch (NoSuchMethodException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return beanMap;
|
|
}
|
|
|
|
|
|
/**
|
|
* 对象转Map
|
|
* @param object
|
|
* @return
|
|
*/
|
|
public Map beanToMap3(Object object){
|
|
return JSONObject.parseObject(JSON.toJSONString(object),Map.class);
|
|
}
|
|
|
|
/**
|
|
* map转对象
|
|
* @param map
|
|
* @param beanClass
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public <T> T mapToBean3(Map map, Class<T> beanClass) {
|
|
return JSONObject.parseObject(JSON.toJSONString(map), beanClass);
|
|
|
|
}
|
|
|
|
|
|
}
|