diff options
author | Alexander Kurtakov | 2016-08-30 06:48:11 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2016-08-30 06:48:11 +0000 |
commit | 7a5dc740ad559cb7cca4742589baa20ff3731cba (patch) | |
tree | ca338da9696fd415b0334f438477f14e6e070414 | |
parent | 092ec6621be4d56784296159cfedfda64fa7f240 (diff) | |
download | eclipse.platform.ui-7a5dc740ad559cb7cca4742589baa20ff3731cba.tar.gz eclipse.platform.ui-7a5dc740ad559cb7cca4742589baa20ff3731cba.tar.xz eclipse.platform.ui-7a5dc740ad559cb7cca4742589baa20ff3731cba.zip |
Bug 500469 - Fix try-with-resources warnings in o.e.ui.tests.harness
Some formatting and multi-catch together with the main fixes.
Change-Id: I69d02e774967b0140404a796f25d6fc79cde2e07
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
3 files changed, 18 insertions, 75 deletions
diff --git a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java index 8161e87b321..cf3acddba90 100644 --- a/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java +++ b/tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/FileTool.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2014 IBM Corporation and others. + * Copyright (c) 2000, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -57,25 +57,8 @@ public class FileTool { String entryName = entry.getName(); File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar)); file.getParentFile().mkdirs(); - InputStream src = null; - OutputStream dst = null; - try { - src = zipFile.getInputStream(entry); - dst = new FileOutputStream(file); + try (InputStream src = zipFile.getInputStream(entry); OutputStream dst= new FileOutputStream(file)){ transferData(src, dst); - } finally { - if(dst != null){ - try { - dst.close(); - } catch(IOException e){ - } - } - if(src != null){ - try { - src.close(); - } catch(IOException e){ - } - } } } } finally { @@ -109,25 +92,8 @@ public class FileTool { */ public static void transferData(File source, File destination) throws IOException { destination.getParentFile().mkdirs(); - InputStream is = null; - OutputStream os = null; - try { - is = new FileInputStream(source); - os = new FileOutputStream(destination); + try (InputStream is = new FileInputStream(source); OutputStream os = new FileOutputStream(destination)) { transferData(is, os); - } finally { - if(os != null){ - try { - os.close(); - } catch(IOException e){ - } - } - if(is != null){ - try { - is.close(); - } catch(IOException e){ - } - } } } /** @@ -177,10 +143,10 @@ public class FileTool { } public static StringBuffer read(String fileName) throws IOException { - FileReader reader = new FileReader(fileName); - StringBuffer result = read(reader); - reader.close(); - return result; + try (FileReader reader = new FileReader(fileName)) { + StringBuffer result = read(reader); + return result; + } } public static StringBuffer read(Reader reader) throws IOException { @@ -202,14 +168,8 @@ public class FileTool { } public static void write(String fileName, StringBuffer content) throws IOException { - Writer writer= new FileWriter(fileName); - try { + try (Writer writer = new FileWriter(fileName)) { writer.write(content.toString()); - } finally { - try { - writer.close(); - } catch (IOException e) { - } } } } diff --git a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/extension/TestContentProvider.java b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/extension/TestContentProvider.java index fccb4f115fd..7c7aa065611 100644 --- a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/extension/TestContentProvider.java +++ b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/extension/TestContentProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2015 IBM Corporation and others. + * Copyright (c) 2003, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -78,8 +78,7 @@ public class TestContentProvider implements ITreeContentProvider, if (project != null && project.isAccessible()) { IFile modelFile = project.getFile(MODEL_FILE_PATH); if (rootElements.containsKey(modelFile)) { - TestExtensionTreeData model = (TestExtensionTreeData) rootElements - .get(modelFile); + TestExtensionTreeData model = (TestExtensionTreeData) rootElements.get(modelFile); return model != null ? model.getChildren() : NO_CHILDREN; } else { TestExtensionTreeData model = updateModel(modelFile); @@ -110,8 +109,7 @@ public class TestContentProvider implements ITreeContentProvider, private TestExtensionTreeData updateModel(IFile modelFile) { Properties model = new Properties(); if (modelFile.exists()) { - try { - InputStream is = modelFile.getContents(); + try (InputStream is = modelFile.getContents()) { model.load(is); is.close(); TestExtensionTreeData root = new TestExtensionTreeData(null, @@ -119,8 +117,7 @@ public class TestContentProvider implements ITreeContentProvider, _modelRoot = root; rootElements.put(modelFile, root); return root; - } catch (IOException e) { - } catch (CoreException e) { + } catch (IOException | CoreException e) { } } else { rootElements.remove(modelFile); diff --git a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/util/ProjectUnzipUtil.java b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/util/ProjectUnzipUtil.java index fd526ea3f6a..1a62b83c949 100644 --- a/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/util/ProjectUnzipUtil.java +++ b/tests/org.eclipse.ui.tests.navigator/src/org/eclipse/ui/tests/navigator/util/ProjectUnzipUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2009 IBM Corporation and others. + * Copyright (c) 2005, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -61,10 +61,7 @@ public class ProjectUnzipUtil { expandZip(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); buildProjects(); - } catch (CoreException e) { - e.printStackTrace(); - return false; - } catch (IOException e) { + } catch (CoreException | IOException e) { e.printStackTrace(); return false; } @@ -76,10 +73,7 @@ public class ProjectUnzipUtil { try { expandZip(); ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null); - } catch (CoreException e) { - e.printStackTrace(); - return false; - } catch (IOException e) { + } catch (CoreException | IOException e) { e.printStackTrace(); return false; } @@ -93,16 +87,10 @@ public class ProjectUnzipUtil { private void expandZip() throws CoreException, IOException { IProgressMonitor monitor = getProgessMonitor(); - ZipFile zipFile = null; - try { - zipFile = new ZipFile(zipLocation.toFile()); - } catch (IOException e1) { - throw e1; - } - try { - Enumeration entries = zipFile.entries(); + try (ZipFile zipFile = new ZipFile(zipLocation.toFile())) { + Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { - ZipEntry entry = (ZipEntry) entries.nextElement(); + ZipEntry entry = entries.nextElement(); monitor.subTask(entry.getName()); File aFile = computeLocation(entry.getName()).toFile(); File parentFile = null; @@ -124,8 +112,6 @@ public class ProjectUnzipUtil { } monitor.worked(1); } - } finally { - zipFile.close(); } } |