自定义判空注解

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 "";
}

  

posted @ 2019-08-16 09:30  丿少女梦丶  阅读(465)  评论(0编辑  收藏  举报