判断model 中属性是否全为空

反射就是运行时,改变类的结构

/**
     * 判断model 中属性是否全为空
     * @return
     */
    @JsonIgnore
    public Boolean isEmpty(){
        int fieldLength = this.getClass().getDeclaredFields().length;
        int count = 0;
        for (Field f : this.getClass().getDeclaredFields()) {
        //设置访问性,反射类的方法,设置为true就可以访问private修饰的东西,否则无法访问
            f.setAccessible(true);
            try {
                if (f.get(this) == null) { //判断字段是否为空,并且对象属性中的基本都会转为对象类型来判断
                    count++;
                }
            } catch (IllegalAccessException e) {
                return true;
            }
        }
        if (count >= fieldLength) {
            return true;
        }
        return false;
    }

你可能感兴趣的:(反射相关)