Skip to content

Commit

Permalink
fix: compiler warning "Potential resource leak"
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jun 12, 2024
1 parent fd20d73 commit 78a3641
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void handle() throws IOException {
logInfo("Locating valid VSCode grammar extensions...");
final Map<String, VsCodeExtensionPackageJson> pkgJsonByExtId = new HashMap<>();
final Map<String, Path> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Path> files = Files.walk(localPath)) {
files.sorted(Comparator.reverseOrder()) //
.map(Path::toFile) //
.forEach(File::delete);
}
}

Files.createDirectories(localPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ public static void execVerbose(final Path workDir, final String cmd, final Strin
}

public static Optional<Path> findFirstFile(final Path path, final Predicate<String> nameFilter) throws IOException {
return Files.list(path) //
.filter(Files::isRegularFile) //
.filter(file -> nameFilter.test(file.getFileName().toString())).findFirst();
try (Stream<Path> files = Files.walk(path)) {
return files //
.filter(Files::isRegularFile) //
.filter(file -> nameFilter.test(file.getFileName().toString())).findFirst();
}
}

public static String getFileExtension(final Path path) {
Expand All @@ -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<Path> files = Files.walk(dir)) {
files.sorted(Comparator.reverseOrder()) //
.map(Path::toFile) //
.forEach(File::delete);
}
}
}

Expand Down

0 comments on commit 78a3641

Please sign in to comment.