通过反射实现类各字段的判空校验

通过反射实现类各字段的判空校验

package com.example.alldemo.ifelse;

/**
* @Author: 张涛
* @Date: 2020/1/16  13:49
* @Content:
*/
public class User {
   private  String  name;
   private  Integer  age;
   public User() {
   }

   public User(String name) {
       this.name = name;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public Integer getAge() {
       return age;
   }

   public void setAge(Integer age) {
       this.age = age;
   }
}

package com.example.alldemo.ifelse;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author: 张涛
 * @Date: 2020/1/16  10:15
 * @Content:
 */
public class utils {
    /**
     *获取类中所有属性名称
     * @param obj
     * @return
     */
    public static String[] getClassAllAttribute(Object obj) {
        Class classObj = (Class) obj.getClass();
        Field[] fields = classObj.getDeclaredFields();
        String[] title = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            title[i] = fields[i].getName();
        }
        return title;
    }

    /**
     * 属性名称判断是否为必要参数
     * @param fieldName
     * @param obj
     * @param notNullFieldNames
     * @return
     */
    public static Boolean getClassAttributeValue(String fieldName, Object obj, List notNullFieldNames) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = obj.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(obj, new Object[]{});
            if (value == null) {
                for(Object notNullFieldName:notNullFieldNames){
                    if (fieldName.equals(notNullFieldName.toString())) {
                        return false;
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断必要参数是否为空
     * @param user
     * @param notNullFieldNames 必要参数名称数组
     * @return
     */
    public static Boolean classIsNull(User user, List notNullFieldNames) {
        Boolean b = true;
        String[] strings = getClassAllAttribute(user);
        for (String name : strings) {
            b = getClassAttributeValue(name, user, notNullFieldNames);
            if (!b) {
                return false;
            }
        }
        return b;
    }

    public static void main(String[] args) {
        User user = new User("123");
        user.setAge(11);
        List list = new ArrayList();
        list.add("age");
        list.add("name");
        System.out.println(classIsNull(user, list));
    }

}

在list中加入需要验证的字段名称,就可以对字段进行判空了

返回值是一个布尔值,如果验证字段都不为空则为true

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值