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 com.db4o.foundation.*;
024:
025: /**
026: * simple annotated stack trace for debugging
027: *
028: * @exclude
029: */
030: public class StackRecorder {
031:
032: // exclude addCase()/record() + newTrace()
033:
034: private static final int EXCLUDEDEPTH = 2;
035:
036: private static StackTrace _curTrace;
037:
038: private static Collection4 _traces = new Collection4();
039:
040: private static Collection4 _includes;
041:
042: private static Collection4 _excludes;
043:
044: public static void addCase(String caseInfo) {
045: _curTrace = newTrace(caseInfo);
046: }
047:
048: public static void addInclude(String classNameSubstring) {
049: if (_includes == null) {
050: _includes = new Collection4();
051: }
052: _includes.add(classNameSubstring);
053: }
054:
055: public static void addExclude(String classNameSubstring) {
056: if (_excludes == null) {
057: _excludes = new Collection4();
058: }
059: _excludes.add(classNameSubstring);
060: }
061:
062: public static void record() {
063: StackTrace trace = newTrace(null);
064: if (!_traces.contains(trace)) {
065: _traces.add(trace);
066: }
067: }
068:
069: public static void record(Object obj) {
070: if (obj == null) {
071: return;
072: }
073: Class clazz = (obj instanceof Class) ? (Class) obj : obj
074: .getClass();
075: if (recordClass(clazz)) {
076: record();
077: }
078: }
079:
080: private static boolean recordClass(Class clazz) {
081:
082: String className = clazz.getName();
083:
084: if (_excludes != null) {
085: Iterator4 i = _excludes.iterator();
086: while (i.moveNext()) {
087: String name = (String) i.current();
088: if (className.indexOf(name) >= 0) {
089: return false;
090: }
091: }
092: }
093:
094: boolean onNotFound = true;
095:
096: if (_includes != null) {
097: onNotFound = false;
098: Iterator4 i = _includes.iterator();
099: while (i.moveNext()) {
100: String name = (String) i.current();
101: if (className.indexOf(name) >= 0) {
102: onNotFound = true;
103: }
104: }
105: }
106:
107: Class claxx = clazz.getSuperclass();
108: if (claxx != null) {
109: if (recordClass(claxx)) {
110: return true;
111: }
112: }
113:
114: return onNotFound;
115: }
116:
117: public static void logAll() {
118: Iterator4 iter = _traces.iterator();
119: while (iter.moveNext()) {
120: System.out.println(iter.current());
121: if (iter.moveNext()) {
122: System.out.println("\n---\n");
123: }
124: }
125: }
126:
127: private static StackTrace newTrace(String caseInfo) {
128: return new StackTrace(EXCLUDEDEPTH, caseInfo, _curTrace);
129: }
130:
131: public static void main(String[] args) {
132: for (int i = 0; i < 2; i++) {
133: for (int j = 0; j < 2; j++) {
134: StackRecorder.addCase("main" + i);
135: foo();
136: }
137: }
138: for (int i = 0; i < 2; i++) {
139: for (int j = 0; j < 2; j++) {
140: StackRecorder.addCase("mainX" + i);
141: foo();
142: }
143: }
144: StackRecorder.logAll();
145: }
146:
147: public static void foo() {
148: for (int i = 0; i < 2; i++) {
149: for (int j = 0; j < 2; j++) {
150: StackRecorder.addCase("foo" + i);
151: bar();
152: }
153: }
154: }
155:
156: public static void bar() {
157: for (int i = 0; i < 2; i++) {
158: StackRecorder.record();
159: }
160: }
161: }
|