Java中通过反射判断私有属性是否为空的工具类

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;
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_37716758/article/details/84204783
今日推荐