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 Map beanToMapNotNull(T bean) { Map map = new HashMap(); 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 Map beanToMap(T bean) { Map map = new HashMap(); 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 bean2Map(Object bean) throws Exception { Map map = new HashMap(); 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 map2Ban(Map beanMap, Class 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 beanToMap4Object(Object object) { Map 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 beanToMap4Object2(Object object) { Map 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 * @return */ public Map beanToMap2(T bean) { Map 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 * @return */ public T mapToBean3(Map map, Class beanClass) { return JSONObject.parseObject(JSON.toJSONString(map), beanClass); } }