好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java如何判断一个对象是否为空对象

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

第一版:

User.java

?

1

2

3

4

5

6

7

8

public class User {

    private String username;

 

    private Boolean active;

 

    private Long id;

    // 省略get和set方法

}

ReflectUtil.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

?

1

2

3

4

5

6

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 是怎么实现属性复制的就看了一下,发现了新的方法,于是就有了第二版。

第二版:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

/**

      *  判断对象是否为空,

      * @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()就可以返回属性描述器。
由于没有太多研究就不多介绍了。

到此这篇关于java如何判断一个对象是否为空对象的文章就介绍到这了,更多相关java 判断对象是否为空内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_39654841/article/details/105206621

查看更多关于java如何判断一个对象是否为空对象的详细内容...

  阅读:33次