Skip to content

Commit

Permalink
Use try-with-resources where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul committed Jan 7, 2025
1 parent e4852a2 commit e4e0e7a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,7 @@ private long recurseFiles(Path path) throws IOException {
count += recurseFiles(path.resolve(child));
}
} else if (path.toString().endsWith(".class")) {
InputStream is = Files.newInputStream(path);
try {
try (InputStream is = Files.newInputStream(path)) {
Collection<ViolationOccurrence> occurrences =
modernizer.check(is);
for (ViolationOccurrence occurrence : occurrences) {
Expand All @@ -421,8 +420,6 @@ private long recurseFiles(Path path) throws IOException {
outputEntries.add(new OutputEntry(name, occurrence));
++count;
}
} finally {
Utils.closeQuietly(is);
}
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ private void detectInternal(Path path) throws IOException {
detectInternal(path.resolve(child));
}
} else if (path.toString().endsWith(".class")) {
InputStream inputStream = null;
try {
inputStream = Files.newInputStream(path);
try (InputStream inputStream = Files.newInputStream(path)) {
detectInternal(new ClassReader(inputStream));
} finally {
Utils.closeQuietly(inputStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ private void detectInternal(Path path) throws IOException {
detectInternal(path.resolve(child));
}
} else if (path.toString().endsWith(".class")) {
InputStream inputStream = null;
try {
inputStream = Files.newInputStream(path);
try (InputStream inputStream = Files.newInputStream(path)) {
detectInternal(new ClassReader(inputStream));
} finally {
Utils.closeQuietly(inputStream);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,18 @@ public final class ModernizerTest {

@Before
public void setUp() throws Exception {
InputStream is = Modernizer.class.getResourceAsStream(
"/modernizer.xml");
try {
try (InputStream is = Modernizer.class.getResourceAsStream(
"/modernizer.xml")) {
violations = Modernizer.parseFromXml(is);
} finally {
Utils.closeQuietly(is);
}
}

@Test
public void readsOldJavaVersionFormat() throws Exception {
InputStream is = Modernizer.class.getResourceAsStream(
"/modernizer-old-versions.xml");
try {
try (InputStream is = Modernizer.class.getResourceAsStream(
"/modernizer-old-versions.xml")) {
Map<String, Violation> old = Modernizer.parseFromXml(is);
assertThat(old).hasSize(1);
} finally {
Utils.closeQuietly(is);
}
}

Expand Down

0 comments on commit e4e0e7a

Please sign in to comment.