KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > lib > StackRecorder


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.lib;
22
23 import com.db4o.foundation.*;
24
25 /**
26  * simple annotated stack trace for debugging
27  *
28  * @exclude
29  */

30 public class StackRecorder {
31     
32     // exclude addCase()/record() + newTrace()
33

34     private static final int EXCLUDEDEPTH = 2;
35     
36     private static StackTrace _curTrace;
37     
38     private static Collection4 _traces=new Collection4();
39     
40     private static Collection4 _includes;
41     
42     private static Collection4 _excludes;
43     
44     
45     public static void addCase(String JavaDoc caseInfo) {
46         _curTrace=newTrace(caseInfo);
47     }
48     
49     public static void addInclude(String JavaDoc classNameSubstring){
50         if(_includes == null){
51             _includes = new Collection4();
52         }
53         _includes.add(classNameSubstring);
54     }
55     
56     public static void addExclude(String JavaDoc classNameSubstring){
57         if(_excludes == null){
58             _excludes = new Collection4();
59         }
60         _excludes.add(classNameSubstring);
61     }
62     
63
64     public static void record() {
65         StackTrace trace=newTrace(null);
66         if(!_traces.contains(trace)) {
67             _traces.add(trace);
68         }
69     }
70     
71     public static void record(Object JavaDoc obj){
72         if(obj == null){
73             return;
74         }
75         Class JavaDoc clazz = (obj instanceof Class JavaDoc) ? (Class JavaDoc)obj : obj.getClass();
76         if(recordClass(clazz)){
77             record();
78         }
79     }
80     
81     private static boolean recordClass(Class JavaDoc clazz){
82         
83         String JavaDoc className = clazz.getName();
84         
85         if(_excludes != null){
86             Iterator4 i = _excludes.iterator();
87             while(i.moveNext()){
88                 String JavaDoc name = (String JavaDoc)i.current();
89                 if(className.indexOf(name) >= 0){
90                     return false;
91                 }
92             }
93         }
94         
95         boolean onNotFound = true;
96         
97         if(_includes != null){
98             onNotFound = false;
99             Iterator4 i = _includes.iterator();
100             while(i.moveNext()){
101                 String JavaDoc name = (String JavaDoc)i.current();
102                 if(className.indexOf(name) >= 0){
103                     onNotFound = true;
104                 }
105             }
106         }
107         
108         
109         Class JavaDoc claxx = clazz.getSuperclass();
110         if(claxx != null){
111             if ( recordClass(claxx)){
112                 return true;
113             }
114         }
115         
116         return onNotFound;
117     }
118     
119     public static void logAll() {
120         Iterator4 iter=_traces.iterator();
121         while(iter.moveNext()) {
122             System.out.println(iter.current());
123             if(iter.moveNext()) {
124                 System.out.println("\n---\n");
125             }
126         }
127     }
128
129     private static StackTrace newTrace(String JavaDoc caseInfo) {
130         return new StackTrace(EXCLUDEDEPTH,caseInfo,_curTrace);
131     }
132     
133     public static void main(String JavaDoc[] args) {
134         for(int i=0;i<2;i++) {
135             for(int j=0;j<2;j++) {
136                 StackRecorder.addCase("main"+i);
137                 foo();
138             }
139         }
140         for(int i=0;i<2;i++) {
141             for(int j=0;j<2;j++) {
142                 StackRecorder.addCase("mainX"+i);
143                 foo();
144             }
145         }
146         StackRecorder.logAll();
147     }
148     
149     public static void foo() {
150         for(int i=0;i<2;i++) {
151             for(int j=0;j<2;j++) {
152                 StackRecorder.addCase("foo"+i);
153                 bar();
154             }
155         }
156     }
157     
158     public static void bar() {
159         for(int i=0;i<2;i++) {
160             StackRecorder.record();
161         }
162     }
163 }
164
Popular Tags