Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Hammer2019-05-12 10:24:41 -0400
committerMichael Keppler2019-05-15 02:00:12 -0400
commit6997c809ef8f6e89dac065d693185286d0af83f6 (patch)
tree7a8a3bbf997d7f38c59b729a9b4b23225695d585
parentf7252caf1b30cd08badd7480a8130f0e55daf723 (diff)
downloadegit-6997c809ef8f6e89dac065d693185286d0af83f6.tar.gz
egit-6997c809ef8f6e89dac065d693185286d0af83f6.tar.xz
egit-6997c809ef8f6e89dac065d693185286d0af83f6.zip
Use try-with-resources
Convert try finally block to try-with-resources Change-Id: I29a482e2bbfe5e9029b41c1fe7a5bf45308905c2 Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
-rw-r--r--org.eclipse.egit.core.junit/src/org/eclipse/egit/core/test/TestRepository.java9
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/models/ModelTestCase.java19
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java5
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java9
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java5
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java12
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java17
-rw-r--r--org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java16
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/GitTraceConfigurationDialog.java10
-rw-r--r--org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java7
10 files changed, 42 insertions, 67 deletions
diff --git a/org.eclipse.egit.core.junit/src/org/eclipse/egit/core/test/TestRepository.java b/org.eclipse.egit.core.junit/src/org/eclipse/egit/core/test/TestRepository.java
index 3043bf1a7..8377a28ed 100644
--- a/org.eclipse.egit.core.junit/src/org/eclipse/egit/core/test/TestRepository.java
+++ b/org.eclipse.egit.core.junit/src/org/eclipse/egit/core/test/TestRepository.java
@@ -437,14 +437,9 @@ public class TestRepository {
*/
public void appendFileContent(File file, String content, boolean append)
throws IOException {
- Writer fw = null;
- try {
- fw = new OutputStreamWriter(new FileOutputStream(file, append),
- "UTF-8");
+ try (Writer fw = new OutputStreamWriter(new FileOutputStream(file, append),
+ "UTF-8")) {
fw.append(content);
- } finally {
- if (fw != null)
- fw.close();
}
}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/models/ModelTestCase.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/models/ModelTestCase.java
index a66dae237..fcf0d5559 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/models/ModelTestCase.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/models/ModelTestCase.java
@@ -81,16 +81,17 @@ public abstract class ModelTestCase extends GitTestCase {
protected void assertContentEquals(IFile file, String expectedContents)
throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- file.getContents(), file.getCharset()));
- StringBuilder contentsBuilder = new StringBuilder();
- String line = reader.readLine();
- while (line != null) {
- contentsBuilder.append(line);
- contentsBuilder.append('\n');
- line = reader.readLine();
+ StringBuilder contentsBuilder;
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(
+ file.getContents(), file.getCharset()))) {
+ contentsBuilder = new StringBuilder();
+ String line = reader.readLine();
+ while (line != null) {
+ contentsBuilder.append(line);
+ contentsBuilder.append('\n');
+ line = reader.readLine();
+ }
}
- reader.close();
assertEquals(expectedContents, contentsBuilder.toString());
}
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
index 184cdc003..ffbe16c4e 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/IgnoreOperation.java
@@ -326,11 +326,8 @@ public class IgnoreOperation implements IEGitOperation {
ignoreLine = getEntry(gitIgnore, ignoreLine);
}
- FileOutputStream os = new FileOutputStream(gitIgnore, true);
- try {
+ try (FileOutputStream os = new FileOutputStream(gitIgnore, true)) {
os.write(ignoreLine.getBytes(StandardCharsets.UTF_8));
- } finally {
- os.close();
}
} catch (IOException e) {
String error = NLS.bind(CoreText.IgnoreOperation_updatingFailed,
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java
index 17e62768b..a586299bc 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/TestUtil.java
@@ -292,14 +292,9 @@ public class TestUtil {
*/
public static void appendFileContent(File file, String content, boolean append)
throws IOException {
- Writer fw = null;
- try {
- fw = new OutputStreamWriter(new FileOutputStream(file, append),
- "UTF-8");
+ try (Writer fw = new OutputStreamWriter(new FileOutputStream(file, append),
+ "UTF-8")) {
fw.append(content);
- } finally {
- if (fw != null)
- fw.close();
}
}
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java
index 27bae49a9..980ba7663 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/BranchAndResetActionTest.java
@@ -202,8 +202,7 @@ public class BranchAndResetActionTest extends LocalRepositoryTestCase {
"Add to stable");
op.execute(null);
- InputStream is = toBeDeleted.getContents();
- try {
+ try (InputStream is = toBeDeleted.getContents()) {
checkout(new String[] { LOCAL_BRANCHES, "master" });
final SWTBotShell showUndeleted = bot
.shell(UIText.NonDeletedFilesDialog_NonDeletedFilesTitle);
@@ -282,8 +281,6 @@ public class BranchAndResetActionTest extends LocalRepositoryTestCase {
UIText.NonDeletedFilesDialog_RetryDeleteButton).click();
showUndeleted.bot().waitUntil(treeEmpty, 1000, 100);
showUndeleted.close();
- } finally {
- is.close();
}
}
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java
index 6be393b08..4229466dd 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/test/team/actions/CompareActionsTest.java
@@ -204,12 +204,12 @@ public class CompareActionsTest extends LocalRepositoryTestCase {
Ref newBranch = git.checkout().setCreateBranch(true)
.setStartPoint(commitOfTag.name()).setName("toMerge")
.call();
- ByteArrayInputStream bis = new ByteArrayInputStream(
- "Modified".getBytes("UTF-8"));
- ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1)
- .getFolder(FOLDER).getFile(FILE2)
- .setContents(bis, false, false, null);
- bis.close();
+ try (ByteArrayInputStream bis = new ByteArrayInputStream(
+ "Modified".getBytes("UTF-8"))) {
+ ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1)
+ .getFolder(FOLDER).getFile(FILE2)
+ .setContents(bis, false, false, null);
+ }
git.commit().setAll(true).setMessage("To be merged").call();
git.merge().include(masterId).call();
String menuLabel = util
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java
index 6afb055d1..c59708f2d 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewGitChangeSetModelTest.java
@@ -256,11 +256,10 @@ public class SynchronizeViewGitChangeSetModelTest extends
String name = "non-workspace.txt";
File root = new File(getTestDirectory(), REPO1);
File nonWorkspace = new File(root, name);
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"));
-
- writer.append("file content");
- writer.close();
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+ Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) {
+ writer.append("file content");
+ }
// TODO Synchronize currently shows "No changes" when the only thing that has
// changed is a non-workspace file, so add another change so that this
// does not happen. When the bug is fixed, this should be removed.
@@ -282,10 +281,10 @@ public class SynchronizeViewGitChangeSetModelTest extends
String name = "non-workspace.txt";
File root = new File(getTestDirectory(), REPO1);
File nonWorkspace = new File(root, name);
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"));
- writer.append(content);
- writer.close();
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+ Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) {
+ writer.append(content);
+ }
// TODO Synchronize currently shows "No changes" when the only thing that has
// changed is a non-workspace file, so add another change so that this
// does not happen. When the bug is fixed, this should be removed.
diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java
index 0d247919a..4b2cbb814 100644
--- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java
+++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/synchronize/SynchronizeViewWorkspaceModelTest.java
@@ -296,10 +296,10 @@ public class SynchronizeViewWorkspaceModelTest extends AbstractSynchronizeViewTe
String name = "non-workspace.txt";
File root = new File(getTestDirectory(), REPO1);
File nonWorkspace = new File(root, name);
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"));
- writer.append("file content");
- writer.close();
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+ Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) {
+ writer.append("file content");
+ }
// when
launchSynchronization(INITIAL_TAG, HEAD, true);
@@ -319,10 +319,10 @@ public class SynchronizeViewWorkspaceModelTest extends AbstractSynchronizeViewTe
String name = "non-workspace.txt";
File root = new File(getTestDirectory(), REPO1);
File nonWorkspace = new File(root, name);
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"));
- writer.append(content);
- writer.close();
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
+ Files.newOutputStream(nonWorkspace.toPath()), "UTF-8"))) {
+ writer.append(content);
+ }
// when
launchSynchronization(INITIAL_TAG, HEAD, true);
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/GitTraceConfigurationDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/GitTraceConfigurationDialog.java
index 8e03ff1f5..923508adc 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/GitTraceConfigurationDialog.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/GitTraceConfigurationDialog.java
@@ -486,11 +486,8 @@ public class GitTraceConfigurationDialog extends TitleAreaDialog {
URL resource = Platform.getBundle(plugin.getPlugin())
.getResource(".options"); //$NON-NLS-1$
if (resource != null) {
- InputStream is = resource.openStream();
- try {
+ try (InputStream is = resource.openStream()) {
props.load(is);
- } finally {
- is.close();
}
}
} catch (IOException e) {
@@ -533,11 +530,8 @@ public class GitTraceConfigurationDialog extends TitleAreaDialog {
URL resource = Platform.getBundle(plugin.getPlugin())
.getResource(".options"); //$NON-NLS-1$
if (resource != null) {
- InputStream is = resource.openStream();
- try {
+ try (InputStream is = resource.openStream()) {
props.load(is);
- } finally {
- is.close();
}
}
} catch (IOException e) {
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java
index 8cc31e5d1..b5b55e0ab 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java
@@ -741,12 +741,9 @@ class CommitGraphTable {
private void writeToFile(final String fileName, String content)
throws IOException {
- Writer output = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(fileName), UTF_8));
- try {
+ try (Writer output = new BufferedWriter(new OutputStreamWriter(
+ new FileOutputStream(fileName), UTF_8))) {
output.write(content);
- } finally {
- output.close();
}
}
}

Back to the top