Class StackTraceUtils

java.lang.Object
net.luis.utils.util.unsafe.StackTraceUtils

public final class StackTraceUtils extends Object
Utility class for stack trace related operations.
Provides methods to get the calling class and method.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Constant for the system property 'unsafe.calls.main'.
    If this property is true, the methods in this class can be called from the main method.
    private static final String
    Constant for the system property 'unsafe.offset.base'.
    The base offset for the stack trace elements is 3.
    Used internally in getStackTrace(int) to get the correct stack trace element.
    private static final String
    Constant for the system property 'unsafe.offset.default'.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Class<?>
    Gets the calling class from the stack trace with an offset of:
    static Class<?>
    getCallingClass(int callsBefore)
    Gets the calling class from the stack trace with an offset of:
    static @NotNull Method
    Gets the calling method from the stack trace with an offset of:
    static @NotNull Method
    getCallingMethod(int callsBefore)
    Gets the calling method from the stack trace with an offset of:
    private static @Nullable Method
    Gets the calling method from the specified stack trace element.
    The calling method will be identified by the class name and the method name.
    If there are multiple methods with the same name in the same class, the method will not be identified.
    static @NotNull Optional<Method>
    Gets the calling method safe from the stack trace with an offset of:
    static @NotNull Optional<Method>
    getCallingMethodSafe(int callsBefore)
    Gets the calling method safe from the stack trace with an offset of:
    private static int
    Gets the default offset for the stack trace elements.
    private static @NotNull StackTraceElement
    getStackTrace(int callsBefore)
    Gets the stack trace element at the specified position.
    This method is used internally to get the correct stack trace element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNSAFE_CALLS_MAIN

      private static final String UNSAFE_CALLS_MAIN
      Constant for the system property 'unsafe.calls.main'.
      If this property is true, the methods in this class can be called from the main method.
      See Also:
    • UNSAFE_OFFSET_BASE

      private static final String UNSAFE_OFFSET_BASE
      Constant for the system property 'unsafe.offset.base'.
      The base offset for the stack trace elements is 3.
      Used internally in getStackTrace(int) to get the correct stack trace element.
      See Also:
    • UNSAFE_OFFSET_DEFAULT

      private static final String UNSAFE_OFFSET_DEFAULT
      Constant for the system property 'unsafe.offset.default'.

      The default offset for the stack trace elements is 1.
      The value must be greater than or equal to 1.
      The value will be added to the base offset to get the correct stack trace element.

      See Also:
  • Constructor Details

    • StackTraceUtils

      private StackTraceUtils()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • getCallingClass

      public static Class<?> getCallingClass()
      Gets the calling class from the stack trace with an offset of:
      base + default

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The method which requests the calling class
      • 4: The calling class
      Returns:
      The calling class
      Throws:
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getCallingClass

      public static Class<?> getCallingClass(int callsBefore)
      Gets the calling class from the stack trace with an offset of:
      base + default + callsBefore

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The class with the method which calls this method
      • 4: The calling class
      • ...
      Parameters:
      callsBefore - The number of calls before the method which requests the calling class
      Returns:
      The calling class at the specified position
      Throws:
      IllegalArgumentException - If the specified calls before value is less than or equal to 0
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getCallingMethod

      @NotNull public static @NotNull Method getCallingMethod()
      Gets the calling method from the stack trace with an offset of:
      base + default

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The method which requests the calling method
      • 4: The calling method
      Returns:
      The calling method
      Throws:
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalStateException - If the calling method could not be identified
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getCallingMethod

      @NotNull public static @NotNull Method getCallingMethod(int callsBefore)
      Gets the calling method from the stack trace with an offset of:
      base + default + callsBefore

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The method which calls this method
      • 4: The calling method
      • ...
      Parameters:
      callsBefore - The number of calls before the method which requests the calling method
      Returns:
      The calling method at the specified position
      Throws:
      IllegalArgumentException - If the specified calls before value is less than or equal to 0
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalStateException - If the calling method could not be identified
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getCallingMethodSafe

      @NotNull public static @NotNull Optional<Method> getCallingMethodSafe()
      Gets the calling method safe from the stack trace with an offset of:
      base + default

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The method which requests the calling method
      • 4: The calling method
      • ...
      Returns:
      An optional containing the calling method or an empty optional if the calling method could not be identified or an error occurred
      Throws:
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getCallingMethodSafe

      @NotNull public static @NotNull Optional<Method> getCallingMethodSafe(int callsBefore)
      Gets the calling method safe from the stack trace with an offset of:
      base + default + callsBefore

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      • ...
      • 2: This method
      • 3: The method which calls this method
      • 4: The calling method
      • 5: The method which calls the method which requests the calling method
      • ...
      Parameters:
      callsBefore - The number of calls before the method which requests the calling method
      Returns:
      An optional containing the calling method or an empty optional if the calling method could not be identified or an error occurred
      Throws:
      IllegalArgumentException - If the specified calls before value is less than or equal to 0
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
      See Also:
    • getDefaultOffset

      private static int getDefaultOffset()
      Gets the default offset for the stack trace elements.

      The default offset will be get from the system property 'unsafe.offset.default'.
      If the property is not set, the default value 1 will be used.

      Returns:
      The default offset
      See Also:
    • getStackTrace

      @NotNull private static @NotNull StackTraceElement getStackTrace(int callsBefore)
      Gets the stack trace element at the specified position.
      This method is used internally to get the correct stack trace element.

      The base offset will be get from the system property 'unsafe.offset.base'.
      If the property is not set, the default value 3 will be used.

      • 0: Thread.getStackTrace()
      • 2: This method
      • 2: The overloading method in this class
      • 3: The method which requests the calling stack trace element
      • 4: The calling method or class
      Parameters:
      callsBefore - The number of calls before the overloading method in this class
      Returns:
      The stack trace element at the specified position
      Throws:
      IllegalArgumentException - If the specified calls before value is less than or equal to 0
      IndexOutOfBoundsException - If the specified position is out of bounds
      IllegalCallerException - If called from the main method and 'unsafe.calls.main' is false
    • getCallingMethod

      @Nullable private static @Nullable Method getCallingMethod(@NotNull @NotNull StackTraceElement element)
      Gets the calling method from the specified stack trace element.
      The calling method will be identified by the class name and the method name.
      If there are multiple methods with the same name in the same class, the method will not be identified.
      Parameters:
      element - The stack trace element
      Returns:
      The calling method or null if the method could not be identified
      Throws:
      NullPointerException - If the specified stack trace element is null