Browse Source

don't hide original exception by throwing a new one from finally

master
Stefan Bodewig 8 years ago
parent
commit
6c8b3542fd
8 changed files with 64 additions and 22 deletions
  1. +6
    -2
      src/main/org/apache/tools/ant/input/DefaultInputHandler.java
  2. +9
    -3
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  3. +5
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java
  4. +8
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java
  5. +8
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java
  6. +7
    -1
      src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java
  7. +6
    -3
      src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java
  8. +15
    -8
      src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java

+ 6
- 2
src/main/org/apache/tools/ant/input/DefaultInputHandler.java View File

@@ -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();
}
}
}

+ 9
- 3
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java View File

@@ -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);
}
}
}



+ 5
- 1
src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java View File

@@ -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);


+ 8
- 2
src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java View File

@@ -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);
}
}
}
}


+ 8
- 2
src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java View File

@@ -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);
}
}
}
}


+ 7
- 1
src/main/org/apache/tools/ant/taskdefs/rmic/SunRmic.java View File

@@ -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
}
}
}
}


+ 6
- 3
src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java View File

@@ -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());
}
}
}
}


+ 15
- 8
src/main/org/apache/tools/ant/util/SymbolicLinkUtils.java View File

@@ -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);
}
}
}
}


Loading…
Cancel
Save