Package de.aristaflow.adept2.util
Class StackTraceTools
- java.lang.Object
-
- de.aristaflow.adept2.util.StackTraceTools
-
public final class StackTraceTools extends Object
Provides tools to manipulate stack traces (i.e. arrays ofStackTraceElement
s.- Author:
- Patrick Schmidt
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
getStackTrace()
Gets the current stack as string.static StackTraceElement[]
headTrim(StackTraceElement[] stackTrace, int trim)
Removes the given number of elements from the head (i.e. the top) of the given stack trace and returns the result in a new array.static StackTraceElement[]
headTrim(StackTraceElement[] origStackTrace, Class<?> cls)
Removes all elements from the head of the stack trace that refer to the given class.static StackTraceElement[]
join(StackTraceElement[] headStackTrace, int tailTrimOfHead, StackTraceElement[] tailStackTrace, int headTrimOfTail)
Joins the two given stack traces, i.e. append the tail stack trace to the head stack trace, and returns the result in a new array.static StackTraceElement[]
join(StackTraceElement[] headStackTrace, StackTraceElement[] tailStackTrace)
Joins the two given stack traces, i.e. append the tail stack trace to the head stack trace, and returns the result in a new array.static void
printStackTrace()
Prints the current stack trace toSystem.out
.static String
stackTracesToString(StackTraceElement[] trace)
Returns the designated stack trace as string.static String
stackTraceToString(Throwable throwable)
Returns the output ofThrowable.printStackTrace(PrintWriter)
in a string.static StackTraceElement[]
tailTrim(StackTraceElement[] stackTrace, int trim)
Removes the given number of elements from the tail (i.e. the bottom) of the given stack trace and returns the result in a new array.
-
-
-
Method Detail
-
tailTrim
public static StackTraceElement[] tailTrim(StackTraceElement[] stackTrace, int trim)
Removes the given number of elements from the tail (i.e. the bottom) of the given stack trace and returns the result in a new array.- Parameters:
stackTrace
- the stack trace to be trimmedtrim
- the number of elements to be trimmed from the stack trace's tail- Returns:
- a new stack trace without the trimmed elements
-
headTrim
public static StackTraceElement[] headTrim(StackTraceElement[] stackTrace, int trim)
Removes the given number of elements from the head (i.e. the top) of the given stack trace and returns the result in a new array.- Parameters:
stackTrace
- the stack trace to be trimmedtrim
- the number of elements to be trimmed from the stack trace's head- Returns:
- a new stack trace without the trimmed elements
-
headTrim
public static StackTraceElement[] headTrim(StackTraceElement[] origStackTrace, Class<?> cls)
Removes all elements from the head of the stack trace that refer to the given class.- Parameters:
origStackTrace
- the stack trace to be trimmedcls
- the class for which to remove all elements from the head of the stack trace- Returns:
- a new stack trace without the trimmed elements
-
join
public static StackTraceElement[] join(StackTraceElement[] headStackTrace, StackTraceElement[] tailStackTrace)
Joins the two given stack traces, i.e. append the tail stack trace to the head stack trace, and returns the result in a new array.- Parameters:
headStackTrace
- the stack trace to which the tail stack trace will be appendedtailStackTrace
- the stack trace that will be appended to the head stack trace- Returns:
- the joined stack traces
-
join
public static StackTraceElement[] join(StackTraceElement[] headStackTrace, int tailTrimOfHead, StackTraceElement[] tailStackTrace, int headTrimOfTail)
Joins the two given stack traces, i.e. append the tail stack trace to the head stack trace, and returns the result in a new array. While doing so, the tail of the head stack trace and the head of the tail stack trace will be trimmed by the given numbers of elements.- Parameters:
headStackTrace
- the stack trace to which the tail stack trace will be appendedtailTrimOfHead
- the number of elements to be trimmed from the trail of the head stack tracetailStackTrace
- the stack trace that will be appended to the head stack traceheadTrimOfTail
- the number of elements to be trimmed from the head of the tail stack trace- Returns:
- the joined stack traces
-
stackTraceToString
public static String stackTraceToString(Throwable throwable)
Returns the output ofThrowable.printStackTrace(PrintWriter)
in a string.- Parameters:
throwable
- the throwable- Returns:
- the output of
Throwable.printStackTrace(PrintWriter)
in a string
-
stackTracesToString
public static String stackTracesToString(StackTraceElement[] trace)
Returns the designated stack trace as string.- Parameters:
trace
- The stack trace to be transformed to a string.- Returns:
- The designated stack trace as string.
-
getStackTrace
public static String getStackTrace()
Gets the current stack as string. This is the normal string representation of a throwable stack, i. e. using tabs and printing line numbers for each stack trace element.
The call of this method will not be in the stack.- Returns:
- The current stack as string.
-
printStackTrace
public static void printStackTrace()
Prints the current stack trace toSystem.out
. This method is only useful for debugging purposes.
-
-