用于校验对象属性是否为空的工具类

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
public class PropertyUtils {
 
 
    /**
     * 判断属性是否为null或""
     * @param args
     * @return 是则返回true
     */
    public static Boolean isEmpty(Object ...args){
        for (Object arg : args) {
            if(arg instanceof String){
                String s = String.valueOf(arg);
                if(StringUtils.isEmpty(s)) return true;
            }else {
                if(arg == null) return true;
            }
        }
        return false;
    }
 
 
    /**
     *判断对象的属性是否为空
     * @param obj 传入的对象
     * @return
     */
    public static Boolean isEmpty(Object obj) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object o = field.get(obj);
                if(o == null){
                    return false;
                }
                if(o instanceof String){
                    if(o.equals("")) return false;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
 
 
    /**
     *判断对象的属性是否为空
     * @param obj 传入的对象
     * @param exclude 排除的属性 (不区分大小写)
     * @return
     */
    public static Boolean isEmpty(Object obj,String ...exclude) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            outer:
            for (Field field : fields) {
                field.setAccessible(true);
                Object o = field.get(obj);
                String name = field.getName();
                for (String arg : exclude) {
                    if(name.equalsIgnoreCase(arg)) continue outer;
                }
                if(o == null){
                    return false;
                }
                if(o instanceof String){
                    if(o.equals("")) return false;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
 
}
posted @   小闸瓦学java  阅读(401)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示