Skip to content

Commit bf0152e

Browse files
nosansnicoll
authored andcommitted
Allow Devtools Restarter to work with a parameterless main method
See gh-47987 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent b9216a0 commit bf0152e

File tree

2 files changed

+34
-14
lines changed
  • spring-boot-project/spring-boot-devtools/src

2 files changed

+34
-14
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private boolean isLoaderClass(String className) {
6060
private Method getMainMethod(StackTraceElement element) {
6161
try {
6262
Class<?> elementClass = Class.forName(element.getClassName());
63-
Method method = elementClass.getDeclaredMethod("main", String[].class);
63+
Method method = getMainMethod(elementClass);
6464
if (Modifier.isStatic(method.getModifiers())) {
6565
return method;
6666
}
@@ -71,6 +71,15 @@ private Method getMainMethod(StackTraceElement element) {
7171
return null;
7272
}
7373

74+
private static Method getMainMethod(Class<?> clazz) throws Exception {
75+
try {
76+
return clazz.getDeclaredMethod("main", String[].class);
77+
}
78+
catch (NoSuchMethodException ex) {
79+
return clazz.getDeclaredMethod("main");
80+
}
81+
}
82+
7483
/**
7584
* Returns the actual main method.
7685
* @return the main method

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020

21-
import org.junit.jupiter.api.BeforeEach;
2221
import org.junit.jupiter.api.Test;
2322

2423
import org.springframework.boot.loader.launch.FakeJarLauncher;
@@ -37,13 +36,6 @@ class MainMethodTests {
3736

3837
private static final ThreadLocal<MainMethod> mainMethod = new ThreadLocal<>();
3938

40-
private Method actualMain;
41-
42-
@BeforeEach
43-
void setup() throws Exception {
44-
this.actualMain = Valid.class.getMethod("main", String[].class);
45-
}
46-
4739
@Test
4840
void threadMustNotBeNull() {
4941
assertThatIllegalArgumentException().isThrownBy(() -> new MainMethod(null))
@@ -52,9 +44,10 @@ void threadMustNotBeNull() {
5244

5345
@Test
5446
void validMainMethod() throws Exception {
47+
Method actualMain = Valid.class.getMethod("main", String[].class);
5548
MainMethod method = new TestThread(Valid::main).test();
56-
assertThat(method.getMethod()).isEqualTo(this.actualMain);
57-
assertThat(method.getDeclaringClassName()).isEqualTo(this.actualMain.getDeclaringClass().getName());
49+
assertThat(method.getMethod()).isEqualTo(actualMain);
50+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
5851
}
5952

6053
@Test // gh-35214
@@ -75,9 +68,19 @@ void viaJarLauncher() throws Exception {
7568
}
7669

7770
@Test
78-
void missingArgsMainMethod() {
79-
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test())
80-
.withMessageContaining("Unable to find main method");
71+
void missingArgsMainMethod() throws Exception {
72+
Method actualMain = MissingArgs.class.getMethod("main");
73+
MainMethod method = new TestThread(MissingArgs::main).test();
74+
assertThat(method.getMethod()).isEqualTo(actualMain);
75+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
76+
}
77+
78+
@Test
79+
void missingArgsPackagePrivateMainMethod() throws Exception {
80+
Method actualMain = MissingArgsPackagePrivate.class.getDeclaredMethod("main");
81+
MainMethod method = new TestThread(MissingArgsPackagePrivate::main).test();
82+
assertThat(method.getMethod()).isEqualTo(actualMain);
83+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
8184
}
8285

8386
@Test
@@ -149,6 +152,14 @@ public static void main() {
149152

150153
}
151154

155+
public static class MissingArgsPackagePrivate {
156+
157+
static void main() {
158+
mainMethod.set(new MainMethod());
159+
}
160+
161+
}
162+
152163
public static class NonStaticMain {
153164

154165
void main(String... args) {

0 commit comments

Comments
 (0)