java 如何实现判断一个对象所有的属性是否为空

如题所述

public static boolean validateEntryParam(Object entryParam) {
// 对象为空,直接返回false
if (null == entryParam) {
return false;
}
// 判断entryParam对象各个变量是否为空,(有一个不为空,返回true)
for (Field f : entryParam.getClass().getDeclaredFields()) {
try {
// 变量设置为可获取
f.setAccessible(true);
// 如果是字符串,如果不为空或者是空字符串,返回true
if (f.get(entryParam) instanceof String) {
if (StringUtils.isNotBlank((String) f.get(entryParam))) {
return true;
}
} else if (f.get(entryParam) != null) {// 不是字符串,不为空的话直接返回true
return true;
}
} catch (Exception e) {
e.printStackTrace();
// 异常验证下一个
continue;
}
}
return false;
}
温馨提示:答案为网友推荐,仅供参考
相似回答