递归BeanUtils.describe()

| 是否存在BeanUtils.describe(customer)的版本,该版本对\'customer \'的复杂属性进行递归调用describe()方法。
class Customer {

String id;
Address address;

}
在这里,我希望describe方法也可以检索address属性的内容。 目前,我所能看到的类名称如下:
{id=123, address=com.test.entities.Address@2a340e}
    
已邀请:
有趣的是,我也想使用describe方法来检索嵌套属性的内容,我不明白为什么不这样做。不过,我继续前进并推出了自己的游戏。在这里,您可以致电:
Map<String,String> beanMap = BeanUtils.recursiveDescribe(customer); 
一些警告。 我不确定BeanUtils如何格式化集合中的属性,因此我使用了“ attribute [index] \”。 我不确定如何格式化地图中的属性,因此我使用了“ attribute [key] \”。 对于名称冲突,优先级是这样的:首先从超类的字段中加载属性,然后从类中加载,然后从getter方法中加载。 我还没有分析这种方法的性能。如果您的对象包含大量对象,这些对象也包含集合,则可能会有一些问题。 这是Alpha代码,不保证没有bug。 我假设您具有Commons beanutils的最新版本 另外,仅供参考,这大致取自我一直在从事的一个项目,该项目被亲切地称为“监狱中的java”,因此您可以下载它然后运行:
Map<String, String[]> beanMap = new SimpleMapper().toMap(customer);
不过,您会注意到它返回的是String [],而不是String,这可能无法满足您的需求。无论如何,下面的代码应该可以正常工作,敬请期待!
public class BeanUtils {
    public static Map<String, String> recursiveDescribe(Object object) {
        Set cache = new HashSet();
        return recursiveDescribe(object, null, cache);
    }

    private static Map<String, String> recursiveDescribe(Object object, String prefix, Set cache) {
        if (object == null || cache.contains(object)) return Collections.EMPTY_MAP;
        cache.add(object);
        prefix = (prefix != null) ? prefix + \".\" : \"\";

        Map<String, String> beanMap = new TreeMap<String, String>();

        Map<String, Object> properties = getProperties(object);
        for (String property : properties.keySet()) {
            Object value = properties.get(property);
            try {
                if (value == null) {
                    //ignore nulls
                } else if (Collection.class.isAssignableFrom(value.getClass())) {
                    beanMap.putAll(convertAll((Collection) value, prefix + property, cache));
                } else if (value.getClass().isArray()) {
                    beanMap.putAll(convertAll(Arrays.asList((Object[]) value), prefix + property, cache));
                } else if (Map.class.isAssignableFrom(value.getClass())) {
                    beanMap.putAll(convertMap((Map) value, prefix + property, cache));
                } else {
                    beanMap.putAll(convertObject(value, prefix + property, cache));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return beanMap;
    }

    private static Map<String, Object> getProperties(Object object) {
        Map<String, Object> propertyMap = getFields(object);
        //getters take precedence in case of any name collisions
        propertyMap.putAll(getGetterMethods(object));
        return propertyMap;
    }

    private static Map<String, Object> getGetterMethods(Object object) {
        Map<String, Object> result = new HashMap<String, Object>();
        BeanInfo info;
        try {
            info = Introspector.getBeanInfo(object.getClass());
            for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
                Method reader = pd.getReadMethod();
                if (reader != null) {
                    String name = pd.getName();
                    if (!\"class\".equals(name)) {
                        try {
                            Object value = reader.invoke(object);
                            result.put(name, value);
                        } catch (Exception e) {
                            //you can choose to do something here
                        }
                    }
                }
            }
        } catch (IntrospectionException e) {
            //you can choose to do something here
        } finally {
            return result;
        }

    }

    private static Map<String, Object> getFields(Object object) {
        return getFields(object, object.getClass());
    }

    private static Map<String, Object> getFields(Object object, Class<?> classType) {
        Map<String, Object> result = new HashMap<String, Object>();

        Class superClass = classType.getSuperclass();
        if (superClass != null) result.putAll(getFields(object, superClass));

        //get public fields only
        Field[] fields = classType.getFields();
        for (Field field : fields) {
            try {
                result.put(field.getName(), field.get(object));
            } catch (IllegalAccessException e) {
                //you can choose to do something here
            }
        }
        return result;
    }

    private static Map<String, String> convertAll(Collection<Object> values, String key, Set cache) {
        Map<String, String> valuesMap = new HashMap<String, String>();
        Object[] valArray = values.toArray();
        for (int i = 0; i < valArray.length; i++) {
            Object value = valArray[i];
            if (value != null) valuesMap.putAll(convertObject(value, key + \"[\" + i + \"]\", cache));
        }
        return valuesMap;
    }

    private static Map<String, String> convertMap(Map<Object, Object> values, String key, Set cache) {
        Map<String, String> valuesMap = new HashMap<String, String>();
        for (Object thisKey : values.keySet()) {
            Object value = values.get(thisKey);
            if (value != null) valuesMap.putAll(convertObject(value, key + \"[\" + thisKey + \"]\", cache));
        }
        return valuesMap;
    }

    private static ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();

    private static Map<String, String> convertObject(Object value, String key, Set cache) {
        //if this type has a registered converted, then get the string and return
        if (converter.lookup(value.getClass()) != null) {
            String stringValue = converter.convert(value);
            Map<String, String> valueMap = new HashMap<String, String>();
            valueMap.put(key, stringValue);
            return valueMap;
        } else {
            //otherwise, treat it as a nested bean that needs to be described itself
            return recursiveDescribe(value, key, cache);
        }
    }
}
    
挑战(或显示障碍)是我们必须处理对象图而不是简单树的问题。图可能包含循环,并且需要为递归算法内部的停止条件开发一些自定义规则或要求。 看一看已死的简单bean(树结构,假定有吸气剂,但未显示):
public class Node {
   private Node parent;
   private Node left;
   private Node right;
}
并像这样初始化它:
        root
        /  \\
       A    B
现在在ѭ7上调用描述。非递归调用将导致
{parent=null, left=A, right=B}
递归调用会执行
1: describe(root) =>
2: {parent=describe(null), left=describe(A), right=describe(B)} =>
3: {parent=null, 
     {A.parent=describe(root), A.left=describe(null), A.right= describe(null)}
     {B.parent=describe(root), B.left=describe(null), B.right= describe(null)}}
并碰到一个“ 10”,因为describe被对象root,A和B一遍又一遍地调用。 自定义实现的一种解决方案可能是记住到目前为止已描述的所有对象(将这些实例记录在集合中,如果set.contains(bean)返回true则停止)并将某种链接存储在结果对象中。     
您可以从相同的commom-beanutils中简单使用:
Map<String, Object> result = PropertyUtils.describe(obj);
返回指定Bean提供读取方法的整个属性集。     

要回复问题请先登录注册