KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > util > StackTraceHelper


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.util;
9
10 import java.util.List JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Utility methods for dealing with stack traces.
16  *
17  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
18  */

19 public final class StackTraceHelper {
20
21     /**
22      * Removes the AspectWerkz specific elements from the stack trace.
23      *
24      * @param exception the Throwable to modify the stack trace on
25      * @param className the name of the fake origin class of the exception
26      */

27     public static void hideFrameworkSpecificStackTrace(final Throwable JavaDoc exception, final String JavaDoc className) {
28         if (exception == null) {
29             throw new IllegalArgumentException JavaDoc("exception can not be null");
30         }
31         if (className == null) {
32             throw new IllegalArgumentException JavaDoc("class name can not be null");
33         }
34         final List JavaDoc newStackTraceList = new ArrayList JavaDoc();
35         final StackTraceElement JavaDoc[] stackTrace = exception.getStackTrace();
36         int i;
37         for (i = 1; i < stackTrace.length; i++) {
38             if (stackTrace[i].getClassName().equals(className)) {
39                 break;
40             }
41         }
42         for (int j = i; j < stackTrace.length; j++) {
43             newStackTraceList.add(stackTrace[j]);
44         }
45         final StackTraceElement JavaDoc[] newStackTrace = new StackTraceElement JavaDoc[newStackTraceList.size()];
46         int k = 0;
47         for (Iterator JavaDoc it = newStackTraceList.iterator(); it.hasNext(); k++) {
48             final StackTraceElement JavaDoc element = (StackTraceElement JavaDoc) it.next();
49             newStackTrace[k] = element;
50         }
51         exception.setStackTrace(newStackTrace);
52     }
53 }
Popular Tags