class ObjectIsEmptyUtil {
public static boolean isEmpty(Object obj, String objProperty) throws Exception, IllegalAccessException {
// 获取类对象
Class<?> clazz = obj.getClass();
// 得到属性集合
Field[] fs = clazz.getDeclaredFields();
// 遍历属性
for (Field field : fs) {
field.setAccessible(true);
// 找到需要判断的属性名称
if (field.getName().equalsIgnoreCase(objProperty)) {
// 判断该对象是否为空
if ((field.get(obj) == null || field.get(obj) == ""
|| "null".equalsIgnoreCase(field.get(obj).toString()))) {
return true;
}
}
}
return false;
}
}
Java中通过反射判断私有属性是否为空的工具类
最新推荐文章于 2024-04-19 20:48:17 发布