Java 判断对象及对象中属性是否为空
程序员文章站
2024-02-21 10:53:46
...
package com.ax.util;
import java.lang.reflect.Field;
/**
* Created by hj.
*/
public class VoUtilsTool {
/****
* @param object 对象
* @return 如果对象不为空,且没有空值。返回false,对象为空或者有空值,返回true
* */
public static boolean checkObjFieldIsNull(Object object) throws IllegalAccessException{
boolean flag = false;
if(null!=object){
for(Field field : object.getClass().getDeclaredFields()){
field.setAccessible(true);
if(field.get(object) == null || field.get(object).equals("")){
flag = true;
return flag;
}
if(field.get(object) != null&&field.get(object).toString().trim().equals("")){
flag = true;
return flag;
}
}
}else{
flag=true;
}
return flag;
}
}