From 9034a33535480007be59cbcc70544e9cbd430361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 12 Jun 2023 14:07:12 +0200 Subject: [PATCH] Use try-with-resource Doesn't try to fix anything. Just code and warnings reduced. --- .../views/EngineDescriptorManager.java | 27 ++------------ .../cheatsheets/CheatSheetPlugin.java | 36 +++---------------- .../contentdetect/ContentDetectHelper.java | 29 ++------------- .../intro/impl/html/IntroHTMLGenerator.java | 24 +++---------- 4 files changed, 15 insertions(+), 101 deletions(-) diff --git a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java index fc588a91b..0c4ae884b 100644 --- a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java +++ b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/EngineDescriptorManager.java @@ -110,12 +110,8 @@ public EngineTypeDescriptor[] getEngineTypes() { public void save() { IPath stateLoc = HelpUIPlugin.getDefault().getStateLocation(); String fileName = stateLoc.append(USER_FILE).toOSString(); - FileOutputStream fos = null; - OutputStreamWriter osw = null; - try { - fos = new FileOutputStream(fileName); - osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8); - PrintWriter writer = new PrintWriter(osw); + try (PrintWriter writer = new PrintWriter( + new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8))) { writer.println(""); //$NON-NLS-1$ writer.println(""); //$NON-NLS-1$ for (int i = 0; i < descriptors.size(); i++) { @@ -126,26 +122,9 @@ public void save() { } writer.println(""); //$NON-NLS-1$ writer.flush(); - } - catch (IOException e) { + } catch (IOException e) { Platform.getLog(getClass()).error(Messages.EngineDescriptorManager_errorSaving, e); } - finally { - if (osw!=null) { - try { - osw.close(); - } - catch (IOException e) { - } - } - if (fos!=null) { - try { - fos.close(); - } - catch (IOException e) { - } - } - } } public void load() { diff --git a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatSheetPlugin.java b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatSheetPlugin.java index 22e5a5661..4614d2349 100644 --- a/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatSheetPlugin.java +++ b/org.eclipse.ui.cheatsheets/src/org/eclipse/ui/internal/cheatsheets/CheatSheetPlugin.java @@ -268,17 +268,10 @@ public void handleException(Throwable e) { */ public XMLMemento readMemento(String filename) { XMLMemento memento; - InputStreamReader reader = null; - - try { - // Read the cheatsheet state file. - final File stateFile = getCheatSheetStateFile(filename); - - FileInputStream input = new FileInputStream(stateFile); - reader = new InputStreamReader(input, StandardCharsets.UTF_8); + // Read the cheatsheet state file. + final File stateFile = getCheatSheetStateFile(filename); + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(stateFile), StandardCharsets.UTF_8)) { memento = XMLMemento.createReadRoot(reader); - - } catch (FileNotFoundException e) { memento = null; // Do nothing, the file will not exist the first time the workbench in used. @@ -286,15 +279,6 @@ public XMLMemento readMemento(String filename) { String message = Messages.ERROR_READING_STATE_FILE; CheatSheetPlugin.getPlugin().getLog().error(message, e); memento = null; - } finally { - try { - if (reader != null) - reader.close(); - } catch (IOException e) { - // Not much to do, just catch the exception and keep going. - String message = Messages.ERROR_READING_STATE_FILE; - CheatSheetPlugin.getPlugin().getLog().error(message, e); - } } return memento; } @@ -337,10 +321,8 @@ public void handleException(Throwable e) { public IStatus saveMemento(XMLMemento memento, String filename) { // Save the IMemento to a file. File stateFile = getCheatSheetStateFile(filename); - OutputStreamWriter writer = null; - try { - FileOutputStream stream = new FileOutputStream(stateFile); - writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); + try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(stateFile), + StandardCharsets.UTF_8)) { memento.save(writer); return Status.OK_STATUS; } catch (IOException e) { @@ -348,14 +330,6 @@ public IStatus saveMemento(XMLMemento memento, String filename) { String message = Messages.ERROR_WRITING_STATE_FILE; IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); return status; - } finally { - try { - if (writer != null) - writer.close(); - } catch (IOException e) { - String message = Messages.ERROR_WRITING_STATE_FILE; - CheatSheetPlugin.getPlugin().getLog().error(message, e); - } } } diff --git a/org.eclipse.ui.intro.universal/src/org/eclipse/ui/internal/intro/universal/contentdetect/ContentDetectHelper.java b/org.eclipse.ui.intro.universal/src/org/eclipse/ui/internal/intro/universal/contentdetect/ContentDetectHelper.java index 2adb2d873..e1fb0d5f2 100644 --- a/org.eclipse.ui.intro.universal/src/org/eclipse/ui/internal/intro/universal/contentdetect/ContentDetectHelper.java +++ b/org.eclipse.ui.intro.universal/src/org/eclipse/ui/internal/intro/universal/contentdetect/ContentDetectHelper.java @@ -105,29 +105,15 @@ public Set getContributors() { private XMLMemento getReadMemento(String filename) { XMLMemento memento; - InputStreamReader reader = null; - - try { - final File stateFile = getStateFile(filename); - - FileInputStream input = new FileInputStream(stateFile); - reader = new InputStreamReader(input, StandardCharsets.UTF_8); + final File stateFile = getStateFile(filename); + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(stateFile), StandardCharsets.UTF_8)){ memento = XMLMemento.createReadRoot(reader); - - } catch (FileNotFoundException e) { memento = null; // Do nothing, the file will not exist the first time the workbench in used. } catch (Exception e) { // TODO should we log an error? memento = null; - } finally { - try { - if (reader != null) - reader.close(); - } catch (IOException e) { - // TODO should we log an error? - } } return memento; } @@ -135,19 +121,10 @@ private XMLMemento getReadMemento(String filename) { private void saveMemento(XMLMemento memento, String filename) { // Save the IMemento to a file. File stateFile = getStateFile(filename); - OutputStreamWriter writer = null; - try { - FileOutputStream stream = new FileOutputStream(stateFile); - writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); + try (OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream(stateFile), StandardCharsets.UTF_8)){ memento.save(writer); } catch (IOException e) { stateFile.delete(); - } finally { - try { - if (writer != null) - writer.close(); - } catch (IOException e) { - } } } diff --git a/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/html/IntroHTMLGenerator.java b/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/html/IntroHTMLGenerator.java index e4bbef296..9f4669b74 100644 --- a/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/html/IntroHTMLGenerator.java +++ b/org.eclipse.ui.intro/src/org/eclipse/ui/internal/intro/impl/html/IntroHTMLGenerator.java @@ -15,12 +15,12 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Field; import java.net.URL; +import java.nio.charset.Charset; import java.util.Map; import org.eclipse.core.runtime.Platform; @@ -1143,19 +1143,13 @@ private boolean filteredFromPresentation(AbstractIntroElement element) { private StringBuilder readFromFile(String src, String charsetName) { if (src == null) return null; - InputStream stream = null; StringBuilder content = new StringBuilder(); - BufferedReader reader = null; - try { - URL url = new URL(src); - stream = url.openStream(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(src).openStream(), + (charsetName == null) ? Charset.defaultCharset().name() : charsetName))) { + // TODO: Do we need to worry about the encoding here? e.g.: // reader = new BufferedReader(new InputStreamReader(stream, // ResourcesPlugin.getEncoding())); - if (charsetName == null) - reader = new BufferedReader(new InputStreamReader(stream)); - else - reader = new BufferedReader(new InputStreamReader(stream, charsetName)); while (true) { int character = reader.read(); if (character == -1) // EOF @@ -1182,16 +1176,6 @@ else if (character == PluginIdParser.SUBSTITUTION_BEGIN) { // possible } } catch (Exception exception) { Log.error("Error reading from file", exception); //$NON-NLS-1$ - } finally { - try { - if (reader != null) - reader.close(); - if (stream != null) - stream.close(); - } catch (IOException e) { - Log.error("Error closing input stream", e); //$NON-NLS-1$ - return null; - } } return content; }