KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
24
25 import com.db4o.foundation.*;
26
27 /**
28  * simple annotated stack trace for debugging
29  *
30  * @exclude
31  */

32 public class StackTrace {
33     // ignore top 2 lines: exception name and 'StackTrace#<init>'
34
private static final int IGNORELINES = 2;
35     // 0: trace line, 1: case info (or null)
36
private String JavaDoc[][] _trace;
37     // cache hashcode for faster equals()
38
private int _hash;
39     
40     public StackTrace(int exclude,String JavaDoc caseInfo,StackTrace old) {
41         String JavaDoc traceStr=readStackTrace(new Throwable JavaDoc());
42         Collection4 split = parseStackTrace(exclude, traceStr);
43         _trace=buildStackTrace(split, caseInfo);
44         copyCaseInfo(old);
45     }
46
47     private void copyCaseInfo(StackTrace old) {
48         if(old==null) {
49             return;
50         }
51         int length=(_trace.length<old._trace.length ? _trace.length : old._trace.length);
52         for(int idx=0;idx<length;idx++) {
53             if(!trace(idx).equals(old.trace(idx))) {
54                 break;
55             }
56             if(old.caseInfo(idx)!=null&&caseInfo(idx)==null) {
57                 _trace[idx][1]=old.caseInfo(idx);
58             }
59         }
60     }
61     
62     public boolean equals(Object JavaDoc obj) {
63         if(this==obj) {
64             return true;
65         }
66         if(obj==null||getClass()!=obj.getClass()) {
67             return false;
68         }
69         StackTrace casted=(StackTrace)obj;
70         if(hashCode()!=casted.hashCode()) {
71             return false;
72         }
73         if(_trace.length!=casted._trace.length) {
74             return false;
75         }
76         for (int idx = 0; idx < _trace.length; idx++) {
77             if(!trace(idx).equals(casted.trace(idx))) {
78                 return false;
79             }
80             if((caseInfo(idx)!=null)^(casted.caseInfo(idx)!=null)) {
81                 return false;
82             }
83             if(caseInfo(idx)!=null&&!caseInfo(idx).equals(casted.caseInfo(idx))) {
84                 return false;
85             }
86         }
87         return true;
88     }
89     
90     public int hashCode() {
91         if(_hash!=0) {
92             return _hash;
93         }
94         int hash=0;
95         for (int idx = 0; idx < _trace.length; idx++) {
96             hash=29*hash+trace(idx).hashCode();
97             if(caseInfo(idx)!=null) {
98                 hash=29*hash+caseInfo(idx).hashCode();
99             }
100         }
101         _hash=hash;
102         return hash;
103     }
104     
105     public String JavaDoc toString() {
106         StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
107         for (int idx = 0; idx < _trace.length; idx++) {
108             if(idx>0) {
109                 buf.append('\n');
110             }
111             buf.append(trace(idx));
112             if(caseInfo(idx)!=null) {
113                 buf.append('\n');
114                 buf.append("["+caseInfo(idx)+"]");
115             }
116         }
117         return buf.toString();
118     }
119
120     private String JavaDoc trace(int idx) {
121         return _trace[idx][0];
122     }
123
124     private String JavaDoc caseInfo(int idx) {
125         return _trace[idx][1];
126     }
127
128     private String JavaDoc readStackTrace(Throwable JavaDoc exc) {
129         StringWriter writer=new StringWriter();
130         exc.printStackTrace(new PrintWriter(writer));
131         return writer.toString();
132     }
133     
134     private Collection4 parseStackTrace(int exclude, String JavaDoc traceStr) {
135         Collection4 split=new Collection4();
136         int nlIdx=traceStr.indexOf('\n');
137         for(int i=0;i<(IGNORELINES+exclude);i++) {
138             traceStr=traceStr.substring(nlIdx+1);
139             nlIdx=traceStr.indexOf('\n');
140             if(nlIdx<0) {
141                 break;
142             }
143         }
144         while(nlIdx>-1) {
145             String JavaDoc cur = traceStr.substring(0,nlIdx);
146             split.add(cleanUpTraceLine(cur));
147             traceStr=traceStr.substring(nlIdx+1);
148             nlIdx=traceStr.indexOf('\n');
149         }
150         // last one is empty (trailing newline)
151
return split;
152     }
153     
154     private String JavaDoc[][] buildStackTrace(Collection4 split,String JavaDoc caseInfo) {
155         String JavaDoc[][] trace=new String JavaDoc[split.size()][2];
156         Iterator4 iter=split.iterator();
157         for (int idx = 0; idx < trace.length; idx++) {
158             iter.moveNext();
159             trace[idx][0]=(String JavaDoc)iter.current();
160         }
161         trace[trace.length-1][1]=caseInfo;
162         return trace;
163     }
164
165     private String JavaDoc cleanUpTraceLine(String JavaDoc str) {
166         int atIdx=str.indexOf("at ");
167         if(atIdx>=0) {
168             str=str.substring(atIdx+3);
169         }
170         int colonIdx=str.lastIndexOf(':');
171         if(colonIdx>=0) {
172             str=str.substring(0,colonIdx)+')';
173         }
174         return str;
175     }
176 }
177
Popular Tags