Skip to content

Commit ecfb90b

Browse files
committed
Feature to support enable-preview main method
1 parent 5d8047a commit ecfb90b

File tree

1 file changed

+27
-2
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/launch

1 file changed

+27
-2
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/launch/Launcher.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,34 @@ private ClassLoader createClassLoader(URL[] urls) {
9797
protected void launch(ClassLoader classLoader, String mainClassName, String[] args) throws Exception {
9898
Thread.currentThread().setContextClassLoader(classLoader);
9999
Class<?> mainClass = Class.forName(mainClassName, false, classLoader);
100-
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
100+
101+
Method[] methods = mainClass.getDeclaredMethods();
102+
Method mainMethod = null;
103+
for(Method m : methods) {
104+
if("main".equals(m.getName())) {
105+
mainMethod = m;
106+
break;
107+
}
108+
}
109+
110+
if(mainMethod == null)
111+
throw new Exception("No main method was found");
112+
101113
mainMethod.setAccessible(true);
102-
mainMethod.invoke(null, new Object[] { args });
114+
Parameter[] params = mainMethod.getParameters();
115+
116+
if(params.length > 0) {
117+
if(params.length > 1)
118+
throw new Exception("No more than one arguments is accepted in main method");
119+
120+
Parameter argParam = params[0];
121+
if(!String[].class.getCanonicalName().equals(argParam.getType().getCanonicalName()))
122+
throw new Exception("Argument of main method is not accepted");
123+
124+
mainMethod.invoke(null, new Object[] { args });
125+
} else {
126+
mainMethod.invoke(null);
127+
}
103128
}
104129

105130
/**

0 commit comments

Comments
 (0)