0abf1681846a7332c555059951f5ea6519e85925.svn-base 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.sinosoft.lz.system.util;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. /**
  5. *
  6. * @author zhangdapeng
  7. * @version 1.0,2014年5月13日
  8. * @since 1.0
  9. */
  10. public class NodeWorker{
  11. /**
  12. * 自身id
  13. */
  14. private String id;
  15. /**
  16. * 父id
  17. */
  18. private String pid;
  19. /**
  20. * 节点内容
  21. */
  22. private Object data;
  23. /**
  24. * 孩子节点列表
  25. */
  26. private ChildrenWorker children = new ChildrenWorker();
  27. public NodeWorker() {
  28. }
  29. public String getId() {
  30. return id;
  31. }
  32. public void setId(String id) {
  33. this.id = id;
  34. }
  35. public String getPid() {
  36. return pid;
  37. }
  38. public void setPid(String pid) {
  39. this.pid = pid;
  40. }
  41. public Object getData() {
  42. return data;
  43. }
  44. public void setData(Object data) {
  45. this.data = data;
  46. }
  47. // 先序遍历,拼接JSON字符串
  48. public String toString() {
  49. Class<? extends Object> clazz = data.getClass();
  50. Field[] field = clazz.getDeclaredFields();
  51. String name = null;
  52. String value = null;
  53. StringBuilder result = new StringBuilder();
  54. for (int i = 0; i < field.length; i++) {
  55. name = field[i].getName();
  56. value = (String) invokeGet(data, name);
  57. if (i != 0) {
  58. result.append(",").append('"' + name + '"' + ":" + '"' + value + '"');
  59. } else
  60. result.append("{").append('"' + name + '"' + ":" + '"' + value + '"');
  61. }
  62. if (children != null && children.getSize() != 0) {
  63. result.append(",").append('"').append("c").append('"').append(":")
  64. .append(children.toString());
  65. } else {
  66. // result.append(",").append('"' + "leaf" + '"' + ":" + '"' + "true"
  67. // + '"');
  68. }
  69. return result.append("}").toString();
  70. }
  71. /**
  72. * java反射bean的get方法
  73. *
  74. * @param objectClass
  75. * @param fieldName
  76. * @return
  77. */
  78. @SuppressWarnings({ "unchecked", "rawtypes" })
  79. public Method getGetMethod(Class objectClass, String fieldName) {
  80. StringBuffer sb = new StringBuffer();
  81. sb.append("get");
  82. sb.append(fieldName.substring(0, 1).toUpperCase());
  83. sb.append(fieldName.substring(1));
  84. try {
  85. return objectClass.getMethod(sb.toString());
  86. } catch (Exception e) {
  87. }
  88. return null;
  89. }
  90. /**
  91. * 执行get方法
  92. *
  93. * @param o
  94. * 执行对象
  95. * @param fieldName
  96. * 属性
  97. */
  98. public Object invokeGet(Object o, String fieldName) {
  99. Method method = getGetMethod(o.getClass(), fieldName);
  100. try {
  101. return method.invoke(o, new Object[0]);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. // 添加孩子节点
  108. public void addChild(NodeWorker node) {
  109. this.children.addChild(node);
  110. }
  111. }