Class StackTraceHelper

java.lang.Object
com.helger.base.rt.StackTraceHelper

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

    • DEFAULT_LINE_SEPARATOR

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

    • appendStackToString

      public static void appendStackToString(@NonNull StringBuilder aSB, @NonNull StackTraceElement[] aStackTraceElements)
      Append the stack trace of the given elements to the provided StringBuilder, using the default line separator.
      Parameters:
      aSB - The StringBuilder to append to. May not be null.
      aStackTraceElements - The stack trace elements to append. May not be null.
    • appendStackToString

      public static void appendStackToString(@NonNull StringBuilder aSB, @NonNull StackTraceElement[] aStackTraceElements, @NonNull String sLineSeparator)
      Append the stack trace of the given elements to the provided StringBuilder, using the specified line separator.
      Parameters:
      aSB - The StringBuilder to append to. May not be null.
      aStackTraceElements - The stack trace elements to append. May not be null.
      sLineSeparator - The line separator to use. May not be null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull StackTraceElement[] aStackTraceElements, boolean bOmitCommonStackTraceElements)
      Get the stack trace elements as a single string.
      Parameters:
      aStackTraceElements - The stack trace elements to convert. May not 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 a string. Never null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull StackTraceElement[] aStackTraceElements, boolean bOmitCommonStackTraceElements, @NonNull String sLineSeparator)
      Get the stack trace elements as a single string, using a custom line separator.
      Parameters:
      aStackTraceElements - The stack trace elements to convert. May not 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 a string. Never null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull StackTraceElement[] aStackTraceElements)
      Get the stack trace elements as a single string, omitting common stack trace elements and using the default line separator.
      Parameters:
      aStackTraceElements - The stack trace elements to convert. May not be null.
      Returns:
      The stack trace as a string. Never null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull Thread aThread)
      Get the stack trace of the passed thread as a single string, omitting common stack trace elements and using the default line separator.
      Parameters:
      aThread - The thread whose stack trace is to be converted. May not be null.
      Returns:
      The stack trace as a string. Never null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull Thread aThread, boolean bOmitCommonStackTraceElements)
      Get the stack trace of the passed thread as a single string, using the default line separator.
      Parameters:
      aThread - The thread whose stack trace is to be converted. May not 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 a string. Never null.
    • getStackAsString

      public static @NonNull String getStackAsString(@NonNull Thread aThread, boolean bOmitCommonStackTraceElements, @NonNull String sLineSeparator)
      Get the stack trace of the passed thread as a single string, using a custom line separator.
      Parameters:
      aThread - The thread whose stack trace is to be converted. May not 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 a string. Never null.
    • getCurrentThreadStackAsString

      public static @NonNull String getCurrentThreadStackAsString()
      Get the stack trace of the current thread as a single string, omitting common stack trace elements and using the default line separator.
      Returns:
      The current thread's stack trace as a string. Never null.
    • getStackAsString

      public static @NonNull 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

      public static @NonNull 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

      public static @NonNull 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.