Class StackTraceUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.Class<?> STACK_WALKER_CLASS
      The Class of java.lang.StackWalker that was introduced in JDK 9.
      static java.lang.String STACK_WALKER_CLASS_NAME
      The class name of java.lang.StackWalker that was introduced in JDK 9.
      static java.lang.Class<?> STACK_WALKER_OPTION_CLASS
      The Class of java.lang.StackWalker.Option that was introduced in JDK 9.
      static java.lang.String STACK_WALKER_OPTION_CLASS_NAME
      The class name of java.lang.StackWalker.Option that was introduced in JDK 9.
      static java.lang.Class<?> STACK_WALKER_STACK_FRAME_CLASS
      The Class of java.lang.StackWalker.StackFrame that was introduced in JDK 9.
      static java.lang.String STACK_WALKER_STACK_FRAME_CLASS_NAME
      The class name of java.lang.StackWalker.StackFrame that was introduced in JDK 9.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getCallerClassName()
      Retrieves the fully qualified name of the class that called this method.
      static java.lang.StackTraceElement[] getStackTrace()
      Retrieves the stack trace elements for the current thread's call stack.
      • Methods inherited from class java.lang.Object

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

      • STACK_WALKER_CLASS_NAME

        public static final java.lang.String STACK_WALKER_CLASS_NAME
        The class name of java.lang.StackWalker that was introduced in JDK 9.
        See Also:
        Constant Field Values
      • STACK_WALKER_OPTION_CLASS_NAME

        public static final java.lang.String STACK_WALKER_OPTION_CLASS_NAME
        The class name of java.lang.StackWalker.Option that was introduced in JDK 9.
        See Also:
        Constant Field Values
      • STACK_WALKER_STACK_FRAME_CLASS_NAME

        public static final java.lang.String STACK_WALKER_STACK_FRAME_CLASS_NAME
        The class name of java.lang.StackWalker.StackFrame that was introduced in JDK 9.
        See Also:
        Constant Field Values
      • STACK_WALKER_CLASS

        @Nullable
        public static final java.lang.Class<?> STACK_WALKER_CLASS
        The Class of java.lang.StackWalker that was introduced in JDK 9. (optional)
      • STACK_WALKER_OPTION_CLASS

        @Nullable
        public static final java.lang.Class<?> STACK_WALKER_OPTION_CLASS
        The Class of java.lang.StackWalker.Option that was introduced in JDK 9. (optional)
      • STACK_WALKER_STACK_FRAME_CLASS

        @Nullable
        public static final java.lang.Class<?> STACK_WALKER_STACK_FRAME_CLASS
        The Class of java.lang.StackWalker.StackFrame that was introduced in JDK 9. (optional)
    • Method Detail

      • getStackTrace

        public static java.lang.StackTraceElement[] getStackTrace()
        Retrieves the stack trace elements for the current thread's call stack.

        This method is useful for inspecting the sequence of method calls that led to the current point of execution. It can be used for debugging, logging, or monitoring purposes.

        Example Usage

        
         StackTraceElement[] stackTrace = StackTraceUtils.getStackTrace();
         for (StackTraceElement element : stackTrace) {
             System.out.println(element);
         }
         
        Returns:
        a non-null array of StackTraceElement representing the current thread's stack trace
      • getCallerClassName

        public static java.lang.String getCallerClassName()
        Retrieves the fully qualified name of the class that called this method.

        This method utilizes either java.lang.StackWalker (available in JDK 9+) or falls back to using StackTraceElement to determine the caller's class name. It ensures compatibility across different JVM versions.

        Example Usage

        
         String callerClassName = StackTraceUtils.getCallerClassName();
         System.out.println("Caller class: " + callerClassName);
         
        Returns:
        the fully qualified name of the calling class
        Throws:
        java.lang.IndexOutOfBoundsException - if the stack trace does not have enough frames to determine the caller