Search for main methods from the bottom of the stack

Update `MainMethod` to search from the bottom of the stack rather than
the start. Prior to this commit, an incorrect `main` method would be
found if more than one `main` was in the stack.

Fixes gh-35214
This commit is contained in:
Phillip Webb
2023-05-01 12:18:33 -07:00
parent e7578f0595
commit 4e6148f47b
2 changed files with 21 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,7 +40,9 @@ class MainMethod {
}
private Method getMainMethod(Thread thread) {
for (StackTraceElement element : thread.getStackTrace()) {
StackTraceElement[] stackTrace = thread.getStackTrace();
for (int i = stackTrace.length - 1; i >= 0; i--) {
StackTraceElement element = stackTrace[i];
if ("main".equals(element.getMethodName())) {
Method method = getMainMethod(element);
if (method != null) {