Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Modifier;
  4. import java.util.Iterator;
  5.  
  6. public class ObjectDumper {
  7.  
  8. private static final String NULL = "NULL";
  9. private static final String EMPTY = "";
  10. private static final String OBJECT_SUFFIX = "}";
  11. private static final String OBJECT_PREFIX = "%s{";
  12. private static final String FIELD_OPERAND = "=";
  13. private static final String ERROR = "<ERROR>";
  14. private static final String CYCLE_FOUND = "<CYCLE FOUND>";
  15. private static final String TUPLE_SEPARATOR = ",";
  16. private static final String TUPLE_BRACKET_CLOSE = "]";
  17. private static final String TUPLE_BLACKET_OPEN = "[";
  18. private static final String DOUBLE_QUOTES = "\"";
  19.  
  20. public static String dump(Object object) {
  21. return dump(object, 5);
  22. }
  23.  
  24. private static String dump(Object object, int maxDepth) {
  25. return dump(object, 0, maxDepth);
  26. }
  27.  
  28. private static String dump(Object object, int currentDepth, int maxDepth) {
  29. if (object == null) {
  30. return NULL;
  31. }
  32. Class<?> type = object.getClass();
  33. if (isWritable(type)) {
  34. return DOUBLE_QUOTES + String.valueOf(object) + DOUBLE_QUOTES;
  35. } else if (Iterable.class.isAssignableFrom(type)) {
  36. Iterator<?> iterator = ((Iterable<?>) object).iterator();
  37. StringBuilder subBuilder = new StringBuilder(TUPLE_BLACKET_OPEN);
  38. while (iterator.hasNext()) {
  39. subBuilder.append(subBuilder.length() > 1 ? TUPLE_SEPARATOR : EMPTY).append(dump(iterator.next(), currentDepth + 1, maxDepth));
  40. }
  41. return subBuilder.append(TUPLE_BRACKET_CLOSE).toString();
  42. } else if (type.isArray()) {
  43. StringBuilder subBuilder = new StringBuilder(TUPLE_BLACKET_OPEN);
  44. for (int i = 0; i < Array.getLength(object); i++) {
  45. subBuilder.append(subBuilder.length() > 1 ? TUPLE_SEPARATOR : EMPTY).append(dump(Array.get(object, i), currentDepth + 1, maxDepth));
  46. }
  47. return subBuilder.append(TUPLE_BRACKET_CLOSE).toString();
  48. } else {
  49. if (currentDepth == maxDepth) {
  50. return String.valueOf(object);
  51. } else {
  52. StringBuilder contents = new StringBuilder();
  53. contents = contents.append(String.format(OBJECT_PREFIX, type.getName()));
  54.  
  55. Class<?> tempType = type;
  56. while (tempType != null && tempType != Object.class) {
  57. for (Field field : tempType.getDeclaredFields()) {
  58. boolean fieldAccessibility = field.isAccessible();
  59. try {
  60. if (!field.isAccessible()) {
  61. field.setAccessible(true);
  62. }
  63. if (!Modifier.isFinal(field.getModifiers())) {
  64. try {
  65. Object fieldValue = field.get(object);
  66. contents = contents.append(contents.indexOf(FIELD_OPERAND) > 1 ? TUPLE_SEPARATOR : EMPTY).append(field.getName())
  67. .append(FIELD_OPERAND);
  68. contents.append(System.identityHashCode(fieldValue) != System.identityHashCode(object) ? dump(fieldValue,
  69. currentDepth + 1, maxDepth) : CYCLE_FOUND);
  70. } catch (Exception e) {
  71. contents = contents.append(ERROR);
  72. }
  73. }
  74. } finally {
  75. field.setAccessible(fieldAccessibility);
  76. }
  77. }
  78. tempType = tempType.getSuperclass();
  79. }
  80. return contents.append(OBJECT_SUFFIX).toString();
  81. }
  82. }
  83. }
  84.  
  85. private static boolean isWritable(Class<?> type) {
  86. if (type.isPrimitive() || type.isEnum() || String.class.isAssignableFrom(type)) {
  87. return true;
  88. }
  89. if (type == java.lang.Long.class || type == java.lang.Integer.class || type == java.lang.Boolean.class || type == java.lang.String.class
  90. || type == java.lang.Double.class || type == java.lang.Short.class || type == java.lang.Byte.class) {
  91. return true;
  92. }
  93. return false;
  94. }
  95.  
  96. public static void main(String[] args) {
  97. System.out.println(ObjectDumper.dump(new Exception()));
  98. }
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement