Class StackTraceHelper


  • @Immutable
    public final class StackTraceHelper
    extends Object
    This class contains utility methods for handling stack traces.
    Author:
    Philip Helger
    • Field Detail

      • DEFAULT_LINE_SEPARATOR

        public static final String DEFAULT_LINE_SEPARATOR
        the separator used to separate different lines of a stack
        See Also:
        Constant Field Values
    • Method Detail

      • getCurrentThreadStackAsString

        @Nonnull
        public static String getCurrentThreadStackAsString()
      • getStackAsString

        @Nonnull
        public static String getStackAsString​(@Nullable
                                              Throwable t)
        Get the stack trace of a throwable as string.
        Parameters:
        t - The throwable to be converted. May be null.
        Returns:
        the stack trace as newline separated string. If the passed Throwable is null an empty string is returned.
      • getStackAsString

        @Nonnull
        public static String getStackAsString​(@Nullable
                                              Throwable t,
                                              boolean bOmitCommonStackTraceElements)
        Get the stack trace of a throwable as string.
        Parameters:
        t - The throwable to be converted. May be null.
        bOmitCommonStackTraceElements - If true the stack trace is cut after certain class names occurring. If false the complete stack trace is returned.
        Returns:
        the stack trace as newline separated string. If the passed Throwable is null an empty string is returned.
      • getStackAsString

        @Nonnull
        public static String getStackAsString​(@Nullable
                                              Throwable t,
                                              boolean bOmitCommonStackTraceElements,
                                              @Nonnull
                                              String sLineSeparator)
        Get the stack trace of a throwable as string.
        Parameters:
        t - The throwable to be converted. May be null.
        bOmitCommonStackTraceElements - If true the stack trace is cut after certain class names occurring. If false the complete stack trace is returned.
        sLineSeparator - The line separator to use. May not be null.
        Returns:
        the stack trace as newline separated string. If the passed Throwable is null an empty string is returned.
        Since:
        9.3.6
      • containsUnitTestElement

        public static boolean containsUnitTestElement​(@Nullable
                                                      Throwable t)
        Check if the passed stack trace array contains a unit test element. Known unit test frameworks are JUnit and TestNG.
        Parameters:
        t - The Throwable whose stack trace should be scanned for unit test classes. May be null.
        Returns:
        true if at least one stack trace element is from a known unit test framework.
      • containsUnitTestElement

        public static boolean containsUnitTestElement​(@Nullable
                                                      StackTraceElement[] aStackTrace)
        Check if the passed stack trace array contains a unit test element. Known unit test frameworks are JUnit and TestNG.
        Parameters:
        aStackTrace - The stack trace array to be scanned. May be null.
        Returns:
        true if at least one stack trace element is from a known unit test framework.