1 21 package com.db4o.test.lib; 22 23 import java.io.*; 24 25 import com.db4o.foundation.*; 26 27 32 public class StackTrace { 33 private static final int IGNORELINES = 2; 35 private String [][] _trace; 37 private int _hash; 39 40 public StackTrace(int exclude,String caseInfo,StackTrace old) { 41 String traceStr=readStackTrace(new Throwable ()); 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 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 toString() { 106 StringBuffer buf=new StringBuffer (); 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 trace(int idx) { 121 return _trace[idx][0]; 122 } 123 124 private String caseInfo(int idx) { 125 return _trace[idx][1]; 126 } 127 128 private String readStackTrace(Throwable 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 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 cur = traceStr.substring(0,nlIdx); 146 split.add(cleanUpTraceLine(cur)); 147 traceStr=traceStr.substring(nlIdx+1); 148 nlIdx=traceStr.indexOf('\n'); 149 } 150 return split; 152 } 153 154 private String [][] buildStackTrace(Collection4 split,String caseInfo) { 155 String [][] trace=new String [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 )iter.current(); 160 } 161 trace[trace.length-1][1]=caseInfo; 162 return trace; 163 } 164 165 private String cleanUpTraceLine(String 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 |