1 3 package jodd.util; 4 5 import java.io.StringWriter ; 6 import java.io.PrintWriter ; 7 import java.util.ArrayList ; 8 9 public class ExceptionUtil { 10 11 15 public static StackTraceElement [] getCurrentStackTrace() { 16 try { 17 throw new Exception (); 18 } catch (Exception ex) { 19 StackTraceElement [] ste = ex.getStackTrace(); 20 if (ste.length > 1) { 21 StackTraceElement [] result = new StackTraceElement [ste.length - 1]; 22 System.arraycopy(ste, 1, result, 0, ste.length - 1); 23 return result; 24 } else { 25 return ste; 26 } 27 } 28 } 29 30 31 32 34 37 public static StackTraceElement [] getStackTrace(Throwable t, String [] allow, String [] deny) { 38 StackTraceElement [] st = t.getStackTrace(); 39 ArrayList result = new ArrayList (st.length); 40 41 elementLoop: 42 for (int i = 0; i < st.length; i++) { 43 StackTraceElement element = st[i]; 44 String className = element.getClassName(); 45 if (allow != null) { 46 boolean validElemenet = false; 47 for (int j = 0; j < allow.length; j++) { 48 String filter = allow[j]; 49 if (className.indexOf(filter) != -1) { 50 validElemenet = true; 51 break; 52 } 53 } 54 if (validElemenet == false) { 55 continue; 56 } 57 } 58 if (deny != null) { 59 for (int j = 0; j < deny.length; j++) { 60 String filter = deny[j]; 61 if (className.indexOf(filter) != -1) { 62 continue elementLoop; 63 } 64 } 65 } 66 result.add(element); 67 } 68 st = new StackTraceElement [result.size()]; 69 for (int i = 0; i < st.length; i++) { 70 st[i] = (StackTraceElement ) result.get(i); 71 } 72 return st; 73 } 74 75 78 public static StackTraceElement [][] getStackTraceChain(Throwable t, String [] allow, String [] deny) { 79 ArrayList result = new ArrayList (); 80 while (t != null) { 81 StackTraceElement [] stack = getStackTrace(t, allow, deny); 82 result.add(stack); 83 t = t.getCause(); 84 } 85 StackTraceElement [][] allStacks = new StackTraceElement [result.size()][]; 86 for (int i = 0; i < allStacks.length; i++) { 87 allStacks[i] = (StackTraceElement []) result.get(i); 88 } 89 return allStacks; 90 } 91 92 93 96 public static Throwable [] getExceptionChain(Throwable throwable) { 97 ArrayList list = new ArrayList (); 98 list.add(throwable); 99 while ((throwable = throwable.getCause()) != null) { 100 list.add(throwable); 101 } 102 Throwable [] result = new Throwable [list.size()]; 103 for (int i = 0; i < result.length; i++) { 104 result[i] = (Throwable ) list.get(i); 105 } 106 return result; 107 } 108 109 110 112 113 116 public static String exceptionToString(Throwable t) { 117 StringWriter sw = new StringWriter (); 118 PrintWriter pw = new PrintWriter (sw, true); 119 t.printStackTrace(pw); 120 pw.flush(); 121 sw.flush(); 122 return sw.toString(); 123 } 124 125 128 public static String exceptionChainToString(Throwable t) { 129 StringWriter sw = new StringWriter (); 130 PrintWriter pw = new PrintWriter (sw, true); 131 while (t != null) { 132 t.printStackTrace(pw); 133 t = t.getCause(); 134 } 135 pw.flush(); 136 sw.flush(); 137 return sw.toString(); 138 } 139 140 142 149 public static Throwable getRootCause(Throwable throwable) { 150 Throwable cause = throwable.getCause(); 151 if (cause != null) { 152 throwable = cause; 153 while ((throwable = throwable.getCause()) != null) { 154 cause = throwable; 155 } 156 } 157 return cause; 158 } 159 } 160 | Popular Tags |