Skip to content

Commit 96457f4

Browse files
committed
Merge branch '3.5.x'
Closes gh-47996
2 parents 4e834f3 + 65a27c2 commit 96457f4

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

module/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
@@ -62,7 +62,7 @@ private boolean isLoaderClass(String className) {
6262
private @Nullable Method getMainMethod(StackTraceElement element) {
6363
try {
6464
Class<?> elementClass = Class.forName(element.getClassName());
65-
Method method = elementClass.getDeclaredMethod("main", String[].class);
65+
Method method = getMainMethod(elementClass);
6666
if (Modifier.isStatic(method.getModifiers())) {
6767
return method;
6868
}
@@ -73,6 +73,15 @@ private boolean isLoaderClass(String className) {
7373
return null;
7474
}
7575

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

module/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
@@ -19,7 +19,6 @@
1919
import java.lang.reflect.Method;
2020

2121
import org.jspecify.annotations.Nullable;
22-
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.boot.loader.launch.FakeJarLauncher;
@@ -38,13 +37,6 @@ class MainMethodTests {
3837

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

41-
private Method actualMain;
42-
43-
@BeforeEach
44-
void setup() throws Exception {
45-
this.actualMain = Valid.class.getMethod("main", String[].class);
46-
}
47-
4840
@Test
4941
@SuppressWarnings("NullAway") // Test null check
5042
void threadMustNotBeNull() {
@@ -54,9 +46,10 @@ void threadMustNotBeNull() {
5446

5547
@Test
5648
void validMainMethod() throws Exception {
49+
Method actualMain = Valid.class.getMethod("main", String[].class);
5750
MainMethod method = new TestThread(Valid::main).test();
58-
assertThat(method.getMethod()).isEqualTo(this.actualMain);
59-
assertThat(method.getDeclaringClassName()).isEqualTo(this.actualMain.getDeclaringClass().getName());
51+
assertThat(method.getMethod()).isEqualTo(actualMain);
52+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
6053
}
6154

6255
@Test // gh-35214
@@ -77,14 +70,41 @@ void viaJarLauncher() throws Exception {
7770
}
7871

7972
@Test
80-
void missingArgsMainMethod() {
81-
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test())
82-
.withMessageContaining("Unable to find main method");
73+
void detectPublicMainMethod() throws Exception {
74+
Method actualMain = PublicMainMethod.class.getMethod("main", String[].class);
75+
MainMethod method = new TestThread(PublicMainMethod::main).test();
76+
assertThat(method.getMethod()).isEqualTo(actualMain);
77+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
78+
}
79+
80+
@Test
81+
void detectPublicParameterlessMainMethod() throws Exception {
82+
Method actualMain = PublicParameterlessMainMethod.class.getMethod("main");
83+
MainMethod method = new TestThread(PublicParameterlessMainMethod::main).test();
84+
assertThat(method.getMethod()).isEqualTo(actualMain);
85+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
86+
}
87+
88+
@Test
89+
void detectPackagePrivateMainMethod() throws Exception {
90+
Method actualMain = PackagePrivateMainMethod.class.getDeclaredMethod("main", String[].class);
91+
MainMethod method = new TestThread(PackagePrivateMainMethod::main).test();
92+
assertThat(method.getMethod()).isEqualTo(actualMain);
93+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
94+
}
95+
96+
@Test
97+
void detectPackagePrivateParameterlessMainMethod() throws Exception {
98+
Method actualMain = PackagePrivateParameterlessMainMethod.class.getDeclaredMethod("main");
99+
MainMethod method = new TestThread(PackagePrivateParameterlessMainMethod::main).test();
100+
assertThat(method.getMethod()).isEqualTo(actualMain);
101+
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
83102
}
84103

85104
@Test
86105
void nonStatic() {
87-
assertThatIllegalStateException().isThrownBy(() -> new TestThread(() -> new NonStaticMain().main()).test())
106+
assertThatIllegalStateException()
107+
.isThrownBy(() -> new TestThread(() -> new NonStaticMainMethod().main()).test())
88108
.withMessageContaining("Unable to find main method");
89109
}
90110

@@ -145,15 +165,39 @@ public static void main(String... args) {
145165

146166
}
147167

148-
public static class MissingArgs {
168+
public static class PublicMainMethod {
169+
170+
public static void main(String... args) {
171+
mainMethod.set(new MainMethod());
172+
}
173+
174+
}
175+
176+
public static class PublicParameterlessMainMethod {
149177

150178
public static void main() {
151179
mainMethod.set(new MainMethod());
152180
}
153181

154182
}
155183

156-
public static class NonStaticMain {
184+
public static class PackagePrivateMainMethod {
185+
186+
static void main(String... args) {
187+
mainMethod.set(new MainMethod());
188+
}
189+
190+
}
191+
192+
public static class PackagePrivateParameterlessMainMethod {
193+
194+
static void main() {
195+
mainMethod.set(new MainMethod());
196+
}
197+
198+
}
199+
200+
public static class NonStaticMainMethod {
157201

158202
void main(String... args) {
159203
mainMethod.set(new MainMethod());

0 commit comments

Comments
 (0)