Determine whether all field properties in the object are null or empty strings

Determine whether the attribute value in the object is empty through reflection

1 If there is no basic data type in the object, you can use this method

    //判断对象值是否为空
    public static boolean isAllFieldsNull(Object obj) {
    
    
        for (Field f : obj.getClass().getDeclaredFields()) {
    
    
            f.setAccessible(true);
            try {
    
    
                if (f.get(obj) != null && !"".equals(f.get(obj).toString().trim())) {
    
    
                    return false;
                }
            } catch (IllegalAccessException e) {
    
    
                e.printStackTrace();
            }
        }
        return true;
    }

2 If there is a basic type, you need to determine the basic type.

    /**
     * 判断对象中的所有字段属性是否都为null或空字符串
     *
     * @param obj 待判断的对象
     * @return 如果所有字段属性都为null或空字符串,则返回true;否则返回false
     */
    public static boolean isObjectFieldsEmpty(Object obj) {
    
    
        Field[] fields = obj.getClass().getDeclaredFields(); // 获取类中的所有字段属性
        try {
    
    
            for (Field field : fields) {
    
    
                field.setAccessible(true); // 设置可访问私有属性

                if (field.get(obj) == null) {
    
     // 判断字段值是否为null
                    continue;
                }

                if (field.getType().isPrimitive()) {
    
     // 判断字段类型是否为基本数据类型
                    // 如果是基本数据类型,先将其转换成对应的包装类型再进行判断
                    if (field.getType().equals(boolean.class)) {
    
    
                        if (!Boolean.valueOf(field.getBoolean(obj))) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(byte.class)) {
    
    
                        if (!(Byte.valueOf(field.getByte(obj)) != 0)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(short.class)) {
    
    
                        if (!(Short.valueOf(field.getShort(obj)) != 0)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(int.class)) {
    
    
                        if (!(Integer.valueOf(field.getInt(obj)) != 0)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(long.class)) {
    
    
                        if (!(Long.valueOf(field.getLong(obj)) != 0L)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(float.class)) {
    
    
                        if (!(Float.valueOf(field.getFloat(obj)) != 0.0f)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(double.class)) {
    
    
                        if (!(Double.valueOf(field.getDouble(obj)) != 0.0d)) {
    
    
                            return false;
                        }
                    } else if (field.getType().equals(char.class)) {
    
    
                        if (!(Character.valueOf(field.getChar(obj)) != '\u0000')) {
    
    
                            return false;
                        }
                    }
                } else {
    
     // 非基本数据类型,判断其是否为空字符串
                    if (!"".equals(field.get(obj).toString().trim())) {
    
    
                        return false;
                    }
                }
            }
        } catch (IllegalAccessException e) {
    
    
            System.err.println("访问权限异常:" + e.getMessage());
            return false;
        }
        return true;
    }

Guess you like

Origin blog.csdn.net/qq_41345584/article/details/130513294