Java开发

获取对象属性为null的字段名称

2023-01-30  本文已影响0人  _浅墨_
 /***
     * 获取为null的字段
     * @param source
     * @return java.lang.String[]
     **/
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
            if (srcValue instanceof List && CollectionUtils.isEmpty((Collection<?>)srcValue)) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
上一篇 下一篇

猜你喜欢

热点阅读