From 5c70be00856d5375485e6f062b6e1e09a606601f Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 13 Mar 2018 11:44:23 +0900 Subject: Open auto-closeable resources in try-with-resource When an auto-closeable resources is not opened in try-with-resource, the warning "should be managed by try-with-resource" is emitted by Eclipse. Fix the ones that can be silenced simply by moving the declaration of the variable into a try-with-resource. In cases where we explicitly call the close() method, for example in tests where we are testing specific behavior caused by the close(), suppress the warning. Leave the ones that will require more significant refcactoring to fix. They can be done in separate commits that can be reviewed and tested in isolation. Change-Id: I9682cd20fb15167d3c7f9027cecdc82bc50b83c4 Signed-off-by: David Pursehouse --- org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Version.java | 11 +++-------- .../src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java | 5 +---- 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'org.eclipse.jgit.pgm/src/org/eclipse') diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Version.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Version.java index 5efc03b287..58acc5cafd 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Version.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Version.java @@ -96,14 +96,9 @@ class Version extends TextBuiltin { } private static String getBundleVersion(URL url) { - try { - InputStream is = url.openStream(); - try { - Manifest manifest = new Manifest(is); - return manifest.getMainAttributes().getValue("Bundle-Version"); //$NON-NLS-1$ - } finally { - is.close(); - } + try (InputStream is = url.openStream()) { + Manifest manifest = new Manifest(is); + return manifest.getMainAttributes().getValue("Bundle-Version"); //$NON-NLS-1$ } catch (IOException e) { // do nothing - will return null } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java index 83b7bcea26..6378e8d220 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java @@ -288,11 +288,8 @@ class TextHashFunctions extends TextBuiltin { else rb.findGitDir(dir); - Repository repo = rb.build(); - try { + try (Repository repo = rb.build()) { run(repo); - } finally { - repo.close(); } } } -- cgit v1.2.3