Commit ee4121c92fd8fa530ae52f2299641dc882935b49

Authored by xqs
1 parent 26c17b50

object的空判断工具类

src/main/java/com/huaheng/common/utils/StringUtils.java
1 1 package com.huaheng.common.utils;
2 2  
  3 +import java.lang.reflect.Field;
3 4 import java.util.Collection;
4 5 import java.util.Map;
5 6 import org.apache.commons.lang.text.StrBuilder;
... ... @@ -125,7 +126,37 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
125 126 */
126 127 public static boolean isNull(Object object)
127 128 {
128   - return object == null;
  129 +
  130 + if (object == null) {
  131 + return true;
  132 + }
  133 + // 得到类对象
  134 + Class clazz = (Class) object.getClass();
  135 + // 得到所有属性
  136 + Field[] fields = clazz.getDeclaredFields();
  137 + //定义返回结果,默认为true
  138 + boolean flag = true;
  139 + for (Field field : fields) {
  140 + field.setAccessible(true);
  141 + Object fieldValue = null;
  142 + String fieldName = null;
  143 + try {
  144 + //得到属性值
  145 + fieldValue = field.get(object);
  146 + //得到属性类型
  147 + //Type fieldType = field.getGenericType();
  148 + //得到属性名
  149 + fieldName = field.getName();
  150 + } catch (IllegalArgumentException | IllegalAccessException e) {
  151 + e.printStackTrace();
  152 + }
  153 + //只要有一个属性值不为null 就返回false 表示对象不为null 忽略序列化
  154 + if (fieldValue != null && !"serialVersionUID".equals(fieldName)) {
  155 + flag = false;
  156 + break;
  157 + }
  158 + }
  159 + return flag;
129 160 }
130 161  
131 162 /**
... ...