From 78a36419c89789de6b74af821205c8041eebaa89 Mon Sep 17 00:00:00 2001 From: sebthom Date: Wed, 12 Jun 2024 13:00:23 +0200 Subject: [PATCH] fix: compiler warning "Potential resource leak" --- .../theme/raw/RawThemeReaderTest.java | 40 ++++++++++--------- .../VSCodeMultiExtensionsSourceHandler.java | 4 +- .../src/main/java/updater/utils/Git.java | 10 +++-- .../src/main/java/updater/utils/Sys.java | 17 ++++---- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/org.eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/theme/raw/RawThemeReaderTest.java b/org.eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/theme/raw/RawThemeReaderTest.java index 1fc4f66e4..42cd9d579 100644 --- a/org.eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/theme/raw/RawThemeReaderTest.java +++ b/org.eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/theme/raw/RawThemeReaderTest.java @@ -31,27 +31,29 @@ class RawThemeReaderTest { @NonNullByDefault({}) void testLoadingThemes() throws IOException { final var count = new AtomicInteger(); - Files.list(Paths.get("../org.eclipse.tm4e.core.tests/src/main/resources/test-cases/themes")).forEach(file -> { - final var fileName = file.getFileName().toString(); - if (fileName.endsWith(".json") && (fileName.contains("light") || fileName.contains("dark") || fileName.contains("black")) - || fileName.endsWith(".tmTheme")) { - System.out.println("Parsing [" + file + "]..."); - try { - final IRawTheme rawTheme = RawThemeReader.readTheme(IThemeSource.fromFile(file)); - count.incrementAndGet(); - assertFalse(castNonNull(rawTheme.getName()).isEmpty()); - assertFalse(castNonNull(rawTheme.getSettings()).isEmpty()); - for (final var setting : castNonNull(rawTheme.getSettings())) { - assertNotNull(setting.getSetting()); + try (final var files = Files.list(Paths.get("../org.eclipse.tm4e.core.tests/src/main/resources/test-cases/themes"))) { + files.forEach(file -> { + final var fileName = file.getFileName().toString(); + if (fileName.endsWith(".json") && (fileName.contains("light") || fileName.contains("dark") || fileName.contains("black")) + || fileName.endsWith(".tmTheme")) { + System.out.println("Parsing [" + file + "]..."); + try { + final IRawTheme rawTheme = RawThemeReader.readTheme(IThemeSource.fromFile(file)); + count.incrementAndGet(); + assertFalse(castNonNull(rawTheme.getName()).isEmpty()); + assertFalse(castNonNull(rawTheme.getSettings()).isEmpty()); + for (final var setting : castNonNull(rawTheme.getSettings())) { + assertNotNull(setting.getSetting()); + } + final var theme = Theme.createFromRawTheme(rawTheme, null); + assertFalse(theme.getColorMap().isEmpty()); + assertNotNull(theme.getDefaults()); + } catch (final Exception ex) { + throw new RuntimeException(ex); } - final var theme = Theme.createFromRawTheme(rawTheme, null); - assertFalse(theme.getColorMap().isEmpty()); - assertNotNull(theme.getDefaults()); - } catch (final Exception ex) { - throw new RuntimeException(ex); } - } - }); + }); + } System.out.println("Successfully parsed " + count.intValue() + " themes."); assertTrue(count.intValue() > 10, "Only " + count.intValue() + " themes found, expected more than 10!"); } diff --git a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/VSCodeMultiExtensionsSourceHandler.java b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/VSCodeMultiExtensionsSourceHandler.java index e5b037945..80263e43c 100644 --- a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/VSCodeMultiExtensionsSourceHandler.java +++ b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/VSCodeMultiExtensionsSourceHandler.java @@ -51,8 +51,8 @@ void handle() throws IOException { logInfo("Locating valid VSCode grammar extensions..."); final Map pkgJsonByExtId = new HashMap<>(); final Map pkgJsonPathByExtId = new HashMap<>(); - try (var l = withLogIndented()) { - for (final var dir : Files.list(sourceExtensionDir).filter(Files::isDirectory).toList()) { + try (var l = withLogIndented(); var files = Files.list(sourceExtensionDir)) { + for (final var dir : files.filter(Files::isDirectory).toList()) { final var pkgJSONPath = dir.resolve("package.json"); if (!Files.exists(pkgJSONPath)) { logInfo("Ignoring extension directory [" + dir.getFileName() + "] - no package.json found"); diff --git a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Git.java b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Git.java index 9d860efe1..4f455a938 100644 --- a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Git.java +++ b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Git.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.regex.Pattern; +import java.util.stream.Stream; import com.fasterxml.jackson.annotation.JsonProperty; @@ -114,10 +115,11 @@ public static GitCheckoutState gitSparseCheckout(final Path localPath, final Git } // delete local git repo if not in desired state - Files.walk(localPath) // - .sorted(Comparator.reverseOrder()) // - .map(Path::toFile) // - .forEach(File::delete); + try (Stream files = Files.walk(localPath)) { + files.sorted(Comparator.reverseOrder()) // + .map(Path::toFile) // + .forEach(File::delete); + } } Files.createDirectories(localPath); diff --git a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Sys.java b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Sys.java index 78eae6b76..bbc77f564 100644 --- a/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Sys.java +++ b/org.eclipse.tm4e.language_pack/updater/src/main/java/updater/utils/Sys.java @@ -149,9 +149,11 @@ public static void execVerbose(final Path workDir, final String cmd, final Strin } public static Optional findFirstFile(final Path path, final Predicate nameFilter) throws IOException { - return Files.list(path) // - .filter(Files::isRegularFile) // - .filter(file -> nameFilter.test(file.getFileName().toString())).findFirst(); + try (Stream files = Files.walk(path)) { + return files // + .filter(Files::isRegularFile) // + .filter(file -> nameFilter.test(file.getFileName().toString())).findFirst(); + } } public static String getFileExtension(final Path path) { @@ -165,10 +167,11 @@ public static String getFileExtension(final String path) { public static void rmDir(final Path dir) throws IOException { if (Files.exists(dir)) { - Files.walk(dir) // - .sorted(Comparator.reverseOrder()) // - .map(Path::toFile) // - .forEach(File::delete); + try (Stream files = Files.walk(dir)) { + files.sorted(Comparator.reverseOrder()) // + .map(Path::toFile) // + .forEach(File::delete); + } } }