in spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java [122:152]
static <T> T doWithMainClasses(File rootDirectory, MainClassCallback<T> callback) throws IOException {
if (!rootDirectory.exists()) {
return null; // nothing to do
}
if (!rootDirectory.isDirectory()) {
throw new IllegalArgumentException("Invalid root directory '" + rootDirectory + "'");
}
String prefix = rootDirectory.getAbsolutePath() + "/";
Deque<File> stack = new ArrayDeque<>();
stack.push(rootDirectory);
while (!stack.isEmpty()) {
File file = stack.pop();
if (file.isFile()) {
try (InputStream inputStream = new FileInputStream(file)) {
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
String className = convertToClassName(file.getAbsolutePath(), prefix);
T result = callback.doWith(new MainClass(className, classDescriptor.getAnnotationNames()));
if (result != null) {
return result;
}
}
}
}
if (file.isDirectory()) {
pushAllSorted(stack, file.listFiles(PACKAGE_DIRECTORY_FILTER));
pushAllSorted(stack, file.listFiles(CLASS_FILE_FILTER));
}
}
return null;
}