1 29 package net.sourceforge.groboutils.util.throwable.v1; 30 31 32 import java.io.StringWriter ; 33 import java.io.PrintWriter ; 34 import java.io.BufferedReader ; 35 import java.io.StringReader ; 36 37 import java.util.Vector ; 38 39 import java.lang.reflect.Method ; 40 41 43 44 51 public class ThrowableParser 52 { 53 56 private BufferedReader traceReader; 58 59 private Object [] traceList; 61 private int traceIndex; 62 63 private static final String JDK14_GST_METHOD = "getStackTrace"; 64 private static final Class [] JDK14_GST_METHOD_ARGS = new Class [0]; 65 66 private static final String JDK14_GC_METHOD = "getCause"; 67 private static final Class [] JDK14_GC_METHOD_ARGS = new Class [0]; 68 69 70 71 75 public ThrowableParser( Throwable t ) 76 { 77 if (t == null) 78 { 79 throw new IllegalArgumentException ("no null arguments"); 80 } 81 parseThrowable( t ); 82 } 83 84 85 89 public synchronized StackTraceLineParser next() 90 { 91 if (this.traceList != null) 92 { 93 return next14(); 94 } 95 96 return next11(); 98 } 99 100 101 103 104 107 protected StackTraceLineParser next11() 108 { 109 while (true) 111 { 112 String line; 113 try 114 { 115 line = this.traceReader.readLine(); 116 } 118 catch (java.io.IOException ioe) 119 { 120 throw new IllegalStateException ("Never supposed to happen: "+ 121 ioe ); 122 } 123 if (line == null) 124 { 125 return null; 127 } 128 try 129 { 130 StackTraceLineParser stlp = new StackTraceLineParser( line ); 131 132 return stlp; 134 } 135 catch (Exception e) 136 { 137 } 140 } 141 } 142 143 144 147 protected StackTraceLineParser next14() 148 { 149 if (this.traceIndex >= this.traceList.length) 151 { 152 return null; 153 } 154 155 StackTraceLineParser stlp = new StackTraceLineParser( 158 this.traceList[ this.traceIndex ] ); 159 ++this.traceIndex; 160 161 return stlp; 162 } 163 164 165 166 protected void parseThrowable( Throwable t ) 167 { 168 try 169 { 170 Vector trace = new Vector (); 172 while (t != null) 173 { 174 Class c = t.getClass(); 175 Method m = c.getDeclaredMethod( JDK14_GST_METHOD, 176 JDK14_GST_METHOD_ARGS ); 177 Object [] o = (Object [])m.invoke( t, null ); 178 if (o != null) 179 { 180 for (int i = 0; i < o.length; ++i) 181 { 182 trace.addElement( o[i] ); 183 } 184 } 185 186 m = c.getDeclaredMethod( JDK14_GC_METHOD, 188 JDK14_GC_METHOD_ARGS ); 189 t = (Throwable )m.invoke( t, null ); 190 } 191 192 Object [] o = new Object [ trace.size() ]; 193 trace.copyInto( o ); 194 this.traceList = o; 195 this.traceIndex = 0; 196 197 } 199 catch (ThreadDeath td) 200 { 201 throw td; 203 } 204 catch (Throwable th) 205 { 206 parseThrowableBuffer( t ); 208 } 209 } 210 211 212 protected void parseThrowableBuffer( Throwable t ) 213 { 214 216 StringWriter sw = new StringWriter (); 217 t.printStackTrace( new PrintWriter ( sw ) ); 218 StringReader sr = new StringReader ( sw.toString() ); 219 this.traceReader = new BufferedReader ( sr ); 220 } 221 } 222 223 | Popular Tags |