判斷l(xiāng)ist集合中對象的屬性是否為空

自定義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;
    }
}

測試實現(xiàn)方法

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);

        //判斷l(xiāng)ist對象中那些屬性為空
        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);
        }
    }
}

打印結果:
對象缺失信息:年齡為空; 住址為空; 公司為空;

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容