KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > ExceptionUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import java.io.StringWriter JavaDoc;
6 import java.io.PrintWriter JavaDoc;
7 import java.util.ArrayList JavaDoc;
8
9 public class ExceptionUtil {
10
11     /**
12      * Returns current stack trace in form of array of stack trace elements
13      * Since an exception is thrown internally, this method is slow.
14      */

15     public static StackTraceElement JavaDoc[] getCurrentStackTrace() {
16         try {
17             throw new Exception JavaDoc();
18         } catch (Exception JavaDoc ex) {
19             StackTraceElement JavaDoc[] ste = ex.getStackTrace();
20             if (ste.length > 1) {
21                 StackTraceElement JavaDoc[] result = new StackTraceElement JavaDoc[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     // ---------------------------------------------------------------- exception stack trace
33

34     /**
35      * Returns stack trace filtered by classnames.
36      */

37     public static StackTraceElement JavaDoc[] getStackTrace(Throwable JavaDoc t, String JavaDoc[] allow, String JavaDoc[] deny) {
38         StackTraceElement JavaDoc[] st = t.getStackTrace();
39         ArrayList JavaDoc result = new ArrayList JavaDoc(st.length);
40
41 elementLoop:
42         for (int i = 0; i < st.length; i++) {
43             StackTraceElement JavaDoc element = st[i];
44             String JavaDoc className = element.getClassName();
45             if (allow != null) {
46                 boolean validElemenet = false;
47                 for (int j = 0; j < allow.length; j++) {
48                     String JavaDoc 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 JavaDoc filter = deny[j];
61                     if (className.indexOf(filter) != -1) {
62                         continue elementLoop;
63                     }
64                 }
65             }
66             result.add(element);
67         }
68         st = new StackTraceElement JavaDoc[result.size()];
69         for (int i = 0; i < st.length; i++) {
70             st[i] = (StackTraceElement JavaDoc) result.get(i);
71         }
72         return st;
73     }
74
75     /**
76      * Returns stack trace chain filtered by classnames.
77      */

78     public static StackTraceElement JavaDoc[][] getStackTraceChain(Throwable JavaDoc t, String JavaDoc[] allow, String JavaDoc[] deny) {
79         ArrayList JavaDoc result = new ArrayList JavaDoc();
80         while (t != null) {
81             StackTraceElement JavaDoc[] stack = getStackTrace(t, allow, deny);
82             result.add(stack);
83             t = t.getCause();
84         }
85         StackTraceElement JavaDoc[][] allStacks = new StackTraceElement JavaDoc[result.size()][];
86         for (int i = 0; i < allStacks.length; i++) {
87             allStacks[i] = (StackTraceElement JavaDoc[]) result.get(i);
88         }
89         return allStacks;
90     }
91
92
93     /**
94      * Returns exception chain starting from top up to root cause.
95      */

96     public static Throwable JavaDoc[] getExceptionChain(Throwable JavaDoc throwable) {
97         ArrayList JavaDoc list = new ArrayList JavaDoc();
98         list.add(throwable);
99         while ((throwable = throwable.getCause()) != null) {
100             list.add(throwable);
101         }
102         Throwable JavaDoc[] result = new Throwable JavaDoc[list.size()];
103         for (int i = 0; i < result.length; i++) {
104             result[i] = (Throwable JavaDoc) list.get(i);
105         }
106         return result;
107     }
108
109
110     // ---------------------------------------------------------------- exception to string
111

112
113     /**
114      * Prints stack trace into a String.
115      */

116     public static String JavaDoc exceptionToString(Throwable JavaDoc t) {
117         StringWriter JavaDoc sw = new StringWriter JavaDoc();
118         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
119         t.printStackTrace(pw);
120         pw.flush();
121         sw.flush();
122         return sw.toString();
123     }
124
125     /**
126      * Prints full exception stack trace, from top to root cause, into a String.
127      */

128     public static String JavaDoc exceptionChainToString(Throwable JavaDoc t) {
129         StringWriter JavaDoc sw = new StringWriter JavaDoc();
130         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(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     // ---------------------------------------------------------------- root cause
141

142     /**
143      * Introspects the <code>Throwable</code> to obtain the root cause.
144      * <p>
145      * This method walks through the exception chain to the last element,
146      * "root" of the tree, and returns that exception. If no root cause faund
147      * returns <code>null</code>.
148      */

149     public static Throwable JavaDoc getRootCause(Throwable JavaDoc throwable) {
150         Throwable JavaDoc 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