Skip to content

Commit 0c1c7e8

Browse files
committed
Don't detect main method from launcher classes
Update `MainMethod` discovery so that launcher classes from the `org.springframework.boot.loader` code are not considered. This restores the behavior of Spring Boot 2.7.11 and allows remote restart of uber jars without pulling the loader classes into the `RestartClassLoader`. Fixes gh-39733
1 parent 0e42a02 commit 0c1c7e8

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ private Method getMainMethod(Thread thread) {
4343
StackTraceElement[] stackTrace = thread.getStackTrace();
4444
for (int i = stackTrace.length - 1; i >= 0; i--) {
4545
StackTraceElement element = stackTrace[i];
46-
if ("main".equals(element.getMethodName())) {
46+
if ("main".equals(element.getMethodName()) && !isLoaderClass(element.getClassName())) {
4747
Method method = getMainMethod(element);
4848
if (method != null) {
4949
return method;
@@ -53,6 +53,10 @@ private Method getMainMethod(Thread thread) {
5353
throw new IllegalStateException("Unable to find main method");
5454
}
5555

56+
private boolean isLoaderClass(String className) {
57+
return className.startsWith("org.springframework.boot.loader.");
58+
}
59+
5660
private Method getMainMethod(StackTraceElement element) {
5761
try {
5862
Class<?> elementClass = Class.forName(element.getClassName());

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.boot.loader.launch.FakeJarLauncher;
2425
import org.springframework.util.ReflectionUtils;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
@@ -64,6 +65,15 @@ void nestedMainMethod() throws Exception {
6465
assertThat(method.getDeclaringClassName()).isEqualTo(nestedMain.getDeclaringClass().getName());
6566
}
6667

68+
@Test // gh-39733
69+
void vaiJarLauncher() throws Exception {
70+
FakeJarLauncher.action = (args) -> Valid.main(args);
71+
MainMethod method = new TestThread(FakeJarLauncher::main).test();
72+
Method expectedMain = Valid.class.getMethod("main", String[].class);
73+
assertThat(method.getMethod()).isEqualTo(expectedMain);
74+
assertThat(method.getDeclaringClassName()).isEqualTo(expectedMain.getDeclaringClass().getName());
75+
}
76+
6777
@Test
6878
void missingArgsMainMethod() {
6979
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.launch;
18+
19+
import java.util.function.Consumer;
20+
21+
/**
22+
* Fake launcher in the {@code org.springframework.boot.loader.launch} package used in
23+
* {@code MainMethodTests}.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
public final class FakeJarLauncher {
28+
29+
public static Consumer<String[]> action;
30+
31+
private FakeJarLauncher() {
32+
}
33+
34+
public static void main(String... args) {
35+
action.accept(args);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)