bayard 2003/03/17 21:10:48
Modified: lang/src/java/org/apache/commons/lang/exception
ExceptionUtils.java
Log:
Added getFullStackTrace and isNestedThrowable. Listed as Bug #16689
Revision Changes Path
1.21 +63 -1
jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
Index: ExceptionUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- ExceptionUtils.java 20 Jan 2003 23:04:19 -0000 1.20
+++ ExceptionUtils.java 18 Mar 2003 05:10:48 -0000 1.21
@@ -455,6 +455,68 @@
}
/**
+ * A way to get the entire nested stack-trace of an throwable.
+ *
+ * @param t The <code>Throwable</code>.
+ * @return The nested stack trace, with the root cause first.
+ */
+ public static String getFullStackTrace(Throwable t) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw, true);
+ Throwable[] ts = getThrowables(t);
+ for(int i=0; i<ts.length; i++) {
+ ts[i].printStackTrace(pw);
+ if(isNestedThrowable(ts[i])) {
+ break;
+ }
+ }
+ return sw.getBuffer().toString();
+ }
+
+ /**
+ * Whether an Throwable is considered nested or not.
+ *
+ * @param t The <code>Throwable</code>.
+ * @return boolean true/false
+ */
+ public static boolean isNestedThrowable(Throwable throwable) {
+ if(throwable == null) {
+ return false;
+ }
+
+ if (throwable instanceof Nestable) {
+ return true;
+ } else if (throwable instanceof SQLException) {
+ return true;
+ } else if (throwable instanceof InvocationTargetException) {
+ return true;
+ }
+
+ int sz = CAUSE_METHOD_NAMES.length;
+ for(int i=0; i<sz; i++) {
+ try {
+ Method method =
throwable.getClass().getMethod(CAUSE_METHOD_NAMES[i], null);
+ if(method != null) {
+ return true;
+ }
+ } catch (NoSuchMethodException ignored) {
+ } catch (SecurityException ignored) {
+ }
+ }
+
+ try {
+ Field field = throwable.getClass().getField("detail");
+ if(field != null) {
+ return true;
+ }
+ } catch (NoSuchFieldException ignored) {
+ } catch (SecurityException ignored) {
+ }
+
+ return false;
+ }
+
+ /**
* Captures the stack trace associated with the specified
* <code>Throwable</code> object, decomposing it into a list of
* stack frames.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]