Skip to content

Commit 1bfd312

Browse files
committed
Use walkFileTree for extractModuleNameFromDirectory
1 parent 7eed096 commit 1bfd312

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

build-tools/src/main/java/org/elasticsearch/gradle/plugin/GenerateTestBuildInfoTask.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,24 @@ private String extractClassNameFromDirectory(File dir) throws IOException {
316316
* if it exists or the preset one derived from the jar task
317317
*/
318318
private String extractModuleNameFromDirectory(File dir) throws IOException {
319-
List<File> files = new ArrayList<>(List.of(dir));
320-
while (files.isEmpty() == false) {
321-
File find = files.removeFirst();
322-
if (find.exists()) {
323-
if (find.getName().equals("module-info.class")) {
324-
try (InputStream inputStream = new FileInputStream(find)) {
325-
return extractModuleNameFromModuleInfo(inputStream);
319+
var visitor = new SimpleFileVisitor<Path>() {
320+
private String result = getModuleName().getOrNull();
321+
322+
@Override
323+
public @NotNull FileVisitResult visitFile(@NotNull Path candidate, @NotNull BasicFileAttributes attrs) throws IOException {
324+
String name = candidate.getFileName().toString(); // Just the part after the last dir separator
325+
if (name.equals("module-info.class")) {
326+
try (InputStream inputStream = new FileInputStream(candidate.toFile())) {
327+
result = extractModuleNameFromModuleInfo(inputStream);
328+
return TERMINATE;
326329
}
327-
} else if (find.isDirectory()) {
328-
files.addAll(Arrays.asList(find.listFiles()));
330+
} else {
331+
return CONTINUE;
329332
}
330333
}
331-
}
332-
return getModuleName().getOrNull();
334+
};
335+
Files.walkFileTree(dir.toPath(), visitor);
336+
return visitor.result;
333337
}
334338

335339
/**

0 commit comments

Comments
 (0)