1 package org.altusmetrum.AltosDroid;
3 import java.lang.reflect.Array;
4 import java.lang.reflect.Field;
5 import java.util.HashMap;
8 private static Dumper instance = new Dumper();
10 protected static Dumper getInstance() {
16 int maxArrayElements = 0;
18 HashMap<String, String> ignoreList = new HashMap<String, String>();
19 HashMap<Object, Integer> visited = new HashMap<Object, Integer>();
22 public static String dump(Object o) {
23 return dump(o, 0, 0, null);
26 public static String dump(Object o, int maxDepth, int maxArrayElements, String[] ignoreList) {
27 DumpContext ctx = Dumper.getInstance().new DumpContext();
28 ctx.maxDepth = maxDepth;
29 ctx.maxArrayElements = maxArrayElements;
31 if (ignoreList != null) {
32 for (int i = 0; i < Array.getLength(ignoreList); i++) {
33 int colonIdx = ignoreList[i].indexOf(':');
35 ignoreList[i] = ignoreList[i] + ":";
36 ctx.ignoreList.put(ignoreList[i], ignoreList[i]);
43 protected static String dump(Object o, DumpContext ctx) {
49 StringBuffer tabs = new StringBuffer();
50 for (int k = 0; k < ctx.callCount; k++) {
53 StringBuffer buffer = new StringBuffer();
54 @SuppressWarnings("rawtypes")
55 Class oClass = o.getClass();
57 String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass);
59 if (ctx.ignoreList.get(oSimpleName + ":") != null)
62 if (oClass.isArray()) {
64 buffer.append(tabs.toString().substring(1));
66 int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o) : Math.min(ctx.maxArrayElements, Array.getLength(o));
67 for (int i = 0; i < rowCount; i++) {
68 buffer.append(tabs.toString());
70 Object value = Array.get(o, i);
71 buffer.append(dumpValue(value, ctx));
72 } catch (Exception e) {
73 buffer.append(e.getMessage());
75 if (i < Array.getLength(o) - 1)
79 if (rowCount < Array.getLength(o)) {
80 buffer.append(tabs.toString());
81 buffer.append(Array.getLength(o) - rowCount + " more array elements...");
84 buffer.append(tabs.toString().substring(1));
88 buffer.append(tabs.toString().substring(1));
90 buffer.append(tabs.toString());
91 buffer.append("hashCode: " + o.hashCode());
93 while (oClass != null && oClass != Object.class) {
94 Field[] fields = oClass.getDeclaredFields();
96 if (ctx.ignoreList.get(oClass.getSimpleName()) == null) {
97 if (oClass != o.getClass()) {
98 buffer.append(tabs.toString().substring(1));
99 buffer.append(" Inherited from superclass " + oSimpleName + ":\n");
102 for (int i = 0; i < fields.length; i++) {
104 String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType());
105 String fName = fields[i].getName();
107 fields[i].setAccessible(true);
108 buffer.append(tabs.toString());
109 buffer.append(fName + "(" + fSimpleName + ")");
112 if (ctx.ignoreList.get(":" + fName) == null &&
113 ctx.ignoreList.get(fSimpleName + ":" + fName) == null &&
114 ctx.ignoreList.get(fSimpleName + ":") == null) {
117 Object value = fields[i].get(o);
118 buffer.append(dumpValue(value, ctx));
119 } catch (Exception e) {
120 buffer.append(e.getMessage());
124 buffer.append("<Ignored>");
128 oClass = oClass.getSuperclass();
129 oSimpleName = oClass.getSimpleName();
135 buffer.append(tabs.toString().substring(1));
139 return buffer.toString();
142 protected static String dumpValue(Object value, DumpContext ctx) {
146 if (value.getClass().isPrimitive() ||
147 value.getClass() == java.lang.Short.class ||
148 value.getClass() == java.lang.Long.class ||
149 value.getClass() == java.lang.String.class ||
150 value.getClass() == java.lang.Integer.class ||
151 value.getClass() == java.lang.Float.class ||
152 value.getClass() == java.lang.Byte.class ||
153 value.getClass() == java.lang.Character.class ||
154 value.getClass() == java.lang.Double.class ||
155 value.getClass() == java.lang.Boolean.class) {
157 return value.toString();
161 Integer visitedIndex = ctx.visited.get(value);
162 if (visitedIndex == null) {
163 ctx.visited.put(value, ctx.callCount);
164 if (ctx.maxDepth == 0 || ctx.callCount < ctx.maxDepth) {
165 return dump(value, ctx);
167 return "<Reached max recursion depth>";
170 return "<Previously visited - see hashCode " + value.hashCode() + ">";
176 private static String getSimpleNameWithoutArrayQualifier(@SuppressWarnings("rawtypes") Class clazz) {
177 String simpleName = clazz.getSimpleName();
178 int indexOfBracket = simpleName.indexOf('[');
179 if (indexOfBracket != -1)
180 return simpleName.substring(0, indexOfBracket);