1 29 package net.sourceforge.groboutils.util.throwable.v1; 30 31 32 import java.lang.reflect.Method ; 33 34 36 37 46 public class StackTraceLineParser 47 { 48 51 private String className; 52 private String methodName; 53 private int lineNumber; 54 55 private static final String JDK14_STE_CLASS = "java.lang.StackTraceElement"; 56 private static final String JDK14_STE_GCN = "getClassName"; 57 private static final Class [] JDK14_STE_GCN_ARGS = new Class [0]; 58 private static final String JDK14_STE_GLN = "getLineNumber"; 59 private static final Class [] JDK14_STE_GLN_ARGS = new Class [0]; 60 private static final String JDK14_STE_GMN = "getMethodName"; 61 private static final Class [] JDK14_STE_GMN_ARGS = new Class [0]; 62 63 private static final String ST_START = "at "; 64 65 66 private static boolean IN_VISUAL_AGE = false; 69 static { 70 try 71 { 72 Class dummy = Class.forName("com.ibm.uvm.tools.DebugSupport"); 73 IN_VISUAL_AGE = true; 74 } 76 catch (Throwable e) 77 { 78 } 81 } 82 83 84 85 public StackTraceLineParser( String className, String methodName ) 86 { 87 this( className, methodName, -1 ); 88 } 89 90 91 public StackTraceLineParser( String className, String methodName, 92 int lineNumber ) 93 { 94 if (className == null || methodName == null) 95 { 96 throw new IllegalArgumentException ("no null arguments"); 97 } 98 if (lineNumber < 0) 99 { 100 lineNumber = -1; 101 } 102 this.lineNumber = lineNumber; 103 this.className = className; 104 this.methodName = methodName; 105 } 106 107 108 114 public StackTraceLineParser( Object obj ) 115 { 116 if (obj == null) 117 { 118 throw new IllegalArgumentException ("no null arguments"); 119 } 120 121 if (obj instanceof String ) 122 { 123 parseLine( (String )obj ); 124 } 125 else 126 { 127 parseElement( obj ); 128 } 129 } 130 131 132 public String getClassName() 133 { 134 return this.className; 135 } 136 137 138 public String getMethodName() 139 { 140 return this.methodName; 141 } 142 143 144 public int getLineNumber() 145 { 146 return this.lineNumber; 147 } 148 149 150 public String toString() 151 { 152 return getClassName() + '#' + getMethodName() + " (line " + 153 getLineNumber() + ')'; 154 } 155 156 157 160 161 protected void parseLine( String line ) 162 { 163 String nl = line.trim(); 164 int startPos = 0; 166 if (!IN_VISUAL_AGE) 167 { 168 if (nl.startsWith(ST_START)) 169 { 170 startPos += ST_START.length(); 171 } 172 else 173 { 174 notValidStackTraceLine( line ); 176 } 177 } 178 179 int endNamePos = nl.lastIndexOf('('); 180 if (endNamePos < startPos) 181 { 182 notValidStackTraceLine( line ); 184 } 185 186 int lastDot = nl.lastIndexOf('.', endNamePos ); 187 if (lastDot < startPos) 188 { 189 notValidStackTraceLine( line ); 192 } 193 194 this.methodName = findMethodName( nl, startPos, lastDot, endNamePos ); 195 this.className = findClassName( nl, startPos, lastDot, endNamePos ); 196 this.lineNumber = findLineNumber( nl, startPos, lastDot, endNamePos ); 197 } 198 199 200 203 protected String findMethodName( String line, int startPos, int lastDot, 204 int endNamePos ) 205 { 206 String name = line.substring( lastDot+1, endNamePos ).trim(); 207 return name; 209 } 210 211 212 215 protected String findClassName( String line, int startPos, int lastDot, 216 int endNamePos ) 217 { 218 String name = line.substring( startPos, lastDot ).trim(); 219 return name; 221 } 222 223 224 227 protected int findLineNumber( String line, int startPos, int lastDot, 228 int endNamePos ) 229 { 230 int ln = -1; 231 if (!IN_VISUAL_AGE) 232 { 233 int lineNumberStart = line.indexOf( ':', endNamePos ); 234 if (lineNumberStart > 0) 235 { 236 int lineNumberEnd = line.indexOf( ')', lineNumberStart ); 238 if (lineNumberEnd > 0) 239 { 240 try 243 { 244 String lineNumber = line.substring( 245 lineNumberStart+1, lineNumberEnd ); 246 ln = Integer.parseInt( lineNumber ); 248 } 249 catch (NumberFormatException e) 250 { 251 ln = -1; 253 } 254 } 255 else 256 { 257 } 259 } 260 else 261 { 262 } 265 } 266 return ln; 267 } 268 269 270 274 protected void parseElement( Object el ) 275 { 276 Class c = el.getClass(); 277 if (String .class.equals( c )) 278 { 279 throw new IllegalStateException ("Incorrect method invoked."); 281 } 282 if (!JDK14_STE_CLASS.equals( c.getName() )) 283 { 284 throw new IllegalArgumentException ( "Object "+el+ 285 " is not of type "+JDK14_STE_CLASS+ 286 ", but rather of type "+c.getName() ); 287 } 288 289 Method gcn; 290 Method gln; 291 Method gmn; 292 try 293 { 294 gcn = c.getDeclaredMethod( JDK14_STE_GCN, 295 JDK14_STE_GCN_ARGS ); 296 gln = c.getDeclaredMethod( JDK14_STE_GLN, 297 JDK14_STE_GLN_ARGS ); 298 gmn = c.getDeclaredMethod( JDK14_STE_GMN, 299 JDK14_STE_GMN_ARGS ); 300 } 301 catch (NoSuchMethodException nsme) 302 { 303 throw new IllegalArgumentException ( "Object "+el+ 304 " does not support the JDK 1.4 specification of "+ 305 JDK14_STE_CLASS ); 306 } 307 308 String cn; 309 int ln; 310 String mn; 311 try 312 { 313 cn = (String )gcn.invoke( el, null ); 314 ln = ((Integer )gln.invoke( el, null )).intValue(); 315 mn = (String )gmn.invoke( el, null ); 316 } 317 catch (Exception e) 318 { 319 throw new IllegalArgumentException ( "Object "+el+ 320 " could not retrieve values for JDK 1.4 specification of "+ 321 JDK14_STE_CLASS ); 322 } 323 324 this.className = cn; 325 this.lineNumber = ln; 326 this.methodName = mn; 327 } 328 329 330 protected void notValidStackTraceLine( String line ) 331 { 332 throw new IllegalArgumentException ("Line ["+ 333 line+"] is not a valid stack-trace line"); 334 } 335 } 336 337 | Popular Tags |