From a99551b2c8b24e895ff80138329013758136bba7 Mon Sep 17 00:00:00 2001 From: mattirn Date: Fri, 31 Mar 2023 07:33:52 +0200 Subject: [PATCH 1/2] Close Files streams using try-with-resources --- .../java/org/jline/builtins/Commands.java | 17 +++++++------ .../main/java/org/jline/builtins/Less.java | 7 +++--- .../main/java/org/jline/builtins/Nano.java | 7 +++--- .../main/java/org/jline/builtins/Source.java | 13 +++++----- .../org/jline/builtins/SyntaxHighlighter.java | 24 ++++++++++--------- .../jline/console/impl/ConsoleEngineImpl.java | 12 ++++++---- .../org/apache/felix/gogo/jline/Posix.java | 16 ++++++++----- .../java/org/jline/script/GroovyCommand.java | 9 +++---- .../java/org/jline/script/GroovyEngine.java | 7 +++--- .../impl/completer/FileNameCompleter.java | 5 ++-- 10 files changed, 66 insertions(+), 51 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Commands.java b/builtins/src/main/java/org/jline/builtins/Commands.java index e1406a172..a4f839e72 100644 --- a/builtins/src/main/java/org/jline/builtins/Commands.java +++ b/builtins/src/main/java/org/jline/builtins/Commands.java @@ -190,8 +190,10 @@ protected static List findFiles(Path root, String files) throws IOExceptio searchRoot = root; } PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - return Files.find(searchRoot, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .collect(Collectors.toList()); + try (Stream pathStream = + Files.find(searchRoot, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + return pathStream.collect(Collectors.toList()); + } } public static void history(LineReader reader, PrintStream out, PrintStream err, Path currentDir, String[] argv) @@ -1677,11 +1679,12 @@ public static void highlighter( String parameter = replaceFileName(currentTheme, "*" + TYPE_NANORCTHEME); out.println(currentTheme.getParent() + ":"); PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path)) - .forEach(p -> out.println(p.getFileName())); + try (Stream pathStream = Files.find( + Paths.get(new File(parameter).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(p -> out.println(p.getFileName())); + } } else { File themeFile; if (opt.isSet("view")) { diff --git a/builtins/src/main/java/org/jline/builtins/Less.java b/builtins/src/main/java/org/jline/builtins/Less.java index 02e1db5de..4e14c02cc 100644 --- a/builtins/src/main/java/org/jline/builtins/Less.java +++ b/builtins/src/main/java/org/jline/builtins/Less.java @@ -25,6 +25,7 @@ import java.util.*; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; import org.jline.builtins.Nano.PatternHistory; import org.jline.builtins.Source.ResourceSource; @@ -155,9 +156,9 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath } } else if (new File("/usr/share/nano").exists() && !ignorercfiles) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc"); - try { - Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .forEach(syntaxFiles::add); + try (Stream pathStream = Files.find( + Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(syntaxFiles::add); nanorcIgnoreErrors = true; } catch (IOException e) { errorMessage = "Encountered error while reading nanorc files"; diff --git a/builtins/src/main/java/org/jline/builtins/Nano.java b/builtins/src/main/java/org/jline/builtins/Nano.java index 8a2642ba4..fcf9be5bc 100644 --- a/builtins/src/main/java/org/jline/builtins/Nano.java +++ b/builtins/src/main/java/org/jline/builtins/Nano.java @@ -32,6 +32,7 @@ import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import org.jline.keymap.BindingReader; import org.jline.keymap.KeyMap; @@ -1574,9 +1575,9 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config } } else if (new File("/usr/share/nano").exists() && !ignorercfiles) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc"); - try { - Files.find(Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .forEach(syntaxFiles::add); + try (Stream pathStream = Files.find( + Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(syntaxFiles::add); nanorcIgnoreErrors = true; } catch (IOException e) { errorMessage = "Encountered error while reading nanorc files"; diff --git a/builtins/src/main/java/org/jline/builtins/Source.java b/builtins/src/main/java/org/jline/builtins/Source.java index 0915d58bf..e89bfe0e4 100644 --- a/builtins/src/main/java/org/jline/builtins/Source.java +++ b/builtins/src/main/java/org/jline/builtins/Source.java @@ -16,6 +16,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; +import java.util.stream.Stream; public interface Source { @@ -47,9 +48,9 @@ public InputStream read() throws IOException { @Override public Long lines() { Long out = null; - try { - out = Files.lines(new File(url.toURI()).toPath()).count(); - } catch (Exception e) { + try (Stream lines = Files.lines(new File(url.toURI()).toPath())) { + out = lines.count(); + } catch (Exception ignore) { } return out; } @@ -81,9 +82,9 @@ public InputStream read() throws IOException { @Override public Long lines() { Long out = null; - try { - out = Files.lines(path).count(); - } catch (Exception e) { + try (Stream lines = Files.lines(path)) { + out = lines.count(); + } catch (Exception ignore) { } return out; } diff --git a/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java b/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java index 978c53bc9..2054e19e5 100644 --- a/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java +++ b/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java @@ -17,6 +17,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; import org.jline.utils.*; @@ -150,11 +151,12 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) { protected static void nanorcInclude(String parameter, List syntaxFiles) throws IOException { if (parameter.contains("*") || parameter.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path)) - .forEach(syntaxFiles::add); + try (Stream pathStream = Files.find( + Paths.get(new File(parameter).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(syntaxFiles::add); + } } else { syntaxFiles.add(Paths.get(parameter)); } @@ -163,12 +165,12 @@ protected static void nanorcInclude(String parameter, List syntaxFiles) th protected static void nanorcTheme(String parameter, List syntaxFiles) throws IOException { if (parameter.contains("*") || parameter.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path)) - .findFirst() - .ifPresent(path -> syntaxFiles.add(0, path)); + try (Stream pathStream = Files.find( + Paths.get(new File(parameter).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path))) { + pathStream.findFirst().ifPresent(path -> syntaxFiles.add(0, path)); + } } else { syntaxFiles.add(0, Paths.get(parameter)); } diff --git a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java index e0641a192..13a106b49 100644 --- a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java +++ b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java @@ -25,6 +25,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jline.builtins.Completers.FilesCompleter; import org.jline.builtins.Completers.OptDesc; @@ -237,11 +238,12 @@ public Map scripts() { for (String e : scriptExtensions()) { String regex = pp + "/*." + e; PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - Files.find( - Paths.get(new File(regex).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path)) - .forEach(scripts::add); + try (Stream pathStream = Files.find( + Paths.get(new File(regex).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(scripts::add); + } } } } diff --git a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java index 964269c10..1aa4dd6be 100644 --- a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java +++ b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java @@ -1025,8 +1025,10 @@ protected void less(CommandSession session, Process process, String[] argv) thro sources.add(new StdInSource(process)); } else if (arg.contains("*") || arg.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + arg); - Files.find(session.currentDir(), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path)) - .forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); + try (Stream pathStream = + Files.find(session.currentDir(), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { + pathStream.forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); + } } else { sources.add(new PathSource(session.currentDir().resolve(arg), arg)); } @@ -1432,10 +1434,12 @@ else if (optCol) { if (expanded.size() > 1) { out.println(currentDir.relativize(path).toString() + ":"); } - display.accept(Stream.concat(Stream.of(".", "..").map(path::resolve), Files.list(path)) - .filter(filter) - .map(p -> new PathEntry(p, path)) - .sorted()); + try (Stream pathStream = Files.list(path)) { + display.accept(Stream.concat(Stream.of(".", "..").map(path::resolve), pathStream) + .filter(filter) + .map(p -> new PathEntry(p, path)) + .sorted()); + } } } diff --git a/groovy/src/main/java/org/jline/script/GroovyCommand.java b/groovy/src/main/java/org/jline/script/GroovyCommand.java index cb7f52334..f6c5a52aa 100644 --- a/groovy/src/main/java/org/jline/script/GroovyCommand.java +++ b/groovy/src/main/java/org/jline/script/GroovyCommand.java @@ -11,6 +11,7 @@ import java.io.File; import java.nio.file.*; import java.util.*; +import java.util.stream.Stream; import org.jline.builtins.Completers; import org.jline.builtins.Completers.OptDesc; @@ -288,10 +289,10 @@ private Object classLoader(CommandInput input) { .getPathMatcher("regex:" + arg.replace("\\", "\\\\").replace(".", "\\.") + separator.replace("\\", "\\\\") + ".*\\.jar"); - Files.walk(Paths.get(arg)) - .filter(matcher::matches) - .map(Path::toString) - .forEach(engine.classLoader::addClasspath); + try (Stream pathStream = + Files.walk(Paths.get(arg)).filter(matcher::matches)) { + pathStream.map(Path::toString).forEach(engine.classLoader::addClasspath); + } } else { engine.classLoader.addClasspath(arg); } diff --git a/groovy/src/main/java/org/jline/script/GroovyEngine.java b/groovy/src/main/java/org/jline/script/GroovyEngine.java index 68c0e657b..7ee117c9c 100644 --- a/groovy/src/main/java/org/jline/script/GroovyEngine.java +++ b/groovy/src/main/java/org/jline/script/GroovyEngine.java @@ -18,6 +18,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.groovy.ast.tools.ImmutablePropertyUtils; import org.codehaus.groovy.control.messages.SyntaxErrorMessage; @@ -822,10 +823,8 @@ private static Set sourcesForPackage(String domain) { PathMatcher matcher = FileSystems.getDefault().getPathMatcher("regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy"); Set out = new HashSet<>(); - try { - List paths = - Files.walk(Paths.get(".")).filter(matcher::matches).collect(Collectors.toList()); - for (Path p : paths) { + try (Stream pathStream = Files.walk(Paths.get(".")).filter(matcher::matches)) { + for (Path p : pathStream.collect(Collectors.toList())) { if (!p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy")) { continue; } diff --git a/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java b/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java index 9ab927db1..3b4bd6816 100644 --- a/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java +++ b/reader/src/main/java/org/jline/reader/impl/completer/FileNameCompleter.java @@ -9,6 +9,7 @@ package org.jline.reader.impl.completer; import java.io.IOException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -71,8 +72,8 @@ public void complete(LineReader reader, ParsedLine commandLine, final List { + try (DirectoryStream directoryStream = Files.newDirectoryStream(current, this::accept)) { + directoryStream.forEach(p -> { String value = curBuf + p.getFileName().toString(); if (Files.isDirectory(p)) { candidates.add(new Candidate( From 7f4fa7b1613594020fb6b8131574f2b21cfd0f44 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 3 Aug 2023 13:52:45 +0200 Subject: [PATCH 2/2] Use Files.walk when possible --- .../java/org/jline/builtins/Commands.java | 12 ++---- .../main/java/org/jline/builtins/Less.java | 5 +-- .../main/java/org/jline/builtins/Nano.java | 5 +-- .../org/jline/builtins/SyntaxHighlighter.java | 26 ++++-------- .../jline/console/impl/ConsoleEngineImpl.java | 8 ++-- .../org/apache/felix/gogo/jline/Posix.java | 7 ++-- .../java/org/jline/script/GroovyCommand.java | 8 ++-- .../java/org/jline/script/GroovyEngine.java | 41 +++++++++---------- 8 files changed, 48 insertions(+), 64 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Commands.java b/builtins/src/main/java/org/jline/builtins/Commands.java index a4f839e72..6ab5d84ad 100644 --- a/builtins/src/main/java/org/jline/builtins/Commands.java +++ b/builtins/src/main/java/org/jline/builtins/Commands.java @@ -190,9 +190,8 @@ protected static List findFiles(Path root, String files) throws IOExceptio searchRoot = root; } PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - try (Stream pathStream = - Files.find(searchRoot, Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { - return pathStream.collect(Collectors.toList()); + try (Stream pathStream = Files.walk(searchRoot)) { + return pathStream.filter(pathMatcher::matches).collect(Collectors.toList()); } } @@ -1679,11 +1678,8 @@ public static void highlighter( String parameter = replaceFileName(currentTheme, "*" + TYPE_NANORCTHEME); out.println(currentTheme.getParent() + ":"); PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - try (Stream pathStream = Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(p -> out.println(p.getFileName())); + try (Stream pathStream = Files.walk(Paths.get(new File(parameter).getParent()))) { + pathStream.filter(pathMatcher::matches).forEach(p -> out.println(p.getFileName())); } } else { File themeFile; diff --git a/builtins/src/main/java/org/jline/builtins/Less.java b/builtins/src/main/java/org/jline/builtins/Less.java index 4e14c02cc..142449ccd 100644 --- a/builtins/src/main/java/org/jline/builtins/Less.java +++ b/builtins/src/main/java/org/jline/builtins/Less.java @@ -156,9 +156,8 @@ public Less(Terminal terminal, Path currentDir, Options opts, ConfigurationPath } } else if (new File("/usr/share/nano").exists() && !ignorercfiles) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc"); - try (Stream pathStream = Files.find( - Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(syntaxFiles::add); + try (Stream pathStream = Files.walk(Paths.get("/usr/share/nano"))) { + pathStream.filter(pathMatcher::matches).forEach(syntaxFiles::add); nanorcIgnoreErrors = true; } catch (IOException e) { errorMessage = "Encountered error while reading nanorc files"; diff --git a/builtins/src/main/java/org/jline/builtins/Nano.java b/builtins/src/main/java/org/jline/builtins/Nano.java index fcf9be5bc..b39c42454 100644 --- a/builtins/src/main/java/org/jline/builtins/Nano.java +++ b/builtins/src/main/java/org/jline/builtins/Nano.java @@ -1575,9 +1575,8 @@ public Nano(Terminal terminal, Path root, Options opts, ConfigurationPath config } } else if (new File("/usr/share/nano").exists() && !ignorercfiles) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:/usr/share/nano/*.nanorc"); - try (Stream pathStream = Files.find( - Paths.get("/usr/share/nano"), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(syntaxFiles::add); + try (Stream pathStream = Files.walk(Paths.get("/usr/share/nano"))) { + pathStream.filter(pathMatcher::matches).forEach(syntaxFiles::add); nanorcIgnoreErrors = true; } catch (IOException e) { errorMessage = "Encountered error while reading nanorc files"; diff --git a/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java b/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java index 2054e19e5..6c24aef52 100644 --- a/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java +++ b/builtins/src/main/java/org/jline/builtins/SyntaxHighlighter.java @@ -14,6 +14,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; +import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -149,30 +150,21 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) { } protected static void nanorcInclude(String parameter, List syntaxFiles) throws IOException { - if (parameter.contains("*") || parameter.contains("?")) { - PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - try (Stream pathStream = Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(syntaxFiles::add); - } - } else { - syntaxFiles.add(Paths.get(parameter)); - } + addFiles(parameter, s -> s.forEach(syntaxFiles::add)); } protected static void nanorcTheme(String parameter, List syntaxFiles) throws IOException { + addFiles(parameter, s -> s.findFirst().ifPresent(p -> syntaxFiles.add(0, p))); + } + + protected static void addFiles(String parameter, Consumer> consumer) throws IOException { if (parameter.contains("*") || parameter.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + parameter); - try (Stream pathStream = Files.find( - Paths.get(new File(parameter).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path))) { - pathStream.findFirst().ifPresent(path -> syntaxFiles.add(0, path)); + try (Stream pathStream = Files.walk(Paths.get(new File(parameter).getParent()))) { + consumer.accept(pathStream.filter(pathMatcher::matches)); } } else { - syntaxFiles.add(0, Paths.get(parameter)); + consumer.accept(Stream.of(Paths.get(parameter))); } } diff --git a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java index 13a106b49..fdbd5b83d 100644 --- a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java +++ b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java @@ -238,11 +238,9 @@ public Map scripts() { for (String e : scriptExtensions()) { String regex = pp + "/*." + e; PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + regex); - try (Stream pathStream = Files.find( - Paths.get(new File(regex).getParent()), - Integer.MAX_VALUE, - (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(scripts::add); + try (Stream pathStream = + Files.walk(new File(regex).getParentFile().toPath())) { + pathStream.filter(pathMatcher::matches).forEach(scripts::add); } } } diff --git a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java index 1aa4dd6be..2945b163c 100644 --- a/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java +++ b/demo/src/main/java/org/apache/felix/gogo/jline/Posix.java @@ -1025,9 +1025,10 @@ protected void less(CommandSession session, Process process, String[] argv) thro sources.add(new StdInSource(process)); } else if (arg.contains("*") || arg.contains("?")) { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + arg); - try (Stream pathStream = - Files.find(session.currentDir(), Integer.MAX_VALUE, (path, f) -> pathMatcher.matches(path))) { - pathStream.forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); + try (Stream pathStream = Files.walk(session.currentDir())) { + pathStream + .filter(pathMatcher::matches) + .forEach(p -> sources.add(doUrlSource(session.currentDir(), p))); } } else { sources.add(new PathSource(session.currentDir().resolve(arg), arg)); diff --git a/groovy/src/main/java/org/jline/script/GroovyCommand.java b/groovy/src/main/java/org/jline/script/GroovyCommand.java index f6c5a52aa..40bf26813 100644 --- a/groovy/src/main/java/org/jline/script/GroovyCommand.java +++ b/groovy/src/main/java/org/jline/script/GroovyCommand.java @@ -289,9 +289,11 @@ private Object classLoader(CommandInput input) { .getPathMatcher("regex:" + arg.replace("\\", "\\\\").replace(".", "\\.") + separator.replace("\\", "\\\\") + ".*\\.jar"); - try (Stream pathStream = - Files.walk(Paths.get(arg)).filter(matcher::matches)) { - pathStream.map(Path::toString).forEach(engine.classLoader::addClasspath); + try (Stream pathStream = Files.walk(Paths.get(arg))) { + pathStream + .filter(matcher::matches) + .map(Path::toString) + .forEach(engine.classLoader::addClasspath); } } else { engine.classLoader.addClasspath(arg); diff --git a/groovy/src/main/java/org/jline/script/GroovyEngine.java b/groovy/src/main/java/org/jline/script/GroovyEngine.java index 7ee117c9c..f3ba92ae5 100644 --- a/groovy/src/main/java/org/jline/script/GroovyEngine.java +++ b/groovy/src/main/java/org/jline/script/GroovyEngine.java @@ -810,37 +810,34 @@ private static Set sourcesForPackage(String domain) { if (separator.equals("\\")) { separator += separator; } - String dom; boolean onlyPackage = domain != null && domain.endsWith("*"); if (onlyPackage) { domain = domain.substring(0, domain.lastIndexOf(".")); } - if (domain != null) { - dom = domain.isEmpty() ? ".*" + separator : separator + domain.replace(".", separator) + "(|.*)"; - } else { + String pkg = domain; + String dom; + if (domain == null) { dom = separator + "(|.*)"; + } else if (domain.isEmpty()) { + dom = ".*" + separator; + } else { + dom = separator + domain.replace(".", separator) + "(|.*)"; } - PathMatcher matcher = - FileSystems.getDefault().getPathMatcher("regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy"); + dom = "regex:\\." + dom + "[A-Z]+[a-zA-Z]*\\.groovy"; + PathMatcher matcher = FileSystems.getDefault().getPathMatcher(dom); Set out = new HashSet<>(); - try (Stream pathStream = Files.walk(Paths.get(".")).filter(matcher::matches)) { - for (Path p : pathStream.collect(Collectors.toList())) { - if (!p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy")) { - continue; - } - String source = p.toString(); - String className = source.substring(2, source.lastIndexOf(".")) - .replace(FileSystems.getDefault().getSeparator(), "."); - if (onlyPackage) { - if (Character.isUpperCase(className.charAt(domain.length() + 1))) { - out.add(className); - } - } else { - out.add(className); - } + try (Stream pathStream = Files.walk(Paths.get("."))) { + Stream classes = pathStream + .filter(matcher::matches) + .filter(p -> p.getFileName().toString().matches("[A-Z]+[a-zA-Z]*\\.groovy")) + .map(Path::toString) + .map(source -> source.substring(2, source.lastIndexOf(".")) + .replace(FileSystems.getDefault().getSeparator(), ".")); + if (onlyPackage) { + classes = classes.filter(cl -> Character.isUpperCase(cl.charAt(pkg.length() + 1))); } + classes.forEach(out::add); } catch (Exception ignore) { - } return out; }