From ed42bad1c6dc11ca39e5edd0903390aa7371363f Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Sat, 4 Apr 2015 00:17:03 +0200 Subject: [PATCH] Use try-with-resource to close resources in GC Change-Id: I62a755a4ce839a252a5e80abf3f0d21243862376 Signed-off-by: Matthias Sohn --- .../jgit/internal/storage/file/GC.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index 48335e48c..338106f8e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@ -618,22 +618,19 @@ public class GC { */ private Set listNonHEADIndexObjects() throws CorruptObjectException, IOException { - RevWalk revWalk = null; try { if (repo.getIndexFile() == null) return Collections.emptySet(); } catch (NoWorkTreeException e) { return Collections.emptySet(); } - TreeWalk treeWalk = new TreeWalk(repo); - try { + try (TreeWalk treeWalk = new TreeWalk(repo)) { treeWalk.addTree(new DirCacheIterator(repo.readDirCache())); ObjectId headID = repo.resolve(Constants.HEAD); if (headID != null) { - revWalk = new RevWalk(repo); - treeWalk.addTree(revWalk.parseTree(headID)); - revWalk.dispose(); - revWalk = null; + try (RevWalk revWalk = new RevWalk(repo)) { + treeWalk.addTree(revWalk.parseTree(headID)); + } } treeWalk.setFilter(TreeFilter.ANY_DIFF); @@ -662,10 +659,6 @@ public class GC { } } return ret; - } finally { - if (revWalk != null) - revWalk.dispose(); - treeWalk.release(); } } @@ -689,8 +682,9 @@ public class GC { } }); - PackWriter pw = new PackWriter((pconfig == null) ? new PackConfig(repo) : pconfig, repo.newObjectReader()); - try { + try (PackWriter pw = new PackWriter( + (pconfig == null) ? new PackConfig(repo) : pconfig, + repo.newObjectReader())) { // prepare the PackWriter pw.setDeltaBaseAsOffset(true); pw.setReuseDeltaCommits(false); @@ -810,7 +804,6 @@ public class GC { } return repo.getObjectDatabase().openPack(realPack); } finally { - pw.release(); if (tmpPack != null && tmpPack.exists()) tmpPack.delete(); for (File tmpExt : tmpExts.values()) {