Skip to content

Commit

Permalink
Prefer try with resources (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo authored Dec 9, 2024
1 parent 7f0b12a commit 5c7616e
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;

import org.codehaus.plexus.util.cli.StreamConsumer;

Expand All @@ -40,16 +40,11 @@ private ConsumerUtils() {}
* @throws IOException if any
*/
public static void consumeFile(File f, StreamConsumer consumer) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(f));

try {
try (BufferedReader reader = Files.newBufferedReader(f.toPath())) {
String line;
while ((line = r.readLine()) != null) {
while ((line = reader.readLine()) != null) {
consumer.consumeLine(line);
}

} finally {
r.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Writer;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
Expand Down Expand Up @@ -73,11 +72,9 @@ public LocalScmMetadata buildMetadata(File repository) throws IOException {
public void writeMetadata(File destinationDir, LocalScmMetadata metadata) throws IOException {
File metadataFile = new File(destinationDir, FILENAME);
metadataFile.createNewFile();
Writer writer = WriterFactory.newXmlWriter(metadataFile);
try {

try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
new LocalScmMetadataXpp3Writer().write(writer, metadata);
} finally {
IOUtil.close(writer);
}
}

Expand All @@ -92,23 +89,20 @@ public LocalScmMetadata readMetadata(File dir) {
if (!metadataFile.exists()) {
return null;
}
LocalScmMetadata result = null;
Reader reader = null;
try {
reader = ReaderFactory.newXmlReader(metadataFile);
result = new LocalScmMetadataXpp3Reader().read(reader);

try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
LocalScmMetadata result = new LocalScmMetadataXpp3Reader().read(reader);
return result;
} catch (XmlPullParserException e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Could not interpret .maven-scm-local - ignoring", e);
}
return null;
} catch (IOException e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Could not Read .maven-scm-local - ignoring", e);
LOGGER.warn("Could not read .maven-scm-local - ignoring", e);
}
} finally {
IOUtil.close(reader);
return null;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.apache.maven.scm.provider.local.command.checkout;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.nio.file.Files;
import java.util.List;

import org.apache.maven.scm.ScmFile;
Expand All @@ -29,7 +29,6 @@
import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
import org.apache.maven.scm.tck.command.checkout.CheckOutCommandTckTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -80,16 +79,13 @@ public void testMetadata() throws Exception {
// ----------------------------------------------------------------------
File metadataFile = new File(getWorkingCopy(), ".maven-scm-local");
assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
Reader reader = new FileReader(metadataFile);
LocalScmMetadata metadata;
try {
metadata = new LocalScmMetadataXpp3Reader().read(reader);
} finally {
IOUtil.close(reader);

try (Reader reader = Files.newBufferedReader(metadataFile.toPath()); ) {
LocalScmMetadata metadata = new LocalScmMetadataXpp3Reader().read(reader);
File root = new File(getRepositoryRoot() + "/" + module);
@SuppressWarnings("unchecked")
List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
assertEquals(fileNames, metadata.getRepositoryFileNames());
}
File root = new File(getRepositoryRoot() + "/" + module);
@SuppressWarnings("unchecked")
List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
assertEquals(fileNames, metadata.getRepositoryFileNames());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.apache.maven.scm.provider.local.command.update;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.nio.file.Files;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand All @@ -36,7 +36,6 @@
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.tck.command.update.UpdateCommandTckTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -130,17 +129,13 @@ public void testDeletion() throws Exception {
// ----------------------------------------------------------------------
File metadataFile = new File(getUpdatingCopy(), ".maven-scm-local");
assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
Reader reader = new FileReader(metadataFile);
LocalScmMetadata metadata;
try {
metadata = new LocalScmMetadataXpp3Reader().read(reader);
} finally {
IOUtil.close(reader);
try (Reader reader = Files.newBufferedReader(metadataFile.toPath())) {
LocalScmMetadata metadata = new LocalScmMetadataXpp3Reader().read(reader);
File root = new File(getRepositoryRoot() + "/" + moduleName);
@SuppressWarnings("unchecked")
List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
assertEquals(fileNames, metadata.getRepositoryFileNames());
}
File root = new File(getRepositoryRoot() + "/" + moduleName);
@SuppressWarnings("unchecked")
List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
assertEquals(fileNames, metadata.getRepositoryFileNames());
}

private void makeRepo(File workingDirectory) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ protected ScmResult executeBranchCommand(
throw new ScmException("This provider doesn't support branching subsets of a directory");
}

Git git = null;
try {
git = JGitUtils.openRepo(fileSet.getBasedir());
try (Git git = JGitUtils.openRepo(fileSet.getBasedir())) {
Ref branchResult = git.branchCreate().setName(branch).call();
logger.info("created [" + branchResult.getName() + "]");

Expand Down Expand Up @@ -102,8 +100,6 @@ protected ScmResult executeBranchCommand(

} catch (Exception e) {
throw new ScmException("JGit branch failed!", e);
} finally {
JGitUtils.closeRepo(git);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.maven.scm.provider.git.command.checkin.GitCheckInCommandTckTest;
import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
import org.apache.maven.scm.repository.ScmRepository;
import org.codehaus.plexus.util.IOUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Config;
Expand Down Expand Up @@ -270,10 +269,7 @@ private RevCommit getHeadCommit(Repository repository) throws Exception {
}

private void createFooJava(File fooJava) throws Exception {
FileWriter output = new FileWriter(fooJava);

PrintWriter printer = new PrintWriter(output);
try {
try (PrintWriter printer = new PrintWriter(new FileWriter(fooJava))) {
printer.println("public class Foo");
printer.println("{");

Expand All @@ -284,9 +280,6 @@ private void createFooJava(File fooJava) throws Exception {
printer.println(" }");

printer.println("}");
} finally {
IOUtil.close(output);
IOUtil.close(printer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ public static void addTarget(Commandline cl, List<File> files) throws IOExceptio
}

File targets = File.createTempFile("maven-scm-", "-targets");
PrintStream out = new PrintStream(new FileOutputStream(targets));
out.print(sb);
out.flush();
out.close();
try (PrintStream out = new PrintStream(new FileOutputStream(targets))) {
out.print(sb);
}

cl.createArg().setValue("--targets");
cl.createArg().setValue(targets.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.maven.scm.tck.command.branch;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.scm.ScmBranch;
import org.apache.maven.scm.ScmFileSet;
Expand All @@ -28,7 +30,6 @@
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void testBranchCommandTest() throws Exception {
assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));

this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
changeReadmeTxt(readmeTxt);
changeReadmeTxt(readmeTxt.toPath());

CheckInScmResult checkinResult =
getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), "commit message");
Expand Down Expand Up @@ -95,12 +96,9 @@ public void testBranchCommandTest() throws Exception {
"check readme.txt contents is from branched version", "/readme.txt", FileUtils.fileRead(readmeTxt));
}

private void changeReadmeTxt(File readmeTxt) throws Exception {
FileWriter output = new FileWriter(readmeTxt);
try {
private void changeReadmeTxt(Path readmeTxt) throws Exception {
try (Writer output = Files.newBufferedWriter(readmeTxt)) {
output.write("changed file");
} finally {
IOUtil.close(output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.util.FilenameUtils;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Assume;
import org.junit.Test;

Expand Down Expand Up @@ -238,10 +237,7 @@ public void testCheckInCommandFilesetWithBasedirOtherThanWorkingCopyRoot() throw
}

private void createFooJava(File fooJava) throws Exception {
FileWriter output = new FileWriter(fooJava);

PrintWriter printer = new PrintWriter(output);
try {
try (PrintWriter printer = new PrintWriter(new FileWriter(fooJava))) {
printer.println("public class Foo");
printer.println("{");

Expand All @@ -251,41 +247,26 @@ private void createFooJava(File fooJava) throws Exception {
printer.println(" }");

printer.println("}");
} finally {
IOUtil.close(output);
IOUtil.close(printer);
}
}

private void createBarJava(File barJava) throws Exception {
FileWriter output = new FileWriter(barJava);

PrintWriter printer = new PrintWriter(output);

printer.println("public class Bar");
printer.println("{");

printer.println(" public int bar()");
printer.println(" {");
printer.println(" return 20;");
printer.println(" }");

printer.println("}");
try (PrintWriter printer = new PrintWriter(new FileWriter(barJava))) {
printer.println("public class Bar");
printer.println("{");

printer.close();
printer.println(" public int bar()");
printer.println(" {");
printer.println(" return 20;");
printer.println(" }");

output.close();
printer.println("}");
}
}

private void changeReadmeTxt(File readmeTxt) throws Exception {
FileWriter output = null;

try {
output = new FileWriter(readmeTxt);

try (FileWriter output = new FileWriter(readmeTxt)) {
output.write("changed file");
} finally {
IOUtil.close(output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.maven.scm.tck.command.tag;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmTag;
Expand All @@ -28,7 +30,6 @@
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.command.tag.TagScmResult;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void testTagCommandTest() throws Exception {
assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));

this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
changeReadmeTxt(readmeTxt);
changeReadmeTxt(readmeTxt.toPath());

CheckInScmResult checkinResult =
getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), "commit message");
Expand Down Expand Up @@ -94,12 +95,9 @@ public void testTagCommandTest() throws Exception {
assertEquals("check readme.txt contents is from tagged version", "/readme.txt", FileUtils.fileRead(readmeTxt));
}

private void changeReadmeTxt(File readmeTxt) throws Exception {
FileWriter output = new FileWriter(readmeTxt);
try {
private void changeReadmeTxt(Path readmeTxt) throws Exception {
try (Writer output = Files.newBufferedWriter(readmeTxt)) {
output.write("changed file");
} finally {
IOUtil.close(output);
}
}
}

0 comments on commit 5c7616e

Please sign in to comment.