Skip to content

Commit 65a27c2

Browse files
committed
Merge pull request #47987 from nosan
* pr/47987: Polish contribution Allow Devtools Restarter to work with a parameterless main method Closes gh-47987
2 parents b9216a0 + 9ab77c7 commit 65a27c2

File tree

2 files changed

+70
-17
lines changed
  • spring-boot-project/spring-boot-devtools/src

2 files changed

+70
-17
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: 60 additions & 16 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,14 +68,41 @@ 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 detectPublicMainMethod() throws Exception {
72+
Method actualMain = PublicMainMethod.class.getMethod("main", String[].class);
73+
MainMethod method = new TestThread(PublicMainMethod::main).test();
74+
assertThat(method.getMethod()).isEqualTo(actualMain);
75+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
76+
}
77+
78+
@Test
79+
void detectPublicParameterlessMainMethod() throws Exception {
80+
Method actualMain = PublicParameterlessMainMethod.class.getMethod("main");
81+
MainMethod method = new TestThread(PublicParameterlessMainMethod::main).test();
82+
assertThat(method.getMethod()).isEqualTo(actualMain);
83+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
84+
}
85+
86+
@Test
87+
void detectPackagePrivateMainMethod() throws Exception {
88+
Method actualMain = PackagePrivateMainMethod.class.getDeclaredMethod("main", String[].class);
89+
MainMethod method = new TestThread(PackagePrivateMainMethod::main).test();
90+
assertThat(method.getMethod()).isEqualTo(actualMain);
91+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
92+
}
93+
94+
@Test
95+
void detectPackagePrivateParameterlessMainMethod() throws Exception {
96+
Method actualMain = PackagePrivateParameterlessMainMethod.class.getDeclaredMethod("main");
97+
MainMethod method = new TestThread(PackagePrivateParameterlessMainMethod::main).test();
98+
assertThat(method.getMethod()).isEqualTo(actualMain);
99+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
81100
}
82101

83102
@Test
84103
void nonStatic() {
85-
assertThatIllegalStateException().isThrownBy(() -> new TestThread(() -> new NonStaticMain().main()).test())
104+
assertThatIllegalStateException()
105+
.isThrownBy(() -> new TestThread(() -> new NonStaticMainMethod().main()).test())
86106
.withMessageContaining("Unable to find main method");
87107
}
88108

@@ -141,15 +161,39 @@ public static void main(String... args) {
141161

142162
}
143163

144-
public static class MissingArgs {
164+
public static class PublicMainMethod {
165+
166+
public static void main(String... args) {
167+
mainMethod.set(new MainMethod());
168+
}
169+
170+
}
171+
172+
public static class PublicParameterlessMainMethod {
145173

146174
public static void main() {
147175
mainMethod.set(new MainMethod());
148176
}
149177

150178
}
151179

152-
public static class NonStaticMain {
180+
public static class PackagePrivateMainMethod {
181+
182+
static void main(String... args) {
183+
mainMethod.set(new MainMethod());
184+
}
185+
186+
}
187+
188+
public static class PackagePrivateParameterlessMainMethod {
189+
190+
static void main() {
191+
mainMethod.set(new MainMethod());
192+
}
193+
194+
}
195+
196+
public static class NonStaticMainMethod {
153197

154198
void main(String... args) {
155199
mainMethod.set(new MainMethod());

0 commit comments

Comments
 (0)