判断一个java对象中的属性值是否为空(批量判断)

有时候数据库中的某些字段值要求不为空,所以代码中要判断这些字段对应的属性值是否为空,当对象属性过多时,一个一个属性去判断,会显得代码冗余,所以,可以借助工具类


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IsNull {
    //整个类都校验
    public static List validateProperty(Object validateObj) {
        return validateProperty(validateObj,(String[])null);
    }
    //类中的某些字段不校验
    public static List validateProperty(Object validateObj,String... ignoreProperties) {
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(validateObj.getClass());
        List ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
        List errList = new ArrayList<>();
        for (PropertyDescriptor targetPd : targetPds) {
            Method readMethod = targetPd.getReadMethod();
            if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                try {
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    Object value = readMethod.invoke(validateObj);
                    if (value instanceof String) {
                        if (StringUtils.isEmpty((String) value)) {
                            errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能为空");
                            continue;
                        }
                    }
                    if (value instanceof Float || value instanceof Integer) {
                        if (StringUtils.isEmpty(value.toString())) {
                            errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能为空");
                            continue;
                        }
                    }
                    if (value == null) {
                        errList.add(validateObj.getClass().getSimpleName() + "中的" + targetPd.getName() + "不能为空");
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException(
                            "Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                }
            }
        }
        return errList;
    }

}

之后对拿到的数据进行业务判断

你可能感兴趣的:(Java)