Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private boolean isLoaderClass(String className) {
private Method getMainMethod(StackTraceElement element) {
try {
Class<?> elementClass = Class.forName(element.getClassName());
Method method = elementClass.getDeclaredMethod("main", String[].class);
Method method = getMainMethod(elementClass);
if (Modifier.isStatic(method.getModifiers())) {
return method;
}
Expand All @@ -71,6 +71,15 @@ private Method getMainMethod(StackTraceElement element) {
return null;
}

private static Method getMainMethod(Class<?> clazz) throws Exception {
try {
return clazz.getDeclaredMethod("main", String[].class);
}
catch (NoSuchMethodException ex) {
return clazz.getDeclaredMethod("main");
}
}

/**
* Returns the actual main method.
* @return the main method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.reflect.Method;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.loader.launch.FakeJarLauncher;
Expand All @@ -37,13 +36,6 @@ class MainMethodTests {

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

private Method actualMain;

@BeforeEach
void setup() throws Exception {
this.actualMain = Valid.class.getMethod("main", String[].class);
}

@Test
void threadMustNotBeNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new MainMethod(null))
Expand All @@ -52,9 +44,10 @@ void threadMustNotBeNull() {

@Test
void validMainMethod() throws Exception {
Method actualMain = Valid.class.getMethod("main", String[].class);
MainMethod method = new TestThread(Valid::main).test();
assertThat(method.getMethod()).isEqualTo(this.actualMain);
assertThat(method.getDeclaringClassName()).isEqualTo(this.actualMain.getDeclaringClass().getName());
assertThat(method.getMethod()).isEqualTo(actualMain);
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
}

@Test // gh-35214
Expand All @@ -75,9 +68,19 @@ void viaJarLauncher() throws Exception {
}

@Test
void missingArgsMainMethod() {
assertThatIllegalStateException().isThrownBy(() -> new TestThread(MissingArgs::main).test())
.withMessageContaining("Unable to find main method");
void missingArgsMainMethod() throws Exception {
Method actualMain = MissingArgs.class.getMethod("main");
MainMethod method = new TestThread(MissingArgs::main).test();
assertThat(method.getMethod()).isEqualTo(actualMain);
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
}

@Test
void missingArgsPackagePrivateMainMethod() throws Exception {
Method actualMain = MissingArgsPackagePrivate.class.getDeclaredMethod("main");
MainMethod method = new TestThread(MissingArgsPackagePrivate::main).test();
assertThat(method.getMethod()).isEqualTo(actualMain);
assertThat(method.getDeclaringClassName()).isEqualTo(actualMain.getDeclaringClass().getName());
}

@Test
Expand Down Expand Up @@ -149,6 +152,14 @@ public static void main() {

}

public static class MissingArgsPackagePrivate {

static void main() {
mainMethod.set(new MainMethod());
}

}

public static class NonStaticMain {

void main(String... args) {
Expand Down