• 自定义判空注解


    package com.cinc.permissionservice.utils;
    
    import com.cinc.permissionservice.annotation.CheckNotEmpty;
    import com.cinc.permissionservice.enums.BackResultEnum;
    import com.cinc.permissionservice.exception.BasException;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    
    /**
     * @Author: 
     * @Despriction: 自定义判空注解
     * @CreatedTime: 2019/5/15 11:06
     * @ModifyBy:
     * @ModifyTime:
     * @ModifyDespriction:
     * @Version: V1.0.0
     */
    
    public class CheckNull {
    
        /**
         *
         * @param obj
         * @return
         */
        public static boolean isValid(Object obj) throws Exception {
            // 对指定对象进行空指针校验。返回true表示该对象跟它的每个字段都非空,返回false表示对象为空或者至少一个字段为空
            if (obj == null) {
                throw new BasException(BackResultEnum.PARAMETERERR);
            }
    
            Class cls = obj.getClass();
    
            // 获取对象的所有属性(如果使用getFields,就无法获取到private的属性)
            Field[] fields = cls.getDeclaredFields();
    
            // 依次遍历每个对象属性
            for (Field field : fields) {
                if (field.isAnnotationPresent(CheckNotEmpty.class)) {
                    CheckNotEmpty paramsRequired = field.getAnnotation(CheckNotEmpty.class);
                    if (paramsRequired.requrie()) {
                        Object value = null;
                        if (field != null) {
                            // 将该字段设置为允许访问
                            field.setAccessible(true);
                            // 获取某实例的字段值
                            value = field.get(obj);
                        }
    
                        if (field.getType().getName().equals(
                                "java.util.List")) {
                            if (value == null) {
                                throw new BasException("5000", paramsRequired.message() + "不能为空");
                            } else {
                                ArrayList list = ((ArrayList) value);
                                if (list.size() <= 0) {
                                    throw new BasException("5000", paramsRequired.message() + "不能为空");
                                } else {
                                    for (Object str : list) {
                                        if (str == null || EmptyUtils.isStringEmpty(str.toString())) {
                                            throw new BasException("5000", paramsRequired.message() + "不能为空");
                                        }
                                    }
                                }
    
                            }
                        }
                        else {
                            // 如果发现该字段为空
                            if (value == null) {
                                throw new BasException("5000", paramsRequired.message() + "不能为空");
                            }
                            else if (value != null && EmptyUtils.isStringEmpty(value.toString().trim()))
                            {
                                throw new BasException("5000", paramsRequired.message() + "不能为空");
                            }
                        }
                    }
                }
            }
    
            return true;
        }
    
    }
    

      

    package com.cinc.ecmp.annotation;
    
    import java.lang.annotation.*;
    
    @Documented
    @Target({ ElementType.FIELD }) // 该注解的作用目标是字段(属性)
    @Retention(RetentionPolicy.RUNTIME) // 该注解保留至运行阶段,这样能够通过反射机制调用
    //定义了一个注解,在interface前面加上符号“@”,表示这是个注解
    public @interface CheckNotEmpty {
        /**
         * 必须参数
         * @return
         */
        boolean requrie() default true;
    
        String message() default "";
    }
    

      

  • 相关阅读:
    [转] java代码块 介绍
    win10多用户远程登录
    [读书笔记] learn python the hard way书中 有关powershell 的一些小问题
    Ansible 汇总
    Shell 研究
    Linux CenOS Python3 和 python2 共存
    Ansible安装
    MySQL 5.7.20绿色版安装详细图文教程
    Mysql常用运算符与函数汇总
    mysql 从陌生到熟练之----数据库备份恢复的实现方法
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/11361917.html
Copyright © 2020-2023  润新知