Java 实体类必填属性校验工具类

12 篇文章 0 订阅
7 篇文章 0 订阅

来,上车

package com.XXX.common;

import com.XXX.PbxSipAccount;

import javax.validation.constraints.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/** 传入需要校验的实体类对象,检验必填参数是否为空或者null
 * @author xinjian
 * @created 2019/2/27 13:06
 * @description
 */
public class ClassPropertyHandler<T> {

    private Field[] fields = new Field[]{};

    private T t;

    private Class clazz;

    public ClassPropertyHandler(T t){
        this.t = t;
        this.fields = t.getClass().getDeclaredFields();
        this.clazz = t.getClass();
    }
    /**获取所有get方法 方法名
     * @return
     */
    private List<String> getGetMethodName(){
        List<String> list = new ArrayList<>();
        for(Field field : fields){
            //可自定义注解,标识必填字段
            Annotation annotation = field.getAnnotation(NotNull.class);
            if(26 == field.getModifiers() || annotation == null){
                continue;
            }
            char[] cs = field.getName().toCharArray();
            //首字母大写
            cs[0] -= 32;
            list.add("get" + String.valueOf(cs));
        }
        return list;
    }
    /**判断实体类属性值那些必填字段为空
     * @return 返回空表示 必须有值的属性都满足需求 其他看返回值
     */
    public String hasNullOrEmptyPropertyValue(){
        String result = "";
        List<String> list = getGetMethodName();
        for(String methodName : list){
            try {
                Method method = clazz.getMethod(methodName);
                Object res = method.invoke(t,null);
                if(res == null || "".equals(res)){
                    //把 "get"截掉
                    String property = methodName.substring(3);
                    char[] chars = property.toCharArray();
                    //首字母变小写
                    chars[0] += 32;
                    result = result + ", " + String.valueOf(chars);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        if("".equals(result)){
            return result;
        }
        return clazz.getSimpleName() + "的属性: " + result.substring(2,result.length() -1) + " : 值为空或者null";
    }

    public static void main(String[] args) {
        PbxSipAccount account = new PbxSipAccount();
        account.setPassword("111111");
        String res = new ClassPropertyHandler<>(account).hasNullOrEmptyPropertyValue();
        System.out.println(res);
    }
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值