自定义参数非空判断

这篇博客介绍了如何在Java中实现参数校验,利用注解`ParamCheck`检查对象字段是否为空,通过反射访问私有变量并进行验证。此外,还展示了如何读取方法上的注解信息。这对于确保程序的健壮性和输入数据的有效性至关重要。
摘要由CSDN通过智能技术生成
package com.moxi.mougblog.base.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

@Data
@Slf4j
public class IsEmptyUtil {
    @ParamCheck(emptyAble = ParamCheck.CheckEnum.NOTEMPTY)
    private String name;

    //判断参数是否为空
    public static boolean paramCheck(Object obj) {
        try {
            Class<?> clazz = obj.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);//设置为true才可以访问私有变量
                ParamCheck paramCheck = field.getAnnotation(ParamCheck.class);
                if (paramCheck != null) {
                    if (paramCheck.emptyAble() == ParamCheck.CheckEnum.NOTEMPTY) {
                        Object val = field.get(obj);
                        if (val == null || "".equals(val.toString())) {
                            log.info(field.getName()+"是空的");
                            return false;
                        }
                    }
                }
            }
        } catch (Exception ex) {
            log.info("Param verify error");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        new IsEmptyUtil().paramCheck(new IsEmptyUtil());
    }
}

package com.moxi.mougblog.base.util;

import java.lang.annotation.*;

/**
 * 参数校验注解
 *
 * @author yangyongjie
 * @date 2020/5/14
 * @desc
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ParamCheck {

    /**
     * 是否允许为空
     */
    CheckEnum emptyAble() default CheckEnum.EMPTY;

    enum CheckEnum {
        NOTEMPTY,
        EMPTY
    }

}

获取属性的注解

public class TestAnnotation {
    public static void main(String[] args){
        try {
            //获取Student的Class对象
            Class stuClass = Class.forName("pojos.Student");

            //说明一下,这里形参不能写成Integer.class,应写为int.class
            Method stuMethod = stuClass.getMethod("study",int.class);

            if(stuMethod.isAnnotationPresent(CherryAnnotation.class)){
                System.out.println("Student类上配置了CherryAnnotation注解!");
                //获取该元素上指定类型的注解
                CherryAnnotation cherryAnnotation = stuMethod.getAnnotation(CherryAnnotation.class);
                System.out.println("name: " + cherryAnnotation.name() + ", age: " + cherryAnnotation.age()
                    + ", score: " + cherryAnnotation.score()[0]);
            }else{
                System.out.println("Student类上没有配置CherryAnnotation注解!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值