Package com.gitee.apanlh.util.thread
Class StackUtils
- java.lang.Object
-
- com.gitee.apanlh.util.thread.StackUtils
-
public class StackUtils extends Object
堆栈工具类- Author:
- Pan
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static List<String>getAllClassName()获取调用堆栈中的所有类名信息static List<String>getAllMethodName()获取调用堆栈中的所有方法名static List<StackTraceElement>getAllStackTraceByClassName(Class<?> clazz)根据指定类名获取调用堆栈中的所有堆栈帧信息static List<StackTraceElement>getAllStackTraceByClassName(String className)根据指定类名获取调用堆栈中的所有堆栈帧信息
如存在全限定名则equals判断
如不存在则只验证类名的结尾是否一致static List<StackTraceElement>getAllStackTraceByKeyword(String keyword)根据某个关键字获取堆栈帧信息static List<StackTraceElement>getAllStackTraceByMethodName(String methodName)根据指定方法名获取调用堆栈中的所有堆栈帧信息static Class<?>getCallClass()获取当前调用者类(class) 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:static Class<?>getCallClass(int depth)获取指定深度栈帧调用者类(Class)static StringgetCallClassName()获取当前调用者完整类名(全限定名) 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:static StringgetCallClassName(int depth)获取指定深度栈帧调用者完整类名(全限定名)static StringgetCallFileName()获取当前调用者的文件名 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:static StringgetCallFileName(int depth)获取指定深度栈帧调用者的文件名static intgetCallLineNumber()获取当前调用者的行号 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:static intgetCallLineNumber(int depth)获取指定深度栈帧调用者行号static StringgetCallMethodName()获取当前调用者的方法名称 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:static StringgetCallMethodName(int depth)获取指定深度栈帧调用者方法名称static StackTraceElement[]getStackTrace()获取调用堆栈的所有堆栈帧static StackTraceElementgetStackTraceByClassName(Class<?> clazz)根据类名获取首次出现的堆栈帧信息static StackTraceElementgetStackTraceByClassName(String className)根据类名获取首次出现的堆栈帧信息
如存在全限定名则equals判断
如不存在则只验证类名的结尾是否一致static StackTraceElementgetStackTraceByKeyword(String keyword)根据某个关键字获取首次出现的堆栈帧信息static StackTraceElementgetStackTraceByMethodName(String methodName)根据方法名获取首次出现的堆栈帧信息static StringgetThreadName()获取当前线程名称
-
-
-
Method Detail
-
getStackTrace
public static StackTraceElement[] getStackTrace()
获取调用堆栈的所有堆栈帧- Returns:
- StackTraceElement[] 包含所有堆栈帧的数组
-
getCallClass
public static Class<?> getCallClass()
获取当前调用者类(class) 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:[0] Thread.getStackTrace() [1] 本方法 (StackUtils.getCallClassName) [2] 调用本方法的上层方法(即“调用者”)若调用链深度增加(例如在工具类内部又封装了一层调用),导致固定深度无法获取到正确的调用者时,请使用带深度参数的重载方法
getCallClassName(int)并传入合适的偏移量- Returns:
- Class 调用者的类
-
getCallClass
public static Class<?> getCallClass(int depth)
获取指定深度栈帧调用者类(Class)- Parameters:
depth- 调用栈深度偏移量,从 0 开始计数
0 表示Thread.getStackTrace()方法本身所在的栈帧;
1 表示当前方法(本方法)的调用者;
2 表示再上一层的调用者,以此类推
一般业务场景中,若本方法被工具类直接调用,需要传入 2 或 3 才能拿到真正的业务调用类- Returns:
- Class 对应深度栈帧的类名(全限定名);若 depth 越界或堆栈获取失败,返回 null
-
getCallClassName
public static String getCallClassName()
获取当前调用者完整类名(全限定名) 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:[0] Thread.getStackTrace() [1] 本方法 (StackUtils.getCallClassName) [2] 调用本方法的上层方法(即“调用者”)若调用链深度增加(例如在工具类内部又封装了一层调用),导致固定深度无法获取到正确的调用者时,请使用带深度参数的重载方法
getCallClassName(int)并传入合适的偏移量- Returns:
- String 调用者的全限定类名
-
getCallClassName
public static String getCallClassName(int depth)
获取指定深度栈帧调用者完整类名(全限定名)- Parameters:
depth- 调用栈深度偏移量,从 0 开始计数
0 表示Thread.getStackTrace()方法本身所在的栈帧;
1 表示当前方法(本方法)的调用者;
2 表示再上一层的调用者,以此类推
一般业务场景中,若本方法被工具类直接调用,需要传入 2 或 3 才能拿到真正的业务调用类- Returns:
- String 对应深度栈帧的类名(全限定名);若 depth 越界或堆栈获取失败,返回 null
-
getCallMethodName
public static String getCallMethodName()
获取当前调用者的方法名称 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:[0] Thread.getStackTrace() [1] 本方法 (StackUtils.getCallClassName) [2] 调用本方法的上层方法(即“调用者”)若调用链深度增加(例如在工具类内部又封装了一层调用),导致固定深度无法获取到正确的调用者时,请使用带深度参数的重载方法
getCallMethodName(int)并传入合适的偏移量- Returns:
- String
-
getCallMethodName
public static String getCallMethodName(int depth)
获取指定深度栈帧调用者方法名称- Parameters:
depth- 调用栈深度偏移量,从 0 开始计数
0 表示Thread.getStackTrace()方法本身所在的栈帧;
1 表示当前方法(本方法)的调用者;
2 表示再上一层的调用者,以此类推
一般业务场景中,若本方法被工具类直接调用,需要传入 2 或 3 才能拿到真正的业务调用类- Returns:
- String 对应深度栈帧的类名(全限定名);若 depth 越界或堆栈获取失败,返回 null
-
getCallLineNumber
public static int getCallLineNumber()
获取当前调用者的行号 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:[0] Thread.getStackTrace() [1] 本方法 (StackUtils.getCallClassName) [2] 调用本方法的上层方法(即“调用者”)若调用链深度增加(例如在工具类内部又封装了一层调用),导致固定深度无法获取到正确的调用者时,请使用带深度参数的重载方法
getCallLineNumber(int)并传入合适的偏移量- Returns:
- int 返回对应调用帧行号
-
getCallLineNumber
public static int getCallLineNumber(int depth)
获取指定深度栈帧调用者行号- Parameters:
depth- 调用栈深度偏移量,从 0 开始计数
0 表示Thread.getStackTrace()方法本身所在的栈帧;
1 表示当前方法(本方法)的调用者;
2 表示再上一层的调用者,以此类推
一般业务场景中,若本方法被工具类直接调用,需要传入 2 或 3 才能拿到真正的业务调用类- Returns:
- int 返回对应调用帧行号
-
getCallFileName
public static String getCallFileName()
获取当前调用者的文件名 实现说明: 本方法使用预定义的固定深度索引 2 从当前线程堆栈中直接获取 该索引值基于以下调用链假设:[0] Thread.getStackTrace() [1] 本方法 (StackUtils.getCallClassName) [2] 调用本方法的上层方法(即“调用者”)若调用链深度增加(例如在工具类内部又封装了一层调用),导致固定深度无法获取到正确的调用者时,请使用带深度参数的重载方法
getCallFileName(int)并传入合适的偏移量- Returns:
- String 返回文件名称如:StackUtils.java
-
getCallFileName
public static String getCallFileName(int depth)
获取指定深度栈帧调用者的文件名- Parameters:
depth- 调用栈深度偏移量,从 0 开始计数
0 表示Thread.getStackTrace()方法本身所在的栈帧;
1 表示当前方法(本方法)的调用者;
2 表示再上一层的调用者,以此类推
一般业务场景中,若本方法被工具类直接调用,需要传入 2 或 3 才能拿到真正的业务调用类- Returns:
- String 返回文件名称如:StackUtils.java
-
getThreadName
public static String getThreadName()
获取当前线程名称- Returns:
- String
-
getAllMethodName
public static List<String> getAllMethodName()
获取调用堆栈中的所有方法名- Returns:
- List 包含所有方法名的列表
-
getAllStackTraceByClassName
public static List<StackTraceElement> getAllStackTraceByClassName(Class<?> clazz)
根据指定类名获取调用堆栈中的所有堆栈帧信息- Parameters:
clazz- 类- Returns:
- List 包含指定类名的所有堆栈帧的列表
-
getAllStackTraceByClassName
public static List<StackTraceElement> getAllStackTraceByClassName(String className)
根据指定类名获取调用堆栈中的所有堆栈帧信息
如存在全限定名则equals判断
如不存在则只验证类名的结尾是否一致- Parameters:
className- 类名- Returns:
- List 包含指定类名的所有堆栈帧的列表
-
getAllStackTraceByMethodName
public static List<StackTraceElement> getAllStackTraceByMethodName(String methodName)
根据指定方法名获取调用堆栈中的所有堆栈帧信息- Parameters:
methodName- 方法名- Returns:
- List 包含指定方法名的所有堆栈帧的列表
-
getAllStackTraceByKeyword
public static List<StackTraceElement> getAllStackTraceByKeyword(String keyword)
根据某个关键字获取堆栈帧信息- Parameters:
keyword- 关键字- Returns:
- List
-
getStackTraceByClassName
public static StackTraceElement getStackTraceByClassName(Class<?> clazz)
根据类名获取首次出现的堆栈帧信息- Parameters:
clazz- 类- Returns:
- StackTraceElement
-
getStackTraceByClassName
public static StackTraceElement getStackTraceByClassName(String className)
根据类名获取首次出现的堆栈帧信息
如存在全限定名则equals判断
如不存在则只验证类名的结尾是否一致- Parameters:
className- 类名- Returns:
- StackTraceElement
-
getStackTraceByMethodName
public static StackTraceElement getStackTraceByMethodName(String methodName)
根据方法名获取首次出现的堆栈帧信息- Parameters:
methodName- 方法名- Returns:
- StackTraceElement
-
getStackTraceByKeyword
public static StackTraceElement getStackTraceByKeyword(String keyword)
根据某个关键字获取首次出现的堆栈帧信息- Parameters:
keyword- 关键字- Returns:
- StackTraceElement
-
-