package com.loan.modules.common.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import jodd.util.StringUtil; import com.alibaba.fastjson.JSONObject; public class ObjectUtil { /**
* 将一个 JavaBean 对象转化为一个 Map
* @param bean 要转化的JavaBean 对象
* @return 转化出来的 Map 对象
* @throws IntrospectionException 如果分析类属性失败
* @throws IllegalArgumentException
* @throws IllegalAccessException 如果实例化 JavaBean 失败
* @throws InvocationTargetException 如果调用属性的 setter 方法失败
*/
public static Map<String,Object> convertBean(Object bean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map<String,Object> returnMap = new HashMap<String,Object>();
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(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
} /**
* 将一个 Map 对象转化为一个 JavaBean
* @param type 要转化的类型
* @param map 包含属性值的 map
* @return 转化出来的 JavaBean 对象
* @throws IntrospectionException
* 如果分析类属性失败
* @throws IllegalAccessException
* 如果实例化 JavaBean 失败
* @throws InstantiationException
* 如果实例化 JavaBean 失败
* @throws InvocationTargetException
* 如果调用属性的 setter 方法失败
*/
public static Object convertMap(Class type, Map map)
throws IntrospectionException, IllegalAccessException,
InstantiationException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
Object obj = type.newInstance(); // 创建 JavaBean 对象 // 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(propertyName); Object[] args = new Object[1];
args[0] = value; descriptor.getWriteMethod().invoke(obj, args);
}
}
return obj;
} /**
* 判断 实体类对象所有属性是否全部为空
* @param object 实体类对象
* @return
*/
public static boolean judgeObjectIsEmpty(Object object){
if(object == null) return false;
JSONObject json = (JSONObject) JSONObject.toJSON(object); for(java.util.Map.Entry<String,Object> entry:json.entrySet()){ if(entry.getValue() != null){
if(StringUtil.isNotBlank(entry.getValue().toString())){ return true;
}
} }
return false;
}
}

最新文章

  1. 分享一种容易理解的js去重排序方法
  2. UDP 一个封锁操作被对 WSACancelBlockingCall 的调用中断
  3. centOS填坑笔记(一)
  4. Hadoop ecosystem notes Outline - TODO
  5. thinkphp中模块和操作映射
  6. overflow-x和overflow-y其中一个设置为visible时的奇怪现象
  7. struts2 后台action向前端JSP传递参数的问题
  8. Relocation - POJ 2923(状态压缩+01背包)
  9. L10 安装网卡驱动
  10. 在magento中使用正则式
  11. Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
  12. while循环--登录
  13. 谷歌chrome浏览器vue调试工具vue-devtools的安装
  14. HashMap深度解析(转载)
  15. 664. Strange Printer
  16. 织梦dedecms自定义搜索可以按照附加表字段进行搜索
  17. airflow
  18. 手动为 Team Foundation Server 安装 SQL Server
  19. Makefile中wildcard的介绍
  20. QT 数字图像处理 笔记一

热门文章

  1. LVS之1---工作原理
  2. wdCP V3.2
  3. DEDECMS自动编号(序号)autoindex属性(转)
  4. Tensorflow创建已知分布的张量
  5. 第12章 DOM操作
  6. 白日梦的Elasticsearch笔记(一)基础篇
  7. [从源码学设计]蚂蚁金服SOFARegistry之配置信息
  8. docker mysql 设置忽略大小写
  9. Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节
  10. MySQL ---- 锁知识