以下:

  • 博主没有大量测试,仅做参考
  • 没有多余包导入
  • 纯使用JDK自带类完成

代码

    /**
     * 验证实体对象是否为空
     * 
     * @param bean
     * @param attributeName
     *            自定义验证的
     */
    public static boolean isEmpty(Object bean, String... attributeName) {
        List<String> list = Arrays.asList(attributeName);
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(bean);
        for (PropertyDescriptor origDescriptor : origDescriptors) {
            String name = origDescriptor.getName();
            if (list.contains(name)) {
                if ("class".equals(name)) {
                    continue;
                }
                if (PropertyUtils.isReadable(bean, name)) {
                    try {
                        Object value = PropertyUtils.getSimpleProperty(bean, name);
                        if (value == null) {
                            continue;
                        } else {
                            return false;
                        }
                    } catch (java.lang.IllegalArgumentException ie) {
                        ;
                    } catch (Exception e) {
                        ;
                    }
                }
            } else {
                continue;
            }
        }
        return true;
    }


    /**
     * 验证实体对象是否为空
     * 如果对象属性为空,则判断该对象为空。
     * 
     * @param bean
     * @return
     */
    public static boolean isEmpty(Object bean) {
        PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(bean);
        for (PropertyDescriptor origDescriptor : origDescriptors) {
            String name = origDescriptor.getName();
            if ("class".equals(name)) {
                continue;
            }
            if (PropertyUtils.isReadable(bean, name)) {
                try {
                    Object value = PropertyUtils.getSimpleProperty(bean, name);
                    if (value == null) {
                        continue;
                    } else {
                        return false;
                    }
                } catch (java.lang.IllegalArgumentException ie) {
                    ;
                } catch (Exception e) {
                    ;
                }
            }
        }
        return true;
    }
学生浅薄,望众师指点

wengang.liu

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-11-27
  • 2021-10-19
  • 2022-01-13
  • 2022-01-06
  • 2021-10-12
猜你喜欢
  • 2021-06-07
  • 2022-02-24
  • 2021-06-14
  • 2021-09-24
  • 2022-02-08
相关资源
相似解决方案