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

@@ -56,6 +56,14 @@ class MainMethodTests {
assertThat(method.getDeclaringClassName()).isEqualTo(this.actualMain.getDeclaringClass().getName());
}
@Test // gh-35214
void nestedMainMethod() throws Exception {
MainMethod method = new TestThread(Nested::main).test();
Method nestedMain = Nested.class.getMethod("main", String[].class);
assertThat(method.getMethod()).isEqualTo(nestedMain);
assertThat(method.getDeclaringClassName()).isEqualTo(nestedMain.getDeclaringClass().getName());
}
@Test
void missingArgsMainMethod() {
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test())
@@ -114,6 +122,15 @@ class MainMethodTests {
}
public static class Nested {
public static void main(String... args) {
mainMethod.set(new MainMethod());
Valid.main(args);
}
}
public static class MissingArgs {
public static void main() {