Browse Source

Merge branch '3.0.x'

Closes gh-35221
pull/35165/head
Phillip Webb 3 years ago
parent
commit
2c0edfd0b5
  1. 6
      spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java
  2. 17
      spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java

6
spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { 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())) { if ("main".equals(element.getMethodName())) {
Method method = getMainMethod(element); Method method = getMainMethod(element);
if (method != null) { if (method != null) {

17
spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java

@ -56,6 +56,14 @@ class MainMethodTests {
assertThat(method.getDeclaringClassName()).isEqualTo(this.actualMain.getDeclaringClass().getName()); 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 @Test
void missingArgsMainMethod() { void missingArgsMainMethod() {
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test()) 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 class MissingArgs {
public static void main() { public static void main() {

Loading…
Cancel
Save