summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Bodewig <bodewig@apache.org>2016-12-21 07:25:00 +0100
committerStefan Bodewig <bodewig@apache.org>2016-12-21 07:25:00 +0100
commit6c8b3542fdfd2a43e9ca9de9191b5df8bd846160 (patch)
treefb3584cecedf8d50e4cf2df1551a380138f8a350 /src
parent7ffc0970d55e6f9285e474cdb443b085bafad76c (diff)
downloadant-6c8b3542fdfd2a43e9ca9de9191b5df8bd846160.tar.gz
don't hide original exception by throwing a new one from finally
Diffstat (limited to 'src')
-rw-r--r--src/main/org/apache/tools/ant/input/DefaultInputHandler.java8
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java12
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java6
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java10
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java10
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java8
-rw-r--r--src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java9
-rw-r--r--src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java23
8 files changed, 64 insertions, 22 deletions
diff --git a/src/main/org/apache/tools/ant/input/DefaultInputHandler.java b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java
index 548ebffeb..164505f6d 100644
--- a/src/main/org/apache/tools/ant/input/DefaultInputHandler.java
+++ b/src/main/org/apache/tools/ant/input/DefaultInputHandler.java
@@ -48,6 +48,7 @@ public class DefaultInputHandler implements InputHandler {
public void handleInput(InputRequest request) throws BuildException {
String prompt = getPrompt(request);
BufferedReader r = null;
+ boolean success = false;
try {
r = new BufferedReader(new InputStreamReader(getInputStream()));
do {
@@ -61,12 +62,15 @@ public class DefaultInputHandler implements InputHandler {
+ " Console.", e);
}
} while (!request.isInputValid());
+ success = true;
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e) {
- throw new BuildException("Failed to close input.", e);
+ if (success) { // don't hide inner exception
+ throw new BuildException("Failed to close input.", e); //NOSONAR
+ }
}
}
}
@@ -117,4 +121,4 @@ public class DefaultInputHandler implements InputHandler {
protected InputStream getInputStream() {
return KeepAliveInputStream.wrapSystemIn();
}
-} \ No newline at end of file
+}
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
index 5c78115b3..a9e1662e4 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
@@ -1279,8 +1279,10 @@ public class JUnitTask extends Task {
checkForkedPath(cmd);
final TestResultHolder result = new TestResultHolder();
+ boolean success = false;
try {
result.exitCode = execute.execute();
+ success = true;
} catch (final IOException e) {
throw new BuildException("Process fork failed.", e, getLocation());
} finally {
@@ -1322,9 +1324,13 @@ public class JUnitTask extends Task {
}
if (!FILE_UTILS.tryHardToDelete(propsFile)) {
- throw new BuildException("Could not delete temporary "
- + "properties file '"
- + propsFile.getAbsolutePath() + "'.");
+ String msg = "Could not delete temporary properties file '"
+ + propsFile.getAbsolutePath() + "'.";
+ if (success) {
+ throw new BuildException(msg); //NOSONAR
+ } else { // don't hide inner exception
+ log(msg, Project.MSG_ERR);
+ }
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java
index 3386ee50e..07264b711 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java
@@ -117,6 +117,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT
* @throws BuildException if unable to write the output
*/
public void endTestSuite(JUnitTest suite) throws BuildException {
+ boolean success = false;
try {
StringBuffer sb = new StringBuffer("Tests run: ");
sb.append(suite.runCount());
@@ -158,12 +159,15 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT
throw new BuildException("Unable to write output", ioex);
}
}
+ success = true;
} finally {
if (out != null) {
try {
wri.close();
} catch (IOException ioex) {
- throw new BuildException("Unable to flush output", ioex);
+ if (success) {
+ throw new BuildException("Unable to flush output", ioex); //NOSONAR
+ }
} finally {
if (out != System.out && out != System.err) {
FileUtils.close(out);
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java
index 01cb4ba9a..61d6e3ddd 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java
@@ -354,6 +354,7 @@ public class RExecTask extends Task {
/** Create the telnet client object */
AntRExecClient rexec = null;
+ boolean success = false;
try {
rexec = new AntRExecClient();
try {
@@ -372,6 +373,7 @@ public class RExecTask extends Task {
/** Keep reading input stream until end of it or time-out */
rexec.waitForEOF(defaultTimeout);
+ success = true;
} catch (IOException e) {
throw new BuildException("Error r-executing command", e);
} finally {
@@ -379,8 +381,12 @@ public class RExecTask extends Task {
try {
rexec.disconnect();
} catch (IOException e) {
- throw new BuildException("Error disconnecting from "
- + server);
+ String msg = "Error disconnecting from " + server;
+ if (success) {
+ throw new BuildException(msg); //NOSONAR
+ } else { // don't hide inner exception
+ log(msg, Project.MSG_ERR);
+ }
}
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java
index 82625fa70..2bd220540 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java
@@ -98,6 +98,7 @@ public class TelnetTask extends Task {
/** Create the telnet client object */
AntTelnetClient telnet = null;
+ boolean success = false;
try {
telnet = new AntTelnetClient();
try {
@@ -118,13 +119,18 @@ public class TelnetTask extends Task {
}
task.execute(telnet);
}
+ success = true;
} finally {
if (telnet != null && telnet.isConnected()) {
try {
telnet.disconnect();
} catch (IOException e) {
- throw new BuildException("Error disconnecting from "
- + server);
+ String msg = "Error disconnecting from " + server;
+ if (success) {
+ throw new BuildException(msg); //NOSONAR
+ } else { // don't hide inner exception
+ log(msg, Project.MSG_ERR);
+ }
}
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java b/src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java
index 1b5e67dad..7281252aa 100644
--- a/src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java
+++ b/src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java
@@ -75,6 +75,7 @@ public class SunRmic extends DefaultRmicAdapter {
LogOutputStream logstr = new LogOutputStream(getRmic(),
Project.MSG_WARN);
+ boolean success = false;
try {
Class c = Class.forName(RMIC_CLASSNAME);
Constructor cons
@@ -86,6 +87,7 @@ public class SunRmic extends DefaultRmicAdapter {
Boolean ok =
(Boolean) doRmic.invoke(rmic,
(new Object[] {cmd.getArguments()}));
+ success = true;
return ok.booleanValue();
} catch (ClassNotFoundException ex) {
if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)) {
@@ -105,7 +107,11 @@ public class SunRmic extends DefaultRmicAdapter {
try {
logstr.close();
} catch (IOException e) {
- throw new BuildException(e);
+ // swallow if there was an error before so that
+ // original error will be passed up
+ if (success) {
+ throw new BuildException(e); //NOSONAR
+ }
}
}
}
diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java
index 334f37ec9..7140b97ff 100644
--- a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java
+++ b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java
@@ -189,6 +189,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector
throw new BuildException("Could not get InputStream from "
+ r.toLongString(), e);
}
+ boolean success = false;
try {
teststr = in.readLine();
@@ -202,7 +203,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector
}
teststr = in.readLine();
}
-
+ success = true;
return false;
} catch (IOException ioe) {
throw new BuildException("Could not read " + r.toLongString());
@@ -210,8 +211,10 @@ public class ContainsRegexpSelector extends BaseExtendSelector
try {
in.close();
} catch (Exception e) {
- throw new BuildException("Could not close "
- + r.toLongString());
+ if (success) {
+ throw new BuildException("Could not close " //NOSONAR
+ + r.toLongString());
+ }
}
}
}
diff --git a/src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java b/src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java
index 3bc9918ea..62b7a3f97 100644
--- a/src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java
+++ b/src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java
@@ -254,6 +254,7 @@ public class SymbolicLinkUtils {
}
boolean renamedTarget = false;
+ boolean success = false;
try {
try {
FILE_UTILS.rename(target, temp);
@@ -270,20 +271,26 @@ public class SymbolicLinkUtils {
+ " (was it a real file? is this "
+ "not a UNIX system?)");
}
+ success = true;
} finally {
if (renamedTarget) {
// return the resource to its original name:
try {
FILE_UTILS.rename(temp, target);
} catch (final IOException e) {
- throw new IOException("Couldn't return resource "
- + temp
- + " to its original name: "
- + target.getAbsolutePath()
- + ". Reason: " + e.getMessage()
- + "\n THE RESOURCE'S NAME ON DISK"
- + " HAS BEEN CHANGED BY THIS"
- + " ERROR!\n");
+ String msg = "Couldn't return resource "
+ + temp
+ + " to its original name: "
+ + target.getAbsolutePath()
+ + ". Reason: " + e.getMessage()
+ + "\n THE RESOURCE'S NAME ON DISK"
+ + " HAS BEEN CHANGED BY THIS"
+ + " ERROR!\n";
+ if (success) {
+ throw new IOException(msg); //NOSONAR
+ } else {
+ System.err.println(msg);
+ }
}
}
}