java – 递归BeanUtils.describe()

前端之家收集整理的这篇文章主要介绍了java – 递归BeanUtils.describe()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有一个版本的 BeanUtils.describe(customer)递归调用describe()方法对’客户’的复杂属性.
  1. class Customer {
  2.  
  3. String id;
  4. Address address;
  5.  
  6. }

在这里,我想使用describe方法来检索address属性内容.

目前,我可以看到类的名称如下:

  1. {id=123,address=com.test.entities.Address@2a340e}

解决方法

有趣的是,我想使用describe方法来检索嵌套属性内容,我不明白为什么不这样做.我继续前进,滚过我自己.在这里,你可以打电话:
  1. Map<String,String> beanMap = BeanUtils.recursiveDescribe(customer);

一些警告.

>我不知道如何在集合中如何使用BeanUtils格式的属性,所以我用“attribute [index]”去了.
>我不知道如何在地图中格式化属性,所以我用“属性[key]”.
>对于名称冲突,优先级是这样的:第一个属性从超级类的字段,然后是类,然后从getter方法加载.
>我没有分析这种方法性能.如果对象中包含大量集合的对象也包含集合,那么可能会有一些问题.
>这是阿尔法的代码,并不是无法免费的.
>我假设你有最新版本的commons beanutils

此外,fyi,这大概取自于我一直在工作的一个项目,亲切地,java in jails,所以你可以下载它,然后运行:

  1. Map<String,String[]> beanMap = new SimpleMapper().toMap(customer);

虽然,你会注意到它返回一个String [],而不是String,这可能无法满足你的需要.无论如何,下面的代码应该工作,所以有它!

  1. public class BeanUtils {
  2. public static Map<String,String> recursiveDescribe(Object object) {
  3. Set cache = new HashSet();
  4. return recursiveDescribe(object,null,cache);
  5. }
  6.  
  7. private static Map<String,String> recursiveDescribe(Object object,String prefix,Set cache) {
  8. if (object == null || cache.contains(object)) return Collections.EMPTY_MAP;
  9. cache.add(object);
  10. prefix = (prefix != null) ? prefix + "." : "";
  11.  
  12. Map<String,String> beanMap = new TreeMap<String,String>();
  13.  
  14. Map<String,Object> properties = getProperties(object);
  15. for (String property : properties.keySet()) {
  16. Object value = properties.get(property);
  17. try {
  18. if (value == null) {
  19. //ignore nulls
  20. } else if (Collection.class.isAssignableFrom(value.getClass())) {
  21. beanMap.putAll(convertAll((Collection) value,prefix + property,cache));
  22. } else if (value.getClass().isArray()) {
  23. beanMap.putAll(convertAll(Arrays.asList((Object[]) value),cache));
  24. } else if (Map.class.isAssignableFrom(value.getClass())) {
  25. beanMap.putAll(convertMap((Map) value,cache));
  26. } else {
  27. beanMap.putAll(convertObject(value,cache));
  28. }
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. return beanMap;
  34. }
  35.  
  36. private static Map<String,Object> getProperties(Object object) {
  37. Map<String,Object> propertyMap = getFields(object);
  38. //getters take precedence in case of any name collisions
  39. propertyMap.putAll(getGetterMethods(object));
  40. return propertyMap;
  41. }
  42.  
  43. private static Map<String,Object> getGetterMethods(Object object) {
  44. Map<String,Object> result = new HashMap<String,Object>();
  45. BeanInfo info;
  46. try {
  47. info = Introspector.getBeanInfo(object.getClass());
  48. for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
  49. Method reader = pd.getReadMethod();
  50. if (reader != null) {
  51. String name = pd.getName();
  52. if (!"class".equals(name)) {
  53. try {
  54. Object value = reader.invoke(object);
  55. result.put(name,value);
  56. } catch (Exception e) {
  57. //you can choose to do something here
  58. }
  59. }
  60. }
  61. }
  62. } catch (IntrospectionException e) {
  63. //you can choose to do something here
  64. } finally {
  65. return result;
  66. }
  67.  
  68. }
  69.  
  70. private static Map<String,Object> getFields(Object object) {
  71. return getFields(object,object.getClass());
  72. }
  73.  
  74. private static Map<String,Object> getFields(Object object,Class<?> classType) {
  75. Map<String,Object>();
  76.  
  77. Class superClass = classType.getSuperclass();
  78. if (superClass != null) result.putAll(getFields(object,superClass));
  79.  
  80. //get public fields only
  81. Field[] fields = classType.getFields();
  82. for (Field field : fields) {
  83. try {
  84. result.put(field.getName(),field.get(object));
  85. } catch (IllegalAccessException e) {
  86. //you can choose to do something here
  87. }
  88. }
  89. return result;
  90. }
  91.  
  92. private static Map<String,String> convertAll(Collection<Object> values,String key,Set cache) {
  93. Map<String,String> valuesMap = new HashMap<String,String>();
  94. Object[] valArray = values.toArray();
  95. for (int i = 0; i < valArray.length; i++) {
  96. Object value = valArray[i];
  97. if (value != null) valuesMap.putAll(convertObject(value,key + "[" + i + "]",cache));
  98. }
  99. return valuesMap;
  100. }
  101.  
  102. private static Map<String,String> convertMap(Map<Object,Object> values,String>();
  103. for (Object thisKey : values.keySet()) {
  104. Object value = values.get(thisKey);
  105. if (value != null) valuesMap.putAll(convertObject(value,key + "[" + thisKey + "]",cache));
  106. }
  107. return valuesMap;
  108. }
  109.  
  110. private static ConvertUtilsBean converter = BeanUtilsBean.getInstance().getConvertUtils();
  111.  
  112. private static Map<String,String> convertObject(Object value,Set cache) {
  113. //if this type has a registered converted,then get the string and return
  114. if (converter.lookup(value.getClass()) != null) {
  115. String stringValue = converter.convert(value);
  116. Map<String,String> valueMap = new HashMap<String,String>();
  117. valueMap.put(key,stringValue);
  118. return valueMap;
  119. } else {
  120. //otherwise,treat it as a nested bean that needs to be described itself
  121. return recursiveDescribe(value,key,cache);
  122. }
  123. }
  124. }

猜你在找的Java相关文章