Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArne Deutsch2017-07-17 16:20:05 +0000
committerAlexander Kurtakov2017-07-18 07:08:50 +0000
commit51c20319307866951c92a5d620fd8f97c2f2da52 (patch)
tree30420a4ea9af401519cbb34520feaa7951f726a6
parent87a89a1a9f0e2a2845c59de13a43807d8c093938 (diff)
downloadeclipse.platform.text-51c20319307866951c92a5d620fd8f97c2f2da52.tar.gz
eclipse.platform.text-51c20319307866951c92a5d620fd8f97c2f2da52.tar.xz
eclipse.platform.text-51c20319307866951c92a5d620fd8f97c2f2da52.zip
Bug 519765 - org.eclipse.ui.editors should use try-with-resourcesI20170718-0355
Use try-with-resources consequently in org.eclipse.ui.editors. Change-Id: Ib58f5d12a365e109ee5d554d91af52eb00ce8e00 Signed-off-by: Arne Deutsch <arne@idedeluxe.com>
-rw-r--r--org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java26
-rw-r--r--org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/StorageDocumentProvider.java30
-rw-r--r--org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java28
3 files changed, 19 insertions, 65 deletions
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
index e875921e5dd..5bcbc787e5b 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/FileDocumentProvider.java
@@ -377,14 +377,9 @@ public class FileDocumentProvider extends StorageDocumentProvider {
protected boolean setDocumentContent(IDocument document, IEditorInput editorInput) throws CoreException {
if (editorInput instanceof IFileEditorInput) {
IFile file= ((IFileEditorInput) editorInput).getFile();
- InputStream stream= file.getContents(false);
- try {
+ try (InputStream stream= file.getContents(false)) {
setDocumentContent(document, stream);
- } finally {
- try {
- stream.close();
- } catch (IOException x) {
- }
+ } catch (IOException x) {
}
return true;
}
@@ -395,9 +390,7 @@ public class FileDocumentProvider extends StorageDocumentProvider {
protected boolean setDocumentContent(IDocument document, IEditorInput editorInput, String encoding) throws CoreException {
if (editorInput instanceof IFileEditorInput) {
IFile file= ((IFileEditorInput) editorInput).getFile();
- InputStream contentStream= file.getContents(false);
- try {
-
+ try (InputStream contentStream= file.getContents(false)) {
FileInfo info= (FileInfo)getElementInfo(editorInput);
boolean removeBOM= false;
if (CHARSET_UTF_8.equals(encoding)) {
@@ -428,11 +421,6 @@ public class FileDocumentProvider extends StorageDocumentProvider {
String message= (ex.getMessage() != null ? ex.getMessage() : ""); //$NON-NLS-1$
IStatus s= new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
- } finally {
- try {
- contentStream.close();
- } catch (IOException e1) {
- }
}
return true;
}
@@ -666,8 +654,7 @@ public class FileDocumentProvider extends StorageDocumentProvider {
return encoding;
// Probe content
- Reader reader= new DocumentReader(document);
- try {
+ try (Reader reader= new DocumentReader(document)) {
QualifiedName[] options= new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
IContentDescription description= Platform.getContentTypeManager().getDescriptionFor(reader, targetFile.getName(), options);
if (description != null) {
@@ -677,11 +664,6 @@ public class FileDocumentProvider extends StorageDocumentProvider {
}
} catch (IOException ex) {
// continue with next strategy
- } finally {
- try {
- reader.close();
- } catch (IOException x) {
- }
}
// Use file's encoding if the file has a BOM
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/StorageDocumentProvider.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/StorageDocumentProvider.java
index 1434673bd20..66514145905 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/StorageDocumentProvider.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/StorageDocumentProvider.java
@@ -191,14 +191,9 @@ public class StorageDocumentProvider extends AbstractDocumentProvider implements
protected boolean setDocumentContent(IDocument document, IEditorInput editorInput, String encoding) throws CoreException {
if (editorInput instanceof IStorageEditorInput) {
IStorage storage= ((IStorageEditorInput) editorInput).getStorage();
- InputStream stream= storage.getContents();
- try {
+ try (InputStream stream= storage.getContents()) {
setDocumentContent(document, stream, encoding);
- } finally {
- try {
- stream.close();
- } catch (IOException x) {
- }
+ } catch (IOException x) {
}
return true;
}
@@ -409,17 +404,17 @@ public class StorageDocumentProvider extends AbstractDocumentProvider implements
public IContentType getContentType(Object element) throws CoreException {
if (element instanceof IStorageEditorInput) {
IStorage storage= ((IStorageEditorInput) element).getStorage();
- Reader reader= null;
- InputStream stream= null;
try {
IContentDescription desc;
IDocument document= getDocument(element);
if (document != null) {
- reader= new DocumentReader(document);
- desc= Platform.getContentTypeManager().getDescriptionFor(reader, storage.getName(), NO_PROPERTIES);
+ try (Reader reader= new DocumentReader(document)) {
+ desc= Platform.getContentTypeManager().getDescriptionFor(reader, storage.getName(), NO_PROPERTIES);
+ }
} else {
- stream= storage.getContents();
- desc= Platform.getContentTypeManager().getDescriptionFor(stream, storage.getName(), NO_PROPERTIES);
+ try (InputStream stream= storage.getContents()) {
+ desc= Platform.getContentTypeManager().getDescriptionFor(stream, storage.getName(), NO_PROPERTIES);
+ }
}
if (desc != null && desc.getContentType() != null)
return desc.getContentType();
@@ -436,15 +431,6 @@ public class StorageDocumentProvider extends AbstractDocumentProvider implements
else
message= TextEditorMessages.StorageDocumentProvider_getContentDescription;
throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, x));
- } finally {
- try {
- // Note: either 'reader' or 'stream' is null
- if (reader != null)
- reader.close();
- if (stream != null)
- stream.close();
- } catch (IOException x) {
- }
}
}
return super.getContentType(element);
diff --git a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java
index d7479dd8667..0a78eadd605 100644
--- a/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java
+++ b/org.eclipse.ui.editors/src/org/eclipse/ui/editors/text/templates/ContributionTemplateStore.java
@@ -132,19 +132,17 @@ public class ContributionTemplateStore extends TemplateStore {
URL url= FileLocator.find(plugin, Path.fromOSString(file), null);
if (url != null) {
ResourceBundle bundle= null;
- InputStream bundleStream= null;
- InputStream stream= null;
- try {
- String translations= element.getAttribute(TRANSLATIONS);
- if (translations != null) {
- URL bundleURL= FileLocator.find(plugin, Path.fromOSString(translations), null);
- if (bundleURL != null) {
- bundleStream= bundleURL.openStream();
+ String translations= element.getAttribute(TRANSLATIONS);
+ if (translations != null) {
+ URL bundleURL= FileLocator.find(plugin, Path.fromOSString(translations), null);
+ if (bundleURL != null) {
+ try (InputStream bundleStream= bundleURL.openStream()) {
bundle= new PropertyResourceBundle(bundleStream);
}
}
+ }
- stream= new BufferedInputStream(url.openStream());
+ try (InputStream stream= new BufferedInputStream(url.openStream())) {
TemplateReaderWriter reader= new TemplateReaderWriter();
TemplatePersistenceData[] datas= reader.read(stream, bundle);
for (int i= 0; i < datas.length; i++) {
@@ -158,18 +156,6 @@ public class ContributionTemplateStore extends TemplateStore {
templates.add(data);
}
}
- } finally {
- try {
- if (bundleStream != null)
- bundleStream.close();
- } catch (IOException x) {
- } finally {
- try {
- if (stream != null)
- stream.close();
- } catch (IOException x) {
- }
- }
}
}
}

Back to the top