Class StackTrace

java.lang.Object
com.globalmentor.java.StackTrace

public class StackTrace extends Object
Convenience class and methods for working with stack traces.
Author:
Garret Wilson
  • Constructor Details

    • StackTrace

      public StackTrace()
      Creates a stack trace from the current caller's location. The stack trace includes all elements up to the caller; it does not include this constructor call.
    • StackTrace

      public StackTrace(StackTraceElement[] stackTraceElements)
      Creates a stack trace from an array of stack elements.
      Parameters:
      stackTraceElements - Stack elements from which to create a stack trace.
      Throws:
      NullPointerException - if the given array of stack trace elements is null.
    • StackTrace

      public StackTrace(StackTraceElement[] stackTraceElements, int start, int end)
      Creates a stack trace from an array of stack elements.
      Parameters:
      stackTraceElements - Stack elements from which to create a stack trace.
      start - the initial index of the range to be included, inclusive
      end - the final index of the range to be included, exclusive.
      Throws:
      NullPointerException - if the given array of stack trace elements is null.
      IllegalArgumentException - if the start index is greater than the end index.
      ArrayIndexOutOfBoundsException - if the start index is less than zero or the end index is greater than the length.
  • Method Details

    • getStackTraceElements

      public List<StackTraceElement> getStackTraceElements()
      Returns:
      A read only list of elements in this stack trace, with the deepest elements at the front of the list.
    • print

      public <A extends Appendable> A print(A appendable) throws IOException
      Prints this stack trace to the specified appendable in a format similar to Throwable.printStackTrace(PrintStream).
      Type Parameters:
      A - The type of appendable.
      Parameters:
      appendable - The appendable to use for output.
      Returns:
      The given appendable.
      Throws:
      NullPointerException - if the given appendable is null.
      IOException - if there is an error printing to the appendable.
    • isCurrentMethodIntersected

      public boolean isCurrentMethodIntersected(StackTrace stackTrace)
      Determines if the given stack trace passes through the top method of this stack.
      Parameters:
      stackTrace - The stack trace to compare with this one.
      Returns:
      true if the class and method of the initial stack trace element of this stack trace is found in the given stack trace.
      Throws:
      NullPointerException - if the given stack trace is null.
    • getCallingClass

      public static Class<?> getCallingClass(Package ignorePackage)
      Retrieves the class representing the caller of the class calling this method.

      The current stack trace element is assumed to be in this method itself, so it is ignored. The stack trace element before that is assumed to be the called to this method, and it is ignored as well. Therefore the class of the stack trace element at least two levels deep that isn't of the ignored packaged is returned.

      Parameters:
      ignorePackage - The package to ignore, or null if no package should be ignored.
      Returns:
      The calling class, or null if no stack trace element meeting the given criteria could be found.
    • getCallingStackTraceElement

      public static StackTraceElement getCallingStackTraceElement(Package ignorePackage)
      Retrieves the stack trace element representing the caller of the class calling this method.

      The current stack trace element is assumed to be in this method itself, so it is ignored. The stack trace element before that is assumed to be the called to this method, and it is ignored as well. Therefore the stack trace element at least two levels deep that isn't of the ignored packaged is returned.

      Parameters:
      ignorePackage - The package to ignore, or null if no package should be ignored.
      Returns:
      The calling stack trace element, or null if no stack trace element meeting the given criteria could be found.
    • getStackTraceElementIgnoreClasses

      public StackTraceElement getStackTraceElementIgnoreClasses(Class<?>... ignoreClasses)
      Retrieves the first stack trace element that is not from one of the ignored classes.
      Parameters:
      ignoreClasses - The classes to ignore.
      Returns:
      The stack trace element ignoring the classes, or null if there is no stack trace element without one of the ignored classes.
    • getStackTraceElementIgnoreClasses

      public StackTraceElement getStackTraceElementIgnoreClasses(String... ignoreClassNames)
      Retrieves the first stack trace element that is not from one of the ignored classes.
      Parameters:
      ignoreClassNames - The names of the classes to ignore.
      Returns:
      The stack trace element ignoring the named classes, or null if there is no stack trace element without one of the ignored class names.
    • getStackTraceElementIgnoreClasses

      public StackTraceElement getStackTraceElementIgnoreClasses(Set<String> ignoreClassNames)
      Retrieves the first stack trace element that is not from one of the ignored classes.
      Parameters:
      ignoreClassNames - The names of the classes to ignore.
      Returns:
      The stack trace element ignoring the named classes, or null if there is no stack trace element without one of the ignored class names.
    • getStackTraceElementIgnorePackages

      public StackTraceElement getStackTraceElementIgnorePackages(Package... ignorePackages)
      Retrieves the first stack trace element that is not from one of the ignored packages.
      Parameters:
      ignorePackages - The packages to ignore.
      Returns:
      The stack trace element ignoring the named packages, or null if there is no stack trace element without one of the ignored packages.
    • getStackTraceElementIgnorePackages

      public StackTraceElement getStackTraceElementIgnorePackages(String... ignorePackageNames)
      Retrieves the first stack trace element that is not from one of the ignored packages.
      Parameters:
      ignorePackageNames - The names of the packages to ignore.
      Returns:
      The stack trace element ignoring the named packages, or null if there is no stack trace element without one of the ignored package names.
    • getStackTraceElementIgnorePackages

      public StackTraceElement getStackTraceElementIgnorePackages(Set<String> ignorePackageNames)
      Retrieves the first stack trace element that is not from one of the ignored packages.
      Parameters:
      ignorePackageNames - The names of the packages to ignore.
      Returns:
      The stack trace element ignoring the named packages, or null if there is no stack trace element without one of the ignored package names.
    • toString

      public String toString()
      Overrides:
      toString in class Object