Skip to content

Commit 18f6613

Browse files
Merge pull request #350 from code-dot-org/sanchit/main-method
Validate full main method signature
2 parents 50c7393 + 228df9e commit 18f6613

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/UserInitiatedExceptionKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public enum UserInitiatedExceptionKey {
1010
TWO_MAIN_METHODS,
1111
// The user's code does not contain a main method.
1212
NO_MAIN_METHOD,
13+
// The user's main method has an invalid signature.
14+
INVALID_MAIN_METHOD,
1315
// The user's code has a compiler error.
1416
COMPILER_ERROR,
1517
// The user tried to include a source file that did not end in .java

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/util/ProjectLoadUtils.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.code.javabuilder.util;
22

33
import java.lang.reflect.Method;
4+
import java.lang.reflect.Modifier;
45
import java.net.URLClassLoader;
56
import java.util.List;
67
import org.code.javabuilder.JavaProjectFile;
@@ -18,8 +19,8 @@ private ProjectLoadUtils() {
1819
* @param classLoader class loader pointing to location of compiled classes
1920
* @param javaFiles a list of JavaProjectFiles
2021
* @return the main method if it is found
21-
* @throws UserInitiatedException if there is more than one main method or no main method, or if
22-
* the class definition is empty
22+
* @throws UserInitiatedException if there is more than one main method, no main method, an error
23+
* with the main method signature, or if the class definition is empty
2324
*/
2425
public static Method findMainMethod(URLClassLoader classLoader, List<JavaProjectFile> javaFiles)
2526
throws UserInitiatedException {
@@ -28,10 +29,7 @@ public static Method findMainMethod(URLClassLoader classLoader, List<JavaProject
2829
try {
2930
Method[] declaredMethods = classLoader.loadClass(file.getClassName()).getDeclaredMethods();
3031
for (Method method : declaredMethods) {
31-
Class[] parameterTypes = method.getParameterTypes();
32-
if (method.getName().equals("main")
33-
&& parameterTypes.length == 1
34-
&& parameterTypes[0].equals(String[].class)) {
32+
if (method.getName().equals("main")) {
3533
if (mainMethod != null) {
3634
throw new UserInitiatedException(UserInitiatedExceptionKey.TWO_MAIN_METHODS);
3735
}
@@ -46,6 +44,20 @@ public static Method findMainMethod(URLClassLoader classLoader, List<JavaProject
4644
}
4745
}
4846

47+
// If we found a main method, make sure the method signature is valid
48+
// (public, static, void return type, one argument which should be a String[]).
49+
if (mainMethod != null) {
50+
final Class<?>[] parameterTypes = mainMethod.getParameterTypes();
51+
final int modifiers = mainMethod.getModifiers();
52+
if (!Modifier.isPublic(modifiers)
53+
|| !Modifier.isStatic(modifiers)
54+
|| mainMethod.getGenericReturnType() != Void.TYPE
55+
|| parameterTypes.length != 1
56+
|| !parameterTypes[0].equals(String[].class)) {
57+
throw new UserInitiatedException(UserInitiatedExceptionKey.INVALID_MAIN_METHOD);
58+
}
59+
}
60+
4961
return mainMethod;
5062
}
5163

0 commit comments

Comments
 (0)