Class StackTraceTools


  • public final class StackTraceTools
    extends Object
    Provides tools to manipulate stack traces (i.e. arrays of StackTraceElements.
    Author:
    Patrick Schmidt
    • 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 trimmed
        trim - 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 trimmed
        trim - 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 trimmed
        cls - 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 appended
        tailStackTrace - 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 appended
        tailTrimOfHead - the number of elements to be trimmed from the trail of the head stack trace
        tailStackTrace - the stack trace that will be appended to the head stack trace
        headTrimOfTail - the number of elements to be trimmed from the head of the tail stack trace
        Returns:
        the joined stack traces
      • 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 to System.out. This method is only useful for debugging purposes.