Skip to content

Commit cd9fe39

Browse files
committed
Fix cannot limit recursion level & some optimize
1 parent bbcf928 commit cd9fe39

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/java/JavaManager.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,16 @@ public void execute() throws Exception {
192192
}
193193

194194
private List<JavaRuntime> searchJava() throws IOException, InterruptedException {
195-
final int maxDepth = 2;
195+
final int maxDepth = 3;
196196

197-
Queue<File> fileQueue = new ArrayDeque<>(64);
198-
ArrayList<JavaRuntime> binaryList = new ArrayList<>();
199-
final String relative = "bin" + File.separator + OperatingSystem.CURRENT_OS.getJavaExecutable();
200197
File[] subDirs = directory.listFiles(File::isDirectory);
201-
if(subDirs == null) return binaryList;
202-
fileQueue.addAll(Arrays.asList(subDirs));
198+
if(subDirs == null) return Collections.emptyList();
199+
List<JavaRuntime> binaryList = new ArrayList<>();
200+
Queue<File> fileQueue = new LinkedList<>(Arrays.asList(subDirs));
203201
fileQueue.add(directory);
202+
final String relative = "bin" + File.separator + OperatingSystem.CURRENT_OS.getJavaExecutable();
204203
int depth = 0;
205-
while (depth < maxDepth) {
206-
if(isCancelled())
207-
return Collections.emptyList();
208-
if(fileQueue.isEmpty()) break;
204+
while(!fileQueue.isEmpty()) {
209205
final File dir = fileQueue.poll();
210206
if (dir == directory) {
211207
depth++;
@@ -218,11 +214,15 @@ private List<JavaRuntime> searchJava() throws IOException, InterruptedException
218214
binaryList.add(JavaManager.getJava(binary.toPath()));
219215
continue;
220216
}
221-
subDirs = dir.listFiles(File::isDirectory);
222-
if (subDirs != null)
223-
fileQueue.addAll(Arrays.asList(subDirs));
217+
if(depth < maxDepth - 1) {
218+
subDirs = dir.listFiles(File::isDirectory);
219+
if (subDirs != null)
220+
fileQueue.addAll(Arrays.asList(subDirs));
221+
}
222+
if(isCancelled())
223+
return Collections.emptyList();
224224
}
225-
return binaryList;
225+
return Collections.unmodifiableList(binaryList);
226226
}
227227
}
228228

0 commit comments

Comments
 (0)