001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.util;
022:
023: import java.io.*;
024: import java.lang.reflect.*;
025: import java.util.*;
026:
027: import com.db4o.internal.*;
028: import com.db4o.test.legacy.soda.*;
029:
030: public class TLogger {
031: private static int maximumDepth = Integer.MAX_VALUE;
032: private static PrintStream out = System.out;
033: private static String cr = "";
034: private static String sp = " ";
035: private static boolean silent = false;
036:
037: public static void log(Object a_object) {
038: if (a_object == null) {
039: log("[NULL]");
040: } else {
041: log(a_object.getClass().getName());
042: log(a_object, 0, new Stack());
043: }
044: }
045:
046: public static void setOut(PrintStream ps) {
047: out = ps;
048: }
049:
050: public static void setMaximumDepth(int depth) {
051: maximumDepth = depth;
052: }
053:
054: public static void setSilent(boolean flag) {
055: silent = flag;
056: }
057:
058: private static void log(Object a_object, int a_depth, Stack a_stack) {
059: if (a_object instanceof SodaTest) {
060: return;
061: }
062: if (a_stack.contains(a_object) || a_depth > maximumDepth) {
063: return;
064: }
065: Class clazz = a_object.getClass();
066: for (int i = 0; i < ignore.length; i++) {
067: if (clazz.isAssignableFrom(ignore[i])) {
068: return;
069: }
070: }
071:
072: a_stack.push(a_object);
073:
074: Class[] classes = getClassHierarchy(a_object);
075:
076: String spaces = "";
077: for (int i = classes.length - 1; i >= 0; i--) {
078: spaces = spaces + sp;
079:
080: String className = spaces;
081: int pos = classes[i].getName().lastIndexOf(".");
082: if (pos > 0) {
083: className += classes[i].getName().substring(pos);
084: } else {
085: className += classes[i].getName();
086: }
087:
088: if (classes[i] == Date.class) {
089: String fieldName = className + ".getTime";
090: Object obj = new Long(((Date) a_object).getTime());
091: log(obj, Long.class, fieldName, a_depth + 1, -1,
092: a_stack);
093:
094: } else {
095: Field[] fields = classes[i].getDeclaredFields();
096: for (int j = 0; j < fields.length; j++) {
097:
098: Platform4.setAccessible(fields[j]);
099:
100: String fieldName = className + "."
101: + fields[j].getName();
102:
103: try {
104: Object obj = fields[j].get(a_object);
105:
106: if (obj.getClass().isArray()) {
107: obj = normalizeNArray(obj);
108:
109: int len = Array.getLength(obj);
110: for (int k = 0; k < len; k++) {
111: Object element = Array.get(obj, k);
112: Class arrClass = element == null ? null
113: : element.getClass();
114: log(element, arrClass, fieldName,
115: a_depth + 1, k, a_stack);
116: }
117: } else {
118: log(obj, fields[j].getType(), fieldName,
119: a_depth + 1, -1, a_stack);
120: }
121: } catch (Exception e) {
122:
123: }
124: }
125: }
126: }
127: }
128:
129: private static void log(Object a_object, Class a_Class,
130: String a_fieldName, int a_depth, int a_arrayElement,
131: Stack a_stack) {
132: if (a_depth > maximumDepth) {
133: return;
134: }
135: String fieldName = (a_arrayElement > -1) ? a_fieldName + sp
136: + sp + a_arrayElement : a_fieldName;
137: if (a_object != null) {
138: log(a_depth, fieldName, "");
139: Class clazz = a_object.getClass();
140: if (Platform4.isSimple(clazz)) {
141: log(a_depth + 1, a_object.getClass().getName(),
142: a_object.toString());
143: } else {
144: log(a_object, a_depth, a_stack);
145: }
146: } else {
147: log(a_depth, fieldName, "[NULL]");
148: }
149: }
150:
151: private static void log(String a_msg) {
152: if (!silent) {
153: out.println(a_msg + cr);
154: }
155: }
156:
157: private static void log(int indent, String a_property,
158: String a_value) {
159: for (int i = 0; i < indent; i++) {
160: a_property = sp + sp + a_property;
161: }
162: log(a_property, a_value);
163: }
164:
165: private static void log(String a_property, String a_value) {
166: if (a_value == null)
167: a_value = "[NULL]";
168: log(a_property + ": " + a_value);
169: }
170:
171: // private static void log(Exception e, Object obj, String msg) {
172: // String l_msg;
173: // if (e != null) {
174: // l_msg = "!!! " + e.getClass().getName();
175: // String l_exMsg = e.getMessage();
176: // if (l_exMsg != null) {
177: // l_msg += sp + l_exMsg;
178: // }
179: // } else {
180: // l_msg = "!!!Exception log";
181: // }
182: // if (obj != null) {
183: // l_msg += " in " + obj.getClass().getName();
184: // }
185: // if (msg != null) {
186: // l_msg += sp + msg;
187: // }
188: // log(l_msg);
189: // }
190:
191: private static Class[] getClassHierarchy(Object a_object) {
192: Class[] classes = new Class[] { a_object.getClass() };
193: return getClassHierarchy(classes);
194: }
195:
196: private static Class[] getClassHierarchy(Class[] a_classes) {
197: Class clazz = a_classes[a_classes.length - 1].getSuperclass();
198: if (clazz.equals(Object.class)) {
199: return a_classes;
200: }
201: Class[] classes = new Class[a_classes.length + 1];
202: System.arraycopy(a_classes, 0, classes, 0, a_classes.length);
203: classes[a_classes.length] = clazz;
204: return getClassHierarchy(classes);
205: }
206:
207: static Object normalizeNArray(Object a_object) {
208: if (Array.getLength(a_object) > 0) {
209: Object first = Array.get(a_object, 0);
210: if (first != null && first.getClass().isArray()) {
211: int dim[] = arrayDimensions(a_object);
212: Object all = new Object[arrayElementCount(dim)];
213: normalizeNArray1(a_object, all, 0, dim, 0);
214: return all;
215: }
216: }
217: return a_object;
218: }
219:
220: static int normalizeNArray1(Object a_object, Object a_all,
221: int a_next, int a_dim[], int a_index) {
222: if (a_index == a_dim.length - 1) {
223: for (int i = 0; i < a_dim[a_index]; i++) {
224: Array.set(a_all, a_next++, Array.get(a_object, i));
225: }
226: } else {
227: for (int i = 0; i < a_dim[a_index]; i++) {
228: a_next = normalizeNArray1(Array.get(a_object, i),
229: a_all, a_next, a_dim, a_index + 1);
230: }
231:
232: }
233: return a_next;
234: }
235:
236: static int[] arrayDimensions(Object a_object) {
237: int count = 0;
238: for (Class clazz = a_object.getClass(); clazz.isArray(); clazz = clazz
239: .getComponentType()) {
240: count++;
241: }
242: int dim[] = new int[count];
243: for (int i = 0; i < count; i++) {
244: dim[i] = Array.getLength(a_object);
245: a_object = Array.get(a_object, 0);
246: }
247: return dim;
248: }
249:
250: static int arrayElementCount(int a_dim[]) {
251: int elements = a_dim[0];
252: for (int i = 1; i < a_dim.length; i++) {
253: elements *= a_dim[i];
254: }
255: return elements;
256: }
257:
258: private static final Class[] ignore = { Class.class };
259:
260: }
|