KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > exception > ExceptionUtility


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.exception;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25
26 /**
27  * <P>
28  * A utility for printing out and dealing with exceptions in a useful way.
29  * This utility supports the <code>getCause</code> method of the
30  * <code>Exception</code> class under JDK 1.4, but uses reflective invocation
31  * to be backwards compatible with JDK 1.3.
32  * </P>
33  *
34  * Copyright 2002 Sapient
35  * @since carbon 1.0
36  * @author Greg Hinkle, January 2002
37  * @version $Revision: 1.8 $($Author: dvoet $ / $Date: 2003/05/05 21:21:21 $)
38  */

39 public class ExceptionUtility {
40
41     /** The parameters to the getCause method */
42     private static final Class JavaDoc[] GET_CAUSE_PARAMS = new Class JavaDoc[0];
43
44     /**
45      * This method returns a string of the stack trace of an exception as
46      * well as the stack traces for all causal or previous exceptions.
47      * @param throwable the exception for which the trace should be returned
48      * @return a string listing of the exceptions stack traces
49      */

50     public static String JavaDoc printStackTracesToString(Throwable JavaDoc throwable) {
51
52         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(
53             ExceptionUtility.captureStackTrace(throwable));
54
55         Throwable JavaDoc cause = ExceptionUtility.getNestedException(throwable);
56         if (cause != null) {
57             buf.append(
58                 ExceptionUtility.printStackTracesToString(cause));
59         }
60         return buf.toString();
61     }
62
63
64     /**
65      * Returns the stack trace information in the form of a string.
66      * Wrapper for java.lang.Throwable.printStackTrace(Writer).
67      * @param throwable the throwable for which the trace should be captured
68      * @return a string listing of the stack trace for the supplied throwable
69      */

70     public static String JavaDoc captureStackTrace(Throwable JavaDoc throwable) {
71         if (throwable == null) {
72             return "";
73         } else {
74
75             StringWriter JavaDoc sw = new StringWriter JavaDoc();
76             throwable.printStackTrace(new PrintWriter JavaDoc(sw, true));
77
78             return sw.toString();
79         }
80     }
81
82
83
84     /**
85      * Gets the nested or causal exception for the supplied throwable. This
86      * method supports InvocationTargetExceptions as well as the Carbon Core's
87      * Exceptionable interface. It also supports the newly standardized
88      * <code>getCause</code> method of the JDK 1.4's Exception class. This is
89      * done via reflection to maintain backwards compatibility with JDK 1.3.
90      *
91      * @param throwable the throwable for which the nested exception should
92      * be retrieved
93      * @return The exception nested in the given exception. Returns <
94      * code>null</code> if the exception type is not supported, or
95      * if the input is <code>null</code>.
96      */

97     public static Throwable JavaDoc getNestedException(Throwable JavaDoc throwable) {
98         if (throwable == null) {
99             return null;
100         }
101
102         if (throwable instanceof InvocationTargetException JavaDoc) {
103             return
104                 ((InvocationTargetException JavaDoc) throwable).getTargetException();
105
106         } else if (throwable instanceof Exceptionable) {
107             return ((Exceptionable) throwable).getCause();
108         } else {
109             // Try to use reflection (will work great for jdk 1.4)
110
try {
111                 Class JavaDoc theClass = throwable.getClass();
112                 Method JavaDoc method =
113                     theClass.getMethod("getCause", GET_CAUSE_PARAMS);
114                 return (Throwable JavaDoc) method.invoke(throwable, null);
115             } catch (Throwable JavaDoc t) {
116                 // That didn't work, but that's ok
117
return null;
118             }
119         }
120         //return null;
121
}
122
123
124 }
Popular Tags