zoukankan      html  css  js  c++  java
  • 判断Java实体对象为空

    判断Java实体对象为空

    以下:

    • 博主没有大量测试,仅做参考
    • 没有多余包导入
    • 纯使用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
    学生浅薄 望众师指点
  • 相关阅读:
    根据当前日期转目的国地区时间戳
    时间戳转换作用域问题
    字符串拼接问题
    input全选和取消全选
    循环遍历渲染模块
    jQuery实现获取选中复选框的值
    React组件
    underscore.js依赖库函数分析二(查找)
    underscore.js依赖库函数分析一(遍历)
    React入门
  • 原文地址:https://www.cnblogs.com/Nihility/p/14695666.html
Copyright © 2011-2022 走看看