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
This commit is contained in:
Phillip Webb
2024-06-14 10:36:56 -07:00
parent 0e42a02dc7
commit 0c1c7e844c
3 changed files with 55 additions and 3 deletions

View File

@@ -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());