Package com.globalmentor.java
Class StackTrace
java.lang.Object
com.globalmentor.java.StackTrace
Convenience class and methods for working with stack traces.
- Author:
- Garret Wilson
-
Constructor Summary
ConstructorDescriptionCreates a stack trace from the current caller's location.StackTrace
(StackTraceElement[] stackTraceElements) Creates a stack trace from an array of stack elements.StackTrace
(StackTraceElement[] stackTraceElements, int start, int end) Creates a stack trace from an array of stack elements. -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
getCallingClass
(Package ignorePackage) Retrieves the class representing the caller of the class calling this method.static StackTraceElement
getCallingStackTraceElement
(Package ignorePackage) Retrieves the stack trace element representing the caller of the class calling this method.getStackTraceElementIgnoreClasses
(Class<?>... ignoreClasses) Retrieves the first stack trace element that is not from one of the ignored classes.getStackTraceElementIgnoreClasses
(String... ignoreClassNames) Retrieves the first stack trace element that is not from one of the ignored classes.getStackTraceElementIgnoreClasses
(Set<String> ignoreClassNames) Retrieves the first stack trace element that is not from one of the ignored classes.getStackTraceElementIgnorePackages
(Package... ignorePackages) Retrieves the first stack trace element that is not from one of the ignored packages.getStackTraceElementIgnorePackages
(String... ignorePackageNames) Retrieves the first stack trace element that is not from one of the ignored packages.getStackTraceElementIgnorePackages
(Set<String> ignorePackageNames) Retrieves the first stack trace element that is not from one of the ignored packages.boolean
isCurrentMethodIntersected
(StackTrace stackTrace) Determines if the given stack trace passes through the top method of this stack.<A extends Appendable>
Aprint
(A appendable) Prints this stack trace to the specified appendable in a format similar toThrowable.printStackTrace(PrintStream)
.toString()
-
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
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 isnull
.
-
StackTrace
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, inclusiveend
- the final index of the range to be included, exclusive.- Throws:
NullPointerException
- if the given array of stack trace elements isnull
.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
- Returns:
- A read only list of elements in this stack trace, with the deepest elements at the front of the list.
-
print
Prints this stack trace to the specified appendable in a format similar toThrowable.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 isnull
.IOException
- if there is an error printing to the appendable.
-
isCurrentMethodIntersected
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 isnull
.
-
getCallingClass
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, ornull
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
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, ornull
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
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
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
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
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
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
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
-