1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.jot.logger; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Enumeration; import java.util.Map; /** * Dumps an whole object hierarchy (fileds, values.. recursively) * Useful for debugging. * * Note: this is all static. * @author thibautc */ public class JOTObjectDumper { public static int maxRecursiveDepth = 30; public static final String TAB = " "; public static int getMaxRecursiveDepth() { return maxRecursiveDepth; } /** * Default: 15 * set to prevent infinite loops * @param maxRecursiveDepth */ public static void setMaxRecursiveDepth(int maxRecursiveDepth) { JOTObjectDumper.maxRecursiveDepth = maxRecursiveDepth; } public static String dump(Object o) { StringBuffer buffer = new StringBuffer(); dumpToSB(o, buffer, "", null); return buffer.toString(); } private static void dumpToSB(Object o, StringBuffer buffer, String padding, String fieldName) { if (padding.length() > 0 && padding.length() / TAB.length() > maxRecursiveDepth) { buffer.append("\n ... More Avail - To deep recursion ... \n"); return; } buffer.append(padding); if (fieldName != null) { buffer.append(fieldName).append(" = "); } if (o == null) { buffer.append("null").append("\n"); return; } Class clazz = o.getClass(); if (sameClass(clazz, Class.class) || sameClass(clazz, Object.class)) { // we don't want to log all the Object/Class internals. buffer.append("\n"); return; } boolean primitive = clazz.isPrimitive() || sameClass(clazz, String.class) || sameClass(clazz, StringBuffer.class) || o instanceof Number; if (primitive) { buffer.append(o.toString()).append("\n"); return; } if (o instanceof Collection) { Collection c = (Collection) o; buffer.append("Collection(").append(clazz.getName()).append(")\n"); Object[] array = c.toArray(); for (int i = 0; i != array.length; i++) { dumpToSB(array[i], buffer, padding + TAB, null); } return; } if (o instanceof Map) { Map m = (Map) o; buffer.append("Map(").append(clazz.getName()).append(")\n"); Object[] array = m.keySet().toArray(); for (int i = 0; i != array.length; i++) { buffer.append(padding + TAB).append(array[i].toString()).append(" => \n"); dumpToSB(m.get(array[i]), buffer, padding + TAB + TAB, null); } return; } if (o instanceof Enumeration) { Enumeration e = (Enumeration) o; buffer.append("Enumeration(").append(clazz.getName()).append(")\n"); while (e.hasMoreElements()) { dumpToSB(e.nextElement(), buffer, padding + TAB, null); } return; } if (clazz.isArray()) { buffer.append("Array(").append(clazz.getName()).append(")\n"); for (int i = 0; i != Array.getLength(o); i++) { Object o2 = Array.get(o, i); dumpToSB(o2, buffer, padding + TAB, null); } return; } buffer.append("Object(").append(clazz.getName()).append(")\n"); // Go through the class fields doFields(clazz, o, buffer, padding); // go through the object superclass(es) fields clazz=clazz.getSuperclass(); while(clazz!=null) { doFields(clazz, o, buffer, padding); clazz=clazz.getSuperclass(); } } private static void doFields(Class clazz, Object o, StringBuffer buffer, String padding) { Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i != fields.length; i++) { int modifiers = fields[i].getModifiers(); fields[i].setAccessible(true); try { if (!(Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers)) && !fields[i].getName().startsWith("this$")) { // do not bother listing constants // also ignore inner classes whihc cause issues Object o2 = fields[i].get(o); //System.out.println(fields[i].getName()); dumpToSB(o2, buffer, padding + TAB, fields[i].getName()); } } catch (IllegalAccessException e) { } } } private static boolean sameClass(Class clazz, Class clazz2) { return clazz.getName().equals(clazz2.getName()); } class TestObject2 { String innerVal="innerVal"; } }