Merge pull request #31828 from quaff

* gh-31828:
  Polish "Take MethodType into account to deduce main application class"
  Take MethodType into account to deduce main application class

Closes gh-31828
This commit is contained in:
Andy Wilkinson
2022-07-22 19:39:39 +01:00

View File

@@ -17,6 +17,7 @@
package org.springframework.boot;
import java.lang.StackWalker.StackFrame;
import java.lang.invoke.MethodType;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -165,6 +166,7 @@ import org.springframework.util.StringUtils;
* @author Brian Clozel
* @author Ethan Rubinson
* @author Chris Bono
* @author Yanming Zhou
* @since 1.0.0
* @see #run(Class, String[])
* @see #run(Class[], String[])
@@ -283,7 +285,9 @@ public class SpringApplication {
}
private Optional<Class<?>> findMainClass(Stream<StackFrame> stack) {
return stack.filter((frame) -> Objects.equals(frame.getMethodName(), "main")).findFirst()
MethodType mainMethodType = MethodType.methodType(void.class, String[].class);
return stack.filter((frame) -> Objects.equals(frame.getMethodName(), "main"))
.filter((frame) -> Objects.equals(frame.getMethodType(), mainMethodType)).findFirst()
.map(StackWalker.StackFrame::getDeclaringClass);
}