| Home >> All >> com >> arranger >> jarl >> [ util Javadoc ] |
Source code: com/arranger/jarl/util/StackTraceUtil.java
1 package com.arranger.jarl.util; 2 3 import java.io.StringReader; 4 import java.io.BufferedReader; 5 import java.io.PrintWriter; 6 import java.io.ByteArrayOutputStream; 7 import java.util.List; 8 import java.util.ArrayList; 9 10 /** 11 * StackTraceUtil created on Apr 3, 2003 12 */ 13 public class StackTraceUtil { 14 15 private Throwable m_throwable = null; 16 private List m_locations = null; 17 18 public StackTraceUtil(Throwable thrown) { 19 m_throwable = thrown; 20 populateLocations(); 21 } 22 23 protected void finalize() throws Throwable { 24 clear(); 25 super.finalize(); 26 } 27 28 public String getExceptionClassName() { 29 return m_throwable.getClass().getName(); 30 } 31 32 public String getExceptionMessage() { 33 return m_throwable.getMessage(); 34 } 35 36 private List getLocations() { 37 if (m_locations == null) 38 m_locations = new ArrayList(); 39 40 return m_locations; 41 } 42 43 /** 44 * Get the entire stack trace 45 */ 46 public static String stackAsString(Throwable thrown) { 47 try { 48 ByteArrayOutputStream exceptOutputStream = new ByteArrayOutputStream() { 49 public String toString() { 50 return new String(this.toByteArray()); 51 } 52 }; 53 PrintWriter printWriter = new PrintWriter(exceptOutputStream); 54 thrown.printStackTrace(printWriter); 55 printWriter.flush(); 56 return exceptOutputStream.toString(); 57 } catch (Exception igorned) { 58 return null; 59 } 60 } 61 62 private void populateLocations() { 63 64 try { 65 BufferedReader bufferReader = new BufferedReader(new StringReader(stackAsString(m_throwable))); 66 67 int lineIndex = 0; 68 String currentLine = null; 69 while ((currentLine = bufferReader.readLine()) != null) { 70 if (lineIndex != 0) { 71 getLocations().add(currentLine.trim()); 72 } 73 lineIndex++; 74 } 75 76 bufferReader.close(); 77 } catch (Exception ignored) { 78 } 79 } 80 81 public String location(int index) { 82 83 if ((index < 0) || (index >= getLocations().size())) { 84 return ""; 85 } 86 87 return (String) getLocations().get(index); 88 } 89 90 public void clear() { 91 m_throwable = null; 92 93 if (m_locations != null) { 94 m_locations.clear(); 95 m_locations = null; 96 } 97 } 98 }