Convert to new AutoCloseable instances coming in JGit 4.0

Repository, RevWalk, and friends have been converted to AutoCloseable.
Use them in try-with-resources blocks in order to avoid warnings about
unclosed resources or calls to deprecated release() methods. Where a
larger rewrite might have been possible to reduce try/finally blocks,
err on the side of keeping the same behavior.

The proximate goal of this change was to eliminate all Eclipse
warnings about deprecated method calls (namely release()) and leaked
resources. Some of these warnings were in fact potential leaks;
Eclipse finding such cases is a side benefit of having these classes
implement AutoCloseable. However, this change certainly does not cover
all cases where try-with-resources could have been used, where it was
not causing a leak.

Fixes all such warnings in plugin submodules as well.

Change-Id: I5d151996ae012d0e8fdfa27cce8cf5e2bfc856a2
This commit is contained in:
Dave Borowitz
2015-03-10 16:10:59 -07:00
committed by David Pursehouse
parent f4fff188ba
commit 7273df290a
55 changed files with 1107 additions and 1446 deletions

View File

@@ -82,53 +82,45 @@ class ListDashboards implements RestReadView<ProjectResource> {
private List<DashboardInfo> scan(ProjectControl ctl, String project,
boolean setDefault) throws ResourceNotFoundException, IOException {
Repository git;
try {
git = gitManager.openRepository(ctl.getProject().getNameKey());
Project.NameKey projectName = ctl.getProject().getNameKey();
try (Repository git = gitManager.openRepository(projectName);
RevWalk rw = new RevWalk(git)) {
List<DashboardInfo> all = Lists.newArrayList();
for (Ref ref : git.getRefDatabase().getRefs(REFS_DASHBOARDS).values()) {
if (ctl.controlForRef(ref.getName()).canRead()) {
all.addAll(scanDashboards(ctl.getProject(), git, rw, ref,
project, setDefault));
}
}
return all;
} catch (RepositoryNotFoundException e) {
throw new ResourceNotFoundException();
}
try {
RevWalk rw = new RevWalk(git);
try {
List<DashboardInfo> all = Lists.newArrayList();
for (Ref ref : git.getRefDatabase().getRefs(REFS_DASHBOARDS).values()) {
if (ctl.controlForRef(ref.getName()).canRead()) {
all.addAll(scanDashboards(ctl.getProject(), git, rw, ref,
project, setDefault));
}
}
return all;
} finally {
rw.release();
}
} finally {
git.close();
}
}
private List<DashboardInfo> scanDashboards(Project definingProject,
Repository git, RevWalk rw, Ref ref, String project, boolean setDefault)
throws IOException {
List<DashboardInfo> list = Lists.newArrayList();
TreeWalk tw = new TreeWalk(rw.getObjectReader());
tw.addTree(rw.parseTree(ref.getObjectId()));
tw.setRecursive(true);
while (tw.next()) {
if (tw.getFileMode(0) == FileMode.REGULAR_FILE) {
try {
list.add(DashboardsCollection.parse(
definingProject,
ref.getName().substring(REFS_DASHBOARDS.length()),
tw.getPathString(),
new BlobBasedConfig(null, git, tw.getObjectId(0)),
project,
setDefault));
} catch (ConfigInvalidException e) {
log.warn(String.format(
"Cannot parse dashboard %s:%s:%s: %s",
definingProject.getName(), ref.getName(), tw.getPathString(),
e.getMessage()));
try (TreeWalk tw = new TreeWalk(rw.getObjectReader())) {
tw.addTree(rw.parseTree(ref.getObjectId()));
tw.setRecursive(true);
while (tw.next()) {
if (tw.getFileMode(0) == FileMode.REGULAR_FILE) {
try {
list.add(DashboardsCollection.parse(
definingProject,
ref.getName().substring(REFS_DASHBOARDS.length()),
tw.getPathString(),
new BlobBasedConfig(null, git, tw.getObjectId(0)),
project,
setDefault));
} catch (ConfigInvalidException e) {
log.warn(String.format(
"Cannot parse dashboard %s:%s:%s: %s",
definingProject.getName(), ref.getName(), tw.getPathString(),
e.getMessage()));
}
}
}
}