/* 判断对象中属性值是否全为空 */ public static boolean checkObjAllFieldsIsNull(Object object) { if (null == object) { return true; } try { for (Field f : object.getClass().getDeclaredFields()) { f.setAccessible(true); if (f.get(object) != null && StringUtils.isNotBlank(f.get(object).toString())) { return false; } } } catch (Exception e) { e.printStackTrace(); } return true; } /** *判断一个实体类对象实例的所有成员变量是否为空 *@param obj 校验的类对象实例 *@return List *@throws Exception */ public static List<String> isObjectFieldEmpty(Object obj) throws Exception { //得到类对象 Class<?> clazz=obj.getClass(); //得到属性集合 Field[] fs=clazz.getDeclaredFields(); List<String> list=new ArrayList<String>(); //遍历属性 for(Field field:fs){ //设置属性是可以访问的(私有的也可以) field.setAccessible(true); if(field.get(obj)==null||field.get(obj)==""||"null".equalsIgnoreCase((String)field.get(obj))){ String name=(String)field.getName(); list.add(name); } } return list; }
java 对象和List 里面的对象判断属性是不是为空
于 2022-04-18 09:34:13 首次发布