Use try-with-resources in o.e.dltk.ui.

Change-Id: If6d3d5b01abda0be9c62c01806aec69f515d4eaf
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateFileChange.java b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateFileChange.java
index 9a6a3f4..a29c900 100644
--- a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateFileChange.java
+++ b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateFileChange.java
@@ -31,7 +31,6 @@
 import org.eclipse.ltk.core.refactoring.Change;
 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
 
-
 public class CreateFileChange extends DLTKChange {
 
 	private String fChangeName;
@@ -46,49 +45,47 @@
 		this(path, source, encoding, IResource.NULL_STAMP);
 	}
 
-	public CreateFileChange(IPath path, String source, String encoding, long stampToRestore) {
+	public CreateFileChange(IPath path, String source, String encoding,
+			long stampToRestore) {
 		Assert.isNotNull(path, "path"); //$NON-NLS-1$
 		Assert.isNotNull(source, "source"); //$NON-NLS-1$
-		fPath= path;
-		fSource= source;
-		fEncoding= encoding;
-		fExplicitEncoding= fEncoding != null;
-		fStampToRestore= stampToRestore;
+		fPath = path;
+		fSource = source;
+		fEncoding = encoding;
+		fExplicitEncoding = fEncoding != null;
+		fStampToRestore = stampToRestore;
 	}
 
 	/*
-	private CreateFileChange(IPath path, String source, String encoding, long stampToRestore, boolean explicit) {
-		Assert.isNotNull(path, "path"); //$NON-NLS-1$
-		Assert.isNotNull(source, "source"); //$NON-NLS-1$
-		Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
-		fPath= path;
-		fSource= source;
-		fEncoding= encoding;
-		fStampToRestore= stampToRestore;
-		fExplicitEncoding= explicit;
-	}
-	*/
+	 * private CreateFileChange(IPath path, String source, String encoding, long
+	 * stampToRestore, boolean explicit) { Assert.isNotNull(path, "path");
+	 * //$NON-NLS-1$ Assert.isNotNull(source, "source"); //$NON-NLS-1$
+	 * Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$ fPath= path;
+	 * fSource= source; fEncoding= encoding; fStampToRestore= stampToRestore;
+	 * fExplicitEncoding= explicit; }
+	 */
 
 	protected void setEncoding(String encoding, boolean explicit) {
 		Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
-		fEncoding= encoding;
-		fExplicitEncoding= explicit;
+		fEncoding = encoding;
+		fExplicitEncoding = explicit;
 	}
 
 	@Override
 	public String getName() {
 		if (fChangeName == null)
-			return Messages.format(NLSChangesMessages.createFile_Create_file, fPath.toOSString());
+			return Messages.format(NLSChangesMessages.createFile_Create_file,
+					fPath.toOSString());
 		else
 			return fChangeName;
 	}
 
 	public void setName(String name) {
-		fChangeName= name;
+		fChangeName = name;
 	}
 
 	protected void setSource(String source) {
-		fSource= source;
+		fSource = source;
 	}
 
 	protected String getSource() {
@@ -96,7 +93,7 @@
 	}
 
 	protected void setPath(IPath path) {
-		fPath= path;
+		fPath = path;
 	}
 
 	protected IPath getPath() {
@@ -110,22 +107,22 @@
 
 	@Override
 	public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
-		RefactoringStatus result= new RefactoringStatus();
-		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
+		RefactoringStatus result = new RefactoringStatus();
+		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
 
-		URI location= file.getLocationURI();
+		URI location = file.getLocationURI();
 		if (location == null) {
 			result.addFatalError(Messages.format(
-				NLSChangesMessages.CreateFileChange_error_unknownLocation,
-				file.getFullPath().toString()));
+					NLSChangesMessages.CreateFileChange_error_unknownLocation,
+					file.getFullPath().toString()));
 			return result;
 		}
 
-		IFileInfo jFile= EFS.getStore(location).fetchInfo();
+		IFileInfo jFile = EFS.getStore(location).fetchInfo();
 		if (jFile.exists()) {
 			result.addFatalError(Messages.format(
-				NLSChangesMessages.CreateFileChange_error_exists,
-				file.getFullPath().toString()));
+					NLSChangesMessages.CreateFileChange_error_exists,
+					file.getFullPath().toString()));
 			return result;
 		}
 		return result;
@@ -134,22 +131,22 @@
 	@Override
 	public Change perform(IProgressMonitor pm) throws CoreException {
 
-		InputStream is= null;
 		try {
 			pm.beginTask(NLSChangesMessages.createFile_creating_resource, 3);
 
 			initializeEncoding();
-			IFile file= getOldFile(new SubProgressMonitor(pm, 1));
+			IFile file = getOldFile(new SubProgressMonitor(pm, 1));
 			/*
-			if (file.exists()) {
-				CompositeChange composite= new CompositeChange(getName());
-				composite.add(new DeleteFileChange(file));
-				composite.add(new CreateFileChange(fPath, fSource, fEncoding, fStampToRestore, fExplicitEncoding));
-				pm.worked(1);
-				return composite.perform(new SubProgressMonitor(pm, 1));
-			} else { */
-			try {
-				is= new ByteArrayInputStream(fSource.getBytes(fEncoding));
+			 * if (file.exists()) { CompositeChange composite= new
+			 * CompositeChange(getName()); composite.add(new
+			 * DeleteFileChange(file)); composite.add(new
+			 * CreateFileChange(fPath, fSource, fEncoding, fStampToRestore,
+			 * fExplicitEncoding)); pm.worked(1); return composite.perform(new
+			 * SubProgressMonitor(pm, 1)); } else {
+			 */
+			try (InputStream is = new ByteArrayInputStream(
+					fSource.getBytes(fEncoding))) {
+
 				file.create(is, false, new SubProgressMonitor(pm, 1));
 				if (fStampToRestore != IResource.NULL_STAMP) {
 					file.revertModificationStamp(fStampToRestore);
@@ -162,16 +159,12 @@
 				return new DeleteFileChange(file);
 			} catch (UnsupportedEncodingException e) {
 				throw new ModelException(e, IModelStatusConstants.IO_EXCEPTION);
+			} catch (IOException ioe) {
+				throw new ModelException(ioe,
+						IModelStatusConstants.IO_EXCEPTION);
 			}
 		} finally {
-			try {
-				if (is != null)
-					is.close();
-			} catch (IOException ioe) {
-				throw new ModelException(ioe, IModelStatusConstants.IO_EXCEPTION);
-			} finally {
-				pm.done();
-			}
+			pm.done();
 		}
 	}
 
@@ -186,27 +179,28 @@
 
 	private void initializeEncoding() {
 		if (fEncoding == null) {
-			fExplicitEncoding= false;
-			IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
+			fExplicitEncoding = false;
+			IFile file = ResourcesPlugin.getWorkspace().getRoot()
+					.getFile(fPath);
 			if (file != null) {
 				try {
 					if (file.exists()) {
-						fEncoding= file.getCharset(false);
+						fEncoding = file.getCharset(false);
 						if (fEncoding == null) {
-							fEncoding= file.getCharset(true);
+							fEncoding = file.getCharset(true);
 						} else {
-							fExplicitEncoding= true;
+							fExplicitEncoding = true;
 						}
 					} else {
-						fEncoding= file.getCharset(true);
+						fEncoding = file.getCharset(true);
 					}
 				} catch (CoreException e) {
-					fEncoding= ResourcesPlugin.getEncoding();
-					fExplicitEncoding= true;
+					fEncoding = ResourcesPlugin.getEncoding();
+					fExplicitEncoding = true;
 				}
 			} else {
-				fEncoding= ResourcesPlugin.getEncoding();
-				fExplicitEncoding= true;
+				fEncoding = ResourcesPlugin.getEncoding();
+				fExplicitEncoding = true;
 			}
 		}
 		Assert.isNotNull(fEncoding);
diff --git a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateTextFileChange.java b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
index a9a172f..beedc47 100644
--- a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
+++ b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/nls/changes/CreateTextFileChange.java
@@ -1,11 +1,10 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
  * http://www.eclipse.org/legal/epl-v10.html
  *
- 
  *******************************************************************************/
 package org.eclipse.dltk.internal.corext.refactoring.nls.changes;
 
@@ -20,30 +19,30 @@
 import org.eclipse.dltk.core.ModelException;
 import org.eclipse.dltk.internal.corext.refactoring.nls.NLSUtil;
 
-
 public class CreateTextFileChange extends CreateFileChange {
-	
+
 	private final String fTextType;
-	
-	public CreateTextFileChange(IPath path, String source, String encoding, String textType) {
+
+	public CreateTextFileChange(IPath path, String source, String encoding,
+			String textType) {
 		super(path, source, encoding);
-		fTextType= textType;
+		fTextType = textType;
 	}
-	
+
 	public String getTextType() {
 		return fTextType;
 	}
-	
+
 	public String getCurrentContent() throws ModelException {
-		IFile file= getOldFile(new NullProgressMonitor());
-		if (! file.exists())
+		IFile file = getOldFile(new NullProgressMonitor());
+		if (!file.exists())
 			return ""; //$NON-NLS-1$
-		InputStream stream= null;
-		try{
-			stream= file.getContents();
-			String c= NLSUtil.readString(stream);
-			return (c == null) ? "": c; //$NON-NLS-1$
-		} catch (CoreException e){
+		InputStream stream = null;
+		try {
+			stream = file.getContents();
+			String c = NLSUtil.readString(stream);
+			return (c == null) ? "" : c; //$NON-NLS-1$
+		} catch (CoreException e) {
 			throw new ModelException(e, IModelStatusConstants.CORE_EXCEPTION);
 		} finally {
 			try {
@@ -53,9 +52,8 @@
 			}
 		}
 	}
-	
+
 	public String getPreview() {
 		return getSource();
 	}
 }
-
diff --git a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/reorg/ModelElementTransfer.java b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/reorg/ModelElementTransfer.java
index 78110f1..0305f6e 100644
--- a/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/reorg/ModelElementTransfer.java
+++ b/core/plugins/org.eclipse.dltk.ui/core refactoring/org/eclipse/dltk/internal/corext/refactoring/reorg/ModelElementTransfer.java
@@ -19,19 +19,19 @@
 import org.eclipse.swt.dnd.ByteArrayTransfer;
 import org.eclipse.swt.dnd.TransferData;
 
-
 public class ModelElementTransfer extends ByteArrayTransfer {
 
 	/**
 	 * Singleton instance.
 	 */
-	private static final ModelElementTransfer fInstance= new ModelElementTransfer();
+	private static final ModelElementTransfer fInstance = new ModelElementTransfer();
 
 	// Create a unique ID to make sure that different Eclipse
 	// applications use different "types" of <code>ScriptElementTransfer</code>
-	private static final String TYPE_NAME= "model-element-transfer-format:" + System.currentTimeMillis() + ":" + fInstance.hashCode(); //$NON-NLS-2$//$NON-NLS-1$
+	private static final String TYPE_NAME = "model-element-transfer-format:" //$NON-NLS-1$
+			+ System.currentTimeMillis() + ":" + fInstance.hashCode(); //$NON-NLS-1$
 
-	private static final int TYPEID= registerType(TYPE_NAME);
+	private static final int TYPEID = registerType(TYPE_NAME);
 
 	private ModelElementTransfer() {
 	}
@@ -49,6 +49,7 @@
 	protected int[] getTypeIds() {
 		return new int[] { TYPEID };
 	}
+
 	@Override
 	protected String[] getTypeNames() {
 		return new String[] { TYPE_NAME };
@@ -59,54 +60,47 @@
 		if (!(data instanceof IModelElement[]))
 			return;
 
-		IModelElement[] modelElements= (IModelElement[]) data;
+		IModelElement[] modelElements = (IModelElement[]) data;
 		/*
-		 * The element serialization format is:
-		 *  (int) number of element
-		 * Then, the following for each element:
-		 *  (String) handle identifier
+		 * The element serialization format is: (int) number of element Then,
+		 * the following for each element: (String) handle identifier
 		 */
 
-		try {
-			ByteArrayOutputStream out= new ByteArrayOutputStream();
-			DataOutputStream dataOut= new DataOutputStream(out);
+		try (ByteArrayOutputStream out = new ByteArrayOutputStream();
+				DataOutputStream dataOut = new DataOutputStream(out)) {
 
-			//write the number of elements
+			// write the number of elements
 			dataOut.writeInt(modelElements.length);
 
-			//write each element
-			for (int i= 0; i < modelElements.length; i++) {
+			// write each element
+			for (int i = 0; i < modelElements.length; i++) {
 				writeScriptElement(dataOut, modelElements[i]);
 			}
 
-			//cleanup
-			dataOut.close();
-			out.close();
-			byte[] bytes= out.toByteArray();
+			byte[] bytes = out.toByteArray();
 			super.javaToNative(bytes, transferData);
 		} catch (IOException e) {
-			//it's best to send nothing if there were problems
+			// it's best to send nothing if there were problems
 		}
 	}
 
 	@Override
 	protected Object nativeToJava(TransferData transferData) {
 		/*
-		 * The element serialization format is:
-		 *  (int) number of element
-		 * Then, the following for each element:
-		 *  (String) handle identifier
+		 * The element serialization format is: (int) number of element Then,
+		 * the following for each element: (String) handle identifier
 		 */
 
-		byte[] bytes= (byte[]) super.nativeToJava(transferData);
+		byte[] bytes = (byte[]) super.nativeToJava(transferData);
 		if (bytes == null)
 			return null;
-		DataInputStream in= new DataInputStream(new ByteArrayInputStream(bytes));
+		DataInputStream in = new DataInputStream(
+				new ByteArrayInputStream(bytes));
 		try {
-			int count= in.readInt();
-			IModelElement[] results= new IModelElement[count];
-			for (int i= 0; i < count; i++) {
-				results[i]= readScriptElement(in);
+			int count = in.readInt();
+			IModelElement[] results = new IModelElement[count];
+			for (int i = 0; i < count; i++) {
+				results[i] = readScriptElement(in);
 			}
 			return results;
 		} catch (IOException e) {
@@ -114,12 +108,14 @@
 		}
 	}
 
-	private IModelElement readScriptElement(DataInputStream dataIn) throws IOException {
-		String handleIdentifier= dataIn.readUTF();
+	private IModelElement readScriptElement(DataInputStream dataIn)
+			throws IOException {
+		String handleIdentifier = dataIn.readUTF();
 		return DLTKCore.create(handleIdentifier);
 	}
 
-	private static void writeScriptElement(DataOutputStream dataOut, IModelElement element) throws IOException {
+	private static void writeScriptElement(DataOutputStream dataOut,
+			IModelElement element) throws IOException {
 		dataOut.writeUTF(element.getHandleIdentifier());
 	}
 }
diff --git a/core/plugins/org.eclipse.dltk.ui/src corext/org/eclipse/dltk/internal/corext/util/History.java b/core/plugins/org.eclipse.dltk.ui/src corext/org/eclipse/dltk/internal/corext/util/History.java
index b90be50..67430fb 100644
--- a/core/plugins/org.eclipse.dltk.ui/src corext/org/eclipse/dltk/internal/corext/util/History.java
+++ b/core/plugins/org.eclipse.dltk.ui/src corext/org/eclipse/dltk/internal/corext/util/History.java
@@ -177,9 +177,7 @@
 		IPath stateLocation = DLTKUIPlugin.getDefault().getStateLocation()
 				.append(fFileName);
 		File file = new File(stateLocation.toOSString());
-		OutputStream out = null;
-		try {
-			out = new FileOutputStream(file);
+		try (OutputStream out = new FileOutputStream(file)) {
 			save(out);
 		} catch (IOException e) {
 			DLTKUIPlugin.log(e);
@@ -189,14 +187,6 @@
 			// The XML library can be misconficgured (e.g. via
 			// -Djava.endorsed.dirs=C:\notExisting\xerces-2_7_1)
 			DLTKUIPlugin.log(e);
-		} finally {
-			try {
-				if (out != null) {
-					out.close();
-				}
-			} catch (IOException e) {
-				DLTKUIPlugin.log(e);
-			}
 		}
 	}
 
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/workingsets/WorkingSetActionGroup.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/workingsets/WorkingSetActionGroup.java
index ead6dcb..c01af07 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/workingsets/WorkingSetActionGroup.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/workingsets/WorkingSetActionGroup.java
@@ -27,6 +27,7 @@
 	
 	private IViewSite fSite;
 	private ISelectionChangedListener fLazyInitializer= new  ISelectionChangedListener() {
+		@Override
 		public void selectionChanged(SelectionChangedEvent event) {
 			ISelectionProvider selectionProvider= fSite.getSelectionProvider();
 			selectionProvider.removeSelectionChangedListener(fLazyInitializer);
@@ -60,6 +61,7 @@
 		fSite.getSelectionProvider().addSelectionChangedListener(fLazyInitializer);
 	}
 	
+	@Override
 	public void dispose() {
 		ISelectionProvider selectionProvider= fSite.getSelectionProvider();
 		
@@ -77,6 +79,7 @@
 		}
 	}
 	
+	@Override
 	public void fillContextMenu(IMenuManager menu) {
 		super.fillContextMenu(menu);
 		menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, new Separator(GROUP_WORKINGSETS));
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/documentation/AbstractDocumentationResponse.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/documentation/AbstractDocumentationResponse.java
index d4e50b0..b176a48 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/documentation/AbstractDocumentationResponse.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/documentation/AbstractDocumentationResponse.java
@@ -20,8 +20,8 @@
 /**
  * @since 2.0
  */
-public abstract class AbstractDocumentationResponse implements
-		IDocumentationResponse {
+public abstract class AbstractDocumentationResponse
+		implements IDocumentationResponse {
 
 	private final Object object;
 
@@ -54,15 +54,9 @@
 
 	@Override
 	public String getText() throws IOException {
-		final Reader reader = getReader();
-		try {
+
+		try (final Reader reader = getReader()) {
 			return DocumentationUtils.readAll(reader);
-		} finally {
-			try {
-				reader.close();
-			} catch (IOException e) {
-				// ignore close exception
-			}
 		}
 	}
 
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeTemplateBlock.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeTemplateBlock.java
index 5c3a380..821b5eb 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeTemplateBlock.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeTemplateBlock.java
@@ -575,18 +575,13 @@
 			TemplateReaderWriter reader = new TemplateReaderWriter();
 			File file = new File(path);
 			if (file.exists()) {
-				InputStream input = new BufferedInputStream(
-						new FileInputStream(file));
-				try {
+
+				try (InputStream input = new BufferedInputStream(
+						new FileInputStream(file))) {
 					TemplatePersistenceData[] datas = reader.read(input, null);
 					for (int i = 0; i < datas.length; i++) {
 						updateTemplate(datas[i]);
 					}
-				} finally {
-					try {
-						input.close();
-					} catch (IOException x) {
-					}
 				}
 			}
 
@@ -705,20 +700,13 @@
 		}
 
 		if (!file.exists() || confirmOverwrite(file)) {
-			OutputStream output = null;
-			try {
-				output = new BufferedOutputStream(new FileOutputStream(file));
+			try (OutputStream output = new BufferedOutputStream(
+					new FileOutputStream(file))) {
+
 				TemplateReaderWriter writer = new TemplateReaderWriter();
 				writer.save(templates, output);
 				output.close();
 			} catch (IOException e) {
-				if (output != null) {
-					try {
-						output.close();
-					} catch (IOException e2) {
-						// ignore
-					}
-				}
 				openWriteErrorDialog();
 			}
 		}
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/UserLibraryPreferencePage.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/UserLibraryPreferencePage.java
index 062eee1..1c5f1fd 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/UserLibraryPreferencePage.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/UserLibraryPreferencePage.java
@@ -594,8 +594,8 @@
 
 		protected static void saveLibraries(List libraries, File file,
 				String encoding, IProgressMonitor monitor) throws IOException {
-			OutputStream stream = new FileOutputStream(file);
-			try {
+
+			try (OutputStream stream = new FileOutputStream(file)) {
 				DocumentBuilder docBuilder = null;
 				DocumentBuilderFactory factory = DocumentBuilderFactory
 						.newInstance();
@@ -674,11 +674,6 @@
 			} catch (TransformerException e) {
 				throw new IOException(e.getMessage());
 			} finally {
-				try {
-					stream.close();
-				} catch (IOException e) {
-					// ignore
-				}
 				if (monitor != null) {
 					monitor.done();
 				}