Class StackTrace

java.lang.Object
com.github.toolarium.common.stacktrace.StackTrace

public final class StackTrace extends Object
The StackInfo class is a collection of static methods to allow users to quickly determine the call stack. This class makes it possible to determine what class called a method and what text files and line numbers resulted in stack calls. The StackInfo calls will not appear in return string. By calling the method getTrace a 2D-String-Array is returned.
  • Field Details

    • DEFAULT_EXCLUDES

      public static final List<String> DEFAULT_EXCLUDES
      Default Stacktrace exclues
  • Method Details

    • getStackTraceElements

      public static StackTraceElement[] getStackTraceElements()
      Parse the stack trace
      Returns:
      an array with stack trace elements.
    • getStackTraceElements

      public static StackTraceElement[] getStackTraceElements(int numberOfLastElements)
      Parse the stack trace
      Parameters:
      numberOfLastElements - the number of the last elements in stack trace
      Returns:
      an array with stack trace elements.
    • getStackTraceElements

      public static StackTraceElement[] getStackTraceElements(int startIndex, int numberOfLastElements)
      Parse the stack trace
      Parameters:
      startIndex - the start index
      numberOfLastElements - the number of the last elements in stack trace
      Returns:
      an array with stack trace elements.
    • getStackTrace

      public static String getStackTrace()
      Create the current stack trace
      Returns:
      the stack trace
    • getStackTrace

      public static String getStackTrace(int traceSize)
      Create the current stack trace
      Parameters:
      traceSize - the size of the stack trace (default -1)
      Returns:
      the stack trace
    • getStackTrace

      public static String getStackTrace(StackTraceElement[] elements)
      Prepares the given trace elements as stack trace
      Parameters:
      elements - the elements
      Returns:
      the prepared stack trace
    • getStackTrace

      public static String getStackTrace(StackTraceElement[] elements, List<String> filteredClassNames)
      Prepares the given trace elements as stack trace
      Parameters:
      elements - the elements
      filteredClassNames - a list of element names which should be filtered
      Returns:
      the prepared stack trace
    • getStackTrace

      public static String getStackTrace(StackTraceElement[] elements, List<String> filteredClassNames, String header)
      Prepares the given trace elements as stack trace
      Parameters:
      elements - the elements
      filteredClassNames - a list of element names which should be filtered
      header - the header
      Returns:
      the prepared stack trace
    • getStackTrace

      public static String getStackTrace(StackTraceElement[] elements, List<String> filteredClassNames, String header, String newline)
      Prepares the given trace elements as stack trace
      Parameters:
      elements - the elements
      filteredClassNames - a list of element names which should be filtered
      header - the header
      newline - the newline
      Returns:
      the prepared stack trace
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer)
      Process the current stack trace
      Parameters:
      consumer - the consumer
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer, List<String> filteredClassNames)
      Create the current stack trace
      Parameters:
      consumer - the consumer
      filteredClassNames - a list of element names which should be filtered
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer, int traceSize)
      Process the current stack trace
      Parameters:
      consumer - the consumer
      traceSize - the size of the stack trace (default -1)
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer, List<String> filteredClassNames, String header)
      Create the current stack trace
      Parameters:
      consumer - the consumer
      filteredClassNames - a list of element names which should be filtered
      header - the header
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer, List<String> filteredClassNames, String header, int traceSize)
      Create the current stack trace
      Parameters:
      consumer - the consumer
      filteredClassNames - a list of element names which should be filtered
      header - the header
      traceSize - the size of the stack trace (default -1)
    • processStackTrace

      public static void processStackTrace(Consumer<String> consumer, List<String> filteredClassNames, String header, String newline, int traceSize)
      Create the current stack trace
      Parameters:
      consumer - the consumer
      filteredClassNames - a list of element names which should be filtered
      header - the header
      newline - the newline
      traceSize - the size of the stack trace (default -1)
    • filterStackTrace

      public static StackTraceElement[] filterStackTrace(StackTraceElement[] elements)
      Filter the given trace elements as stack trace
      Parameters:
      elements - the elements
      Returns:
      the prepared stack trace
    • filterStackTrace

      public static StackTraceElement[] filterStackTrace(StackTraceElement[] elements, List<String> filteredClassNames)
      Filter the given trace elements as stack trace
      Parameters:
      elements - the elements
      filteredClassNames - a list of element names which should be filtered
      Returns:
      the prepared stack trace
    • parseStackTraceElements

      public static StackTraceElement[] parseStackTraceElements(Throwable t, int startTrace, int traceSize) throws IOException
      Parse the stack trace into the desired two dimensional string
      Parameters:
      t - the exception
      startTrace - the start of the trace (default 2)
      traceSize - the stack trace size (default -1)
      Returns:
      a formated two dimensional string: Each row representate a call on the stack and the columns are formated like below:
      • member 0 : name of text file from which call was made
      • member 1 : line number of text file from which call was made
      • member 2 : method name of calling class
      • member 3 : name of calling class
      • member 4 : full package name of calling class
      Throws:
      IOException - in case of error
    • parseStackTraceElements

      public static StackTraceElement[] parseStackTraceElements(String callStack, int startIndex, int traceSize)
      Parse the stack trace into the desired two dimensional string
      Parameters:
      callStack - the stack trace to parse
      startIndex - the start of the trace (default 2)
      traceSize - the stack trace size (default -1)
      Returns:
      a formated two dimensional string: Each row representate a call on the stack and the columns are formated like below:
      • member 0 : name of text file from which call was made
      • member 1 : line number of text file from which call was made
      • member 2 : method name of calling class
      • member 3 : name of calling class
      • member 4 : full package name of calling class
    • parseStackTraceElement

      public static StackTraceElement parseStackTraceElement(String line)
      Break a line from the stack trace into the format: member 0 : name of text file from which call was made member 1 : line number of text file from which call was made member 2 : method name of calling class member 3 : name of calling class member 4 : full package name of calling class
      Parameters:
      line - a line from the stack trace
      Returns:
      a stack trace element
    • parseStackTraceLine

      public static String[] parseStackTraceLine(String line)
      Break a line from the stack trace into the format: member 0 : name of text file from which call was made member 1 : line number of text file from which call was made member 2 : method name of calling class member 3 : name of calling class member 4 : full package name of calling class
      Parameters:
      line - a line from the stack trace
      Returns:
      a formated stack trace array:
      • member 0 : name of text file from which call was made
      • member 1 : line number of text file from which call was made
      • member 2 : method name of calling class
      • member 3 : name of calling class
      • member 4 : full package name of calling class