SpringBot通过 RuntimeException 推测 运行时方法栈的入口类

阅读 18

2022-04-14

	private Class<?> deduceMainApplicationClass() {
		try {
			StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
			for (StackTraceElement stackTraceElement : stackTrace) {
				if ("main".equals(stackTraceElement.getMethodName())) {
					return Class.forName(stackTraceElement.getClassName());
				}
			}
		}
		catch (ClassNotFoundException ex) {
			// Swallow and continue
		}
		return null;
	}
    public synchronized Throwable fillInStackTrace() {
        if (stackTrace != null ||
            backtrace != null /* Out of protocol state */ ) {
            fillInStackTrace(0);
            stackTrace = UNASSIGNED_STACK;
        }
        return this;
    }

    private native Throwable fillInStackTrace(int dummy);

1、RuntimeException 的父类 Throwable 中有一个 fillInStackTrace 本地方法,该方法会在子类实例化的时候 填充堆栈信息。可以通过分析堆栈信息 获取 调用链路 的参与类名和方法名称。

精彩评论(0)

0 0 举报