java判断对象是否为空

最近项目中遇到一个问题,在用户没填数据的时候,我们需要接收从前端传过来的对象为null,但是前端说他们一个一个判断特别麻烦,只能传个空对象过来,我第一个想法就是可以通过反射来判断对象是否为空。

第一版:

User.java

public class User {
private String username;

private Boolean active;

private Long id;
// 省略get和set方法

}
ReflectUtil.java

public class ReflectUtil {
public static boolean isObjectNull(Object obj){
if (obj != null) {
Class<?> objClass = obj.getClass();
Method[] declaredMethods = objClass.getDeclaredMethods();
if (declaredMethods.length > 0) {
int methodCount = 0; // get 方法数量
int nullValueCount = 0; // 结果为空

            for (Method declaredMethod : declaredMethods) {
                String name = declaredMethod.getName();
                if (name.startsWith("get") || name.startsWith("is")){
                    methodCount += 1;
                    try {
                        Object invoke = declaredMethod.invoke(obj);
                        if (invoke == null) {
                            nullValueCount += 1;
                        }
                    } catch (IllegalAccessException | InvocationTargetException e){
                        e.printStackTrace();
                    }
                }
            }
            return methodCount == nullValueCount;
        }
    }
    return false;
}

}

TestReflect.java

public class TestReflect {
public static void main(String[] args) {
User user = new User();
System.out.println(ReflectUtil.isObjectNull(user));
}
}
结果:

true
第一版 获取一个类的声明的方法,判断方法如果以get或者is开头就是get方法,然后通过反射调用改方法获取结果,再判断结果是否为空,如果结果为null的话就把nullValueCount+1,最后返回结果为空的值的数量和get方法数量比较的结果,如果两者数量相同则说明该对象为空,反之不为空。
第一版也可以判断一个对象是否为空,但前提是对象必须使用包装类,没有默认值的就不行了,当然你也可以根据类型和返回值结果来判断对象是否为空,但是如果想忽略某个属性不做判断,改起来就有点麻烦了。 后来想知道spring 的BeanUtils 是怎么实现属性复制的就看了一下,发现了新的方法,于是就有了第二版。

第二版:

/**
* 判断对象是否为空,
* @param obj
* @param ignoreProperties 忽略的属性
* @return 如果get 方法的数量等于 属性为空的数量 返回true,否则false
*/
public static boolean isNullObject(Object obj , String... ignoreProperties) throws IntrospectionException {
if (obj != null) {
Class<?> objClass = obj.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(objClass);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

        int count = 1; // 结果为空的属性数量 初始化为1 去除Object的getClass方法
        int propertyCount = propertyDescriptors.length; // 属性数量
        if (ignoreList != null){
            propertyCount -= ignoreList.size();
        }

        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            Method readMethod = propertyDescriptor.getReadMethod();
            String name = propertyDescriptor.getName();
            if (readMethod != null && (ignoreList == null || !ignoreList.contains(name))) {
                Class<?> returnType = readMethod.getReturnType();
                String typeName = returnType.getSimpleName();
                Object invoke = null;
                try {
                    invoke = readMethod.invoke(obj);
                    if (invoke == null) {
                        count+=1;
                    }else {
                        switch (typeName) {
                            case "String":
                                if ("".equals(invoke.toString().trim())) {
                                    count += 1;
                                }
                                break;
                            case "Integer":
                                if ((Integer) invoke <= 0) {
                                    count += 1;
                                }
                                break;
                            case "int":
                                if ((int) invoke <= 0) {
                                    count += 1;
                                }
                                break;
                            case "double":
                                if ((double) invoke <= 0.0d) {
                                    count += 1;
                                }
                                break;
                            case "Double":
                                if ((Double) invoke <= 0.0D) {
                                    count += 1;
                                }
                                break;
                            case "float":
                                if ((float) invoke <= 0.0f) {
                                    count += 1;
                                }
                                break;
                            case "Float":
                                if ((Float) invoke <= 0.0F) {
                                    count += 1;
                                }
                                break;
                            case "Long":
                                if ((Long) invoke <= 0L) {
                                    count += 1;
                                }
                                break;
                            case "long":
                                if ((long) invoke <= 0L) {
                                    count += 1;
                                }
                                break;
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return propertyCount == count;
    }
    return true;
}

第一版和第二版思想基本都是一样的,都是通过读方法去判断返回值是否为空,只不过第二版在第一版上加强了可以忽略属性这个功能。
通过spring 的beanutils发现PropertyDescriptor这个类,从名字看来是个属性描述器,描述属性相关的东西,通过属性描述器可以获取bean的属性名称,读写方法,使用起来还挺方便。
通过Introspector内省类的静态方法getBeanInfo(Class<?> beanClass)获取BeanInfo,然后通过BeanInfo对象的getPropertyDescriptors()就可以返回属性描述器。
由于没有太多研究就不多介绍了。
如果你还有其他方法判断一个对象是否为空请留言,谢谢

能力有限,水平一般,如有错误,请多指出。

posted @ 2024-06-25 00:07  自在现实  阅读(126)  评论(0)    收藏  举报