Class ExceptionUtil
This class contains static utility methods for handling and manipulating exceptions;
the only one you're likely to call being
getStackTraceWithRevisions(Throwable, ClassLoader, int, String),
which extracts CVS revision information from classes to produce an annotated (and highlighted)
stack trace.
When passing in ClassLoaders to these methods, you may want to try one of
- this.getClass().getClassLoader()
- Thread.currentThread().getContextClassLoader()
- Author:
- knoxg
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intAllow stack trace elements to be highlighted, as bold HTMLstatic final intAllow stack trace elements to be highlighted, with span'ed CSS elementsstatic final intPerform no stack trace element highlightingstatic final intAllow stack trace elements to be highlighted, as textstatic final intThe number of characters of the git revision to include in non-CSS stack traces (from the left side of the String) -
Method Summary
Modifier and TypeMethodDescriptionstatic StringescapeHtml(String string) Returns the HTML-escaped form of a string.static StringgetClassRevision2(ClassLoader loader, String className) An alternate implementation ofgetClassRevision(ClassLoader, String), which searches the raw bytecode of the class, rather than using Java reflection.static intReturn the number of elements in the current thread's stack trace (i.e.static StringConverts an exception's stack trace to a string.getStackTraceSummary(Throwable throwable) Returns a list of all the messages contained within a exception (following the causal chain of the supplied exception).static StringgetStackTraceWithRevisions(Throwable e, ClassLoader loader, int highlight, String highlightPrefix) Converts an exception's stack trace to a string.
-
Field Details
-
HIGHLIGHT_NONE
Perform no stack trace element highlighting- See Also:
-
HIGHLIGHT_TEXT
Allow stack trace elements to be highlighted, as text- See Also:
-
HIGHLIGHT_HTML
Allow stack trace elements to be highlighted, as bold HTML- See Also:
-
HIGHLIGHT_HTML_WITH_CSS
Allow stack trace elements to be highlighted, with span'ed CSS elements- See Also:
-
NUM_CHARACTERS_GIT_REVISION
The number of characters of the git revision to include in non-CSS stack traces (from the left side of the String)- See Also:
-
-
Method Details
-
getStackTrace
Converts an exception's stack trace to a string. If the exception passed to this function is null, returns the empty string.- Parameters:
e- exception- Returns:
- string representation of the exception's stack trace
-
getStackTraceWithRevisions
public static String getStackTraceWithRevisions(Throwable e, ClassLoader loader, int highlight, String highlightPrefix) Converts an exception's stack trace to a string. Each stack trace element is annotated to include the CVS revision Id, if it contains a public static String element containing this information.Stack trace elements whose classes begin with the specified highlightPrefix are also marked, to allow easier debugging. Highlights used can be text (which will insert the string "=>" before relevent stack trace elements), or HTML (which will render the stack trace element between <b> and </b> tags.
If HTML highlighting is enabled, then the exception message is also HTML-escaped.
- Parameters:
e- exceptionloader- ClassLoader used to read stack trace element revision informationhighlight- One of the HIGHLIGHT_* constants in this classhighlightPrefix- A prefix used to determine which stack trace elements are rendered as being 'important'. (e.g. "com.randomnoun.common."). Multiple prefixes can be specified, if separated by commas.- Returns:
- string representation of the exception's stack trace
-
getStackDepth
Return the number of elements in the current thread's stack trace (i.e. the height of the call stack).- Returns:
- the height of the call stack
-
getClassRevision2
public static String getClassRevision2(ClassLoader loader, String className) throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException An alternate implementation ofgetClassRevision(ClassLoader, String), which searches the raw bytecode of the class, rather than using Java reflection. May be a bit more robust.- Parameters:
loader- Classloader to useclassName- Class to load- Returns:
- The CVS revision string, in the form "ver 1.234".
- Throws:
ClassNotFoundExceptionSecurityExceptionNoSuchFieldExceptionIllegalArgumentExceptionIllegalAccessException
-
getStackTraceSummary
Returns a list of all the messages contained within a exception (following the causal chain of the supplied exception). This may be more useful to and end-user, since it should not contain any references to Java class names.- Parameters:
throwable- an exception- Returns:
- a List of Strings
-
escapeHtml
Returns the HTML-escaped form of a string. Any&,<,>, and"characters are converted to&,<,>, and"respectively.- Parameters:
string- the string to convert- Returns:
- the HTML-escaped form of the string
-