From 0c1c7e844ca1b6ecfd4bde14c3e5505b692efa99 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 14 Jun 2024 10:36:56 -0700 Subject: [PATCH] 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 --- .../boot/devtools/restart/MainMethod.java | 8 +++- .../devtools/restart/MainMethodTests.java | 12 +++++- .../boot/loader/launch/FakeJarLauncher.java | 38 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/loader/launch/FakeJarLauncher.java diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java index a2435453783..9f3767ae6b9 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/MainMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ class MainMethod { 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()) && !isLoaderClass(element.getClassName())) { Method method = getMainMethod(element); if (method != null) { return method; @@ -53,6 +53,10 @@ class MainMethod { throw new IllegalStateException("Unable to find main method"); } + private boolean isLoaderClass(String className) { + return className.startsWith("org.springframework.boot.loader."); + } + private Method getMainMethod(StackTraceElement element) { try { Class elementClass = Class.forName(element.getClassName()); diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java index 593735aff57..b8fce3b1a31 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/MainMethodTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.boot.loader.launch.FakeJarLauncher; import org.springframework.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -64,6 +65,15 @@ class MainMethodTests { assertThat(method.getDeclaringClassName()).isEqualTo(nestedMain.getDeclaringClass().getName()); } + @Test // gh-39733 + void vaiJarLauncher() throws Exception { + FakeJarLauncher.action = (args) -> Valid.main(args); + MainMethod method = new TestThread(FakeJarLauncher::main).test(); + Method expectedMain = Valid.class.getMethod("main", String[].class); + assertThat(method.getMethod()).isEqualTo(expectedMain); + assertThat(method.getDeclaringClassName()).isEqualTo(expectedMain.getDeclaringClass().getName()); + } + @Test void missingArgsMainMethod() { assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test()) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/loader/launch/FakeJarLauncher.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/loader/launch/FakeJarLauncher.java new file mode 100644 index 00000000000..fb522a2da49 --- /dev/null +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/loader/launch/FakeJarLauncher.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.loader.launch; + +import java.util.function.Consumer; + +/** + * Fake launcher in the {@code org.springframework.boot.loader.launch} package used in + * {@code MainMethodTests}. + * + * @author Phillip Webb + */ +public final class FakeJarLauncher { + + public static Consumer action; + + private FakeJarLauncher() { + } + + public static void main(String... args) { + action.accept(args); + } + +}