美文网首页
判断list集合中对象的属性是否为空

判断list集合中对象的属性是否为空

作者: 小石读史 | 来源:发表于2022-11-15 09:54 被阅读0次

    自定义CheckNull注解

    import java.lang.annotation.*;
    
    /**
     * 校验为空的字段
     */
    @Documented
    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface CheckNull {
        String message() default "";
    }
    

    在对象中对需要校验空的属性加上CheckNull 注解

    
    
    import java.io.Serializable;
    
    /**
     * @author hjh
     */
    public class Person implements Serializable {
    
        private static final long serialVersionUID = 5074596588311962143L;
    
    
        @CheckNull(message = "姓名为空")
        private String name;
    
    
        @CheckNull(message = "手机号为空")
        private String phone;
    
    
        private Integer sex;
    
        @CheckNull(message = "年龄为空")
        private Integer age;
    
        @CheckNull(message = "住址为空")
        private String address;
    
        @CheckNull(message = "公司为空")
        private String company;
    
    
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public Integer getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getCompany() {
            return company;
        }
    
        public void setCompany(String company) {
            this.company = company;
        }
    }
    
    

    测试实现方法

    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    public class CheckNullTest {
    
        public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    
            List<Person> personList = new ArrayList<>();
            Person person1 = new Person();
            //只设置名字和手机号
            person1.setName("小明");
            person1.setPhone("13847115418");
            person1.setAddress("");
            personList.add(person1);
    
            //判断list对象中那些属性为空
            Field[] fields = Person.class.getDeclaredFields();
            for(Person person : personList){
                StringBuilder errorMessage = new StringBuilder();
                for (Field field : fields) {
                    if(field.isAnnotationPresent(CheckNull.class)) {
                        field.setAccessible(true);
                        CheckNull annotation = field.getAnnotation(CheckNull.class);
                        if(field.get(person) == null){
                            errorMessage.append(annotation.message() + "; ");
                        }
                        //字符型长度为0
                        if(field.getType().equals(String.class)){
                            Method getLength = String.class.getMethod("length");
                            if(field.get(person) != null && getLength.invoke(field.get(person)).equals(0)){
                                errorMessage.append(annotation.message() + "; ");
                            }
                        }
                    }
                }
                System.out.println("对象缺失信息:"+errorMessage);
            }
        }
    }
    
    

    打印结果:
    对象缺失信息:年龄为空; 住址为空; 公司为空;

    相关文章

      网友评论

          本文标题:判断list集合中对象的属性是否为空

          本文链接:https://www.haomeiwen.com/subject/oxvsxdtx.html