From 46316a1b626fdfc280b0892c82d398cea465d397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Svensson?= Date: Tue, 30 Jun 2020 12:16:18 +0200 Subject: [PATCH] Throw an exception rather than return null on error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch to try-with-resources pattern Change-Id: I7bbb1a6faf0ba86e456f8a66776c3eda9b9144ed Signed-off-by: Torbjörn Svensson --- .../cdt/internal/qt/core/index/QMakeInfo.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/qt/org.eclipse.cdt.qt.core/src/org/eclipse/cdt/internal/qt/core/index/QMakeInfo.java b/qt/org.eclipse.cdt.qt.core/src/org/eclipse/cdt/internal/qt/core/index/QMakeInfo.java index 326ab0785dd..bc2330b4f4c 100644 --- a/qt/org.eclipse.cdt.qt.core/src/org/eclipse/cdt/internal/qt/core/index/QMakeInfo.java +++ b/qt/org.eclipse.cdt.qt.core/src/org/eclipse/cdt/internal/qt/core/index/QMakeInfo.java @@ -88,8 +88,11 @@ public final class QMakeInfo implements IQMakeInfo { } // run "qmake -query" - Map qmake1 = exec(PATTERN_QUERY_LINE, extraEnv, qmakePath, "-query"); //$NON-NLS-1$ - if (qmake1 == null) { + Map qmake1; + try { + qmake1 = exec(PATTERN_QUERY_LINE, extraEnv, qmakePath, "-query"); //$NON-NLS-1$ + } catch (IOException e) { + Activator.log(e); return INVALID; } @@ -98,9 +101,15 @@ public final class QMakeInfo implements IQMakeInfo { // TODO - no support for pre-3.0 // for QMake version 3.0 or newer, run "qmake -E file.pro" - Map qmake2 = version != null && version.getMajor() >= 3 - ? exec(PATTERN_EVAL_LINE, extraEnv, qmakePath, "-E", proPath) //$NON-NLS-1$ - : Collections.emptyMap(); + Map qmake2 = Collections.emptyMap(); + if (version != null && version.getMajor() >= 3) { + try { + qmake2 = exec(PATTERN_EVAL_LINE, extraEnv, qmakePath, "-E", proPath); //$NON-NLS-1$ + } catch (IOException e) { + Activator.log(e); + // Continue with an empty map. + } + } return new QMakeInfo(true, qmake1, qmake2); } @@ -181,13 +190,12 @@ public final class QMakeInfo implements IQMakeInfo { * @param extraEnv the extra environment for command * @param cmd the command line * @return the map of resolved key-value pairs + * @throws IOException If command execution failed */ - private static Map exec(Pattern regex, String[] extraEnv, String... command) { + private static Map exec(Pattern regex, String[] extraEnv, String... command) throws IOException { if (command.length < 1 || !new File(command[0]).exists()) { - Activator.log("qmake: cannot run command: " + (command.length > 0 ? command[0] : "")); //$NON-NLS-1$ //$NON-NLS-2$ - return null; + throw new IOException("qmake: cannot run command: " + (command.length > 0 ? command[0] : "")); //$NON-NLS-1$ //$NON-NLS-2$ } - BufferedReader reader = null; Process process = null; try { if (extraEnv != null && extraEnv.length > 0) { @@ -195,18 +203,10 @@ public final class QMakeInfo implements IQMakeInfo { } else { process = ProcessFactory.getFactory().exec(command); } - reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - return QMakeParser.parse(regex, reader); - } catch (IOException e) { - Activator.log(e); - return null; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + return QMakeParser.parse(regex, reader); + } } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - /* ignore */ - } if (process != null) { process.destroy(); }