好得很程序员自学网

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

java objectUtils 使用可能会出现的问题

objectUtils使用可能会出现的问题

当一个类没有实现toString方法的时候,objectUtils会通过调用object的toString方法对对象实例化,所以objectUtils.isEmpty可能会对为空的对象返回false

例如

?

1

2

3

4

5

6

String s1 = ObjectUtils.identityToString( null );

String s2 = ObjectUtils.identityToString( "" );

String s3 = ObjectUtils.identityToString(Boolean.TRUE);

System.out.println(s1); // null

System.out.println(s2); //java.lang.String@73a80183

System.out.println(s3); //java.lang.Boolean@1afb7ac7

推荐使用

判断对象为空直接使用null ,判断字符就用string

?

1

2

3

4

public static boolean isNullOrEmpty(Object obj){undefined

return obj == null || [].equals(obj.toString())

}

public static boolean isEmpty(String str ) { return str== null || str.length() == 0 ;}

判断对象内容是否为空ObjectUtils工具类

?

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

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

public class ObjectUtils {

     /**

      * 判断对象是否为空(根据一般常用类型区分)

      * @param object

      * @return: boolean

      */

     public static boolean isEmpty(Object object){

         //判断对象是否为null

         if ( null == object){

             return true ;

         }

         //判断对象是否为String类型

         if (object instanceof String){

             if (object.toString().length() == 0 ){

                 return true ;

             }

         }

         //判断对象是否为Map

         if (object instanceof Map){

             Map map = (Map)object;

             if (map.size() == 0 ){

                 return true ;

             }

         }

         //判断对象是否为List

         if (object instanceof List){

             List list = (List)object;

             if (list.size() == 0 ){

                 return true ;

             }

         }

         //普通的类对象

         Field[] fields = object.getClass().getDeclaredFields();

         //先假设全部属性都是空的,所以只要出现一个属性不为空的就不需要在循环判断

         boolean flag = true ;

         for (Field field : fields) {

             field.setAccessible( true );

             try {

                 if (!ObjectUtils.isEmpty(field.get(object))) {

                     flag = false ;

                     break ;

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return flag;

     }

     /**

      * 判断obejct对象中除了names里面的字段,其他字段都为null(已知对象类型)

      * @param object

      * @param names

      * @return

      */

     public static boolean isEmpty(Object object,String... names){

         Field[] fields = object.getClass().getDeclaredFields();

         //用于判断所有属性是否为空,如果参数为空则不查询

         boolean flag = true ;

         for (Field field : fields) {

             //不检查 直接取值

             field.setAccessible( true );

             try {

                 String fieldName = field.getName();

                 List<String> nameList = new ArrayList<>();

                 if ( null != names && names.length != 0 ){

                     nameList = Arrays.asList(names);

                 }

                 if (!nameList.contains(fieldName) && !Objects.isNull(field.get(object))) {

                     //不为空

                     flag = false ;

                     break ;

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return flag;

     }

     /**

      * 判断object对象中除了names里面的字段,其他字段都为null或者是""(已知对象类型)

      * @param object

      * @param names

      * @return

      */

     public static boolean isBlank(Object object,String... names){

         Field[] fields = object.getClass().getDeclaredFields();

         //用于判断所有属性是否为空,如果参数为空则不查询

         boolean flag = true ;

         for (Field field : fields) {

             //不检查 直接取值

             field.setAccessible( true );

             try {

                 String fieldName = field.getName();

                 List<String> nameList = new ArrayList<>();

                 if ( null != names && names.length != 0 ){

                     nameList = Arrays.asList(names);

                 }

                 Object value = field.get(object);

                 if (!nameList.contains(fieldName) && !isEmpty(value)) {

                     //不为空

                     flag = false ;

                     break ;

                 }

             } catch (Exception e) {

                 e.printStackTrace();

             }

         }

         return flag;

     }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_39347667/article/details/109045098

查看更多关于java objectUtils 使用可能会出现的问题的详细内容...

  阅读:20次