From 11c355f54f6d651aa9d4d79e64ca61dcebc6be1e Mon Sep 17 00:00:00 2001 From: "Jesse N. Glick" Date: Wed, 26 Aug 2009 20:19:20 +0000 Subject: [PATCH] Taking advantage of Throwable.cause. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@808169 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/BuildException.java | 65 ++----------------- .../org/apache/tools/ant/DefaultLogger.java | 2 +- .../tools/ant/taskdefs/AbstractCvsTask.java | 2 +- .../tools/ant/IntrospectionHelperTest.java | 34 +++++----- .../tools/ant/taskdefs/SQLExecTest.java | 4 +- 5 files changed, 25 insertions(+), 82 deletions(-) diff --git a/src/main/org/apache/tools/ant/BuildException.java b/src/main/org/apache/tools/ant/BuildException.java index ff700e7d3..1a47af847 100644 --- a/src/main/org/apache/tools/ant/BuildException.java +++ b/src/main/org/apache/tools/ant/BuildException.java @@ -17,9 +17,6 @@ */ package org.apache.tools.ant; -import java.io.PrintStream; -import java.io.PrintWriter; - /** * Signals an error condition during a build */ @@ -27,9 +24,6 @@ public class BuildException extends RuntimeException { private static final long serialVersionUID = -5419014565354664240L; - /** Exception that might have caused this one. */ - private Throwable cause; - /** Location in the build file where the exception occurred */ private Location location = Location.UNKNOWN_LOCATION; @@ -61,7 +55,7 @@ public class BuildException extends RuntimeException { */ public BuildException(String message, Throwable cause) { super(message); - this.cause = cause; + initCause(cause); } /** @@ -87,8 +81,7 @@ public class BuildException extends RuntimeException { * Should not be null. */ public BuildException(Throwable cause) { - super(cause.toString()); - this.cause = cause; + super(cause); } /** @@ -124,19 +117,10 @@ public class BuildException extends RuntimeException { * * @return the nested exception, or null if no * exception is associated with this one + * @deprecated Use {@link #getCause} instead. */ public Throwable getException() { - return cause; - } - - /** - * Returns the nested exception, if any. - * - * @return the nested exception, or null if no - * exception is associated with this one - */ - public Throwable getCause() { - return getException(); + return getCause(); } /** @@ -167,45 +151,4 @@ public class BuildException extends RuntimeException { return location; } - /** - * Prints the stack trace for this exception and any - * nested exception to System.err. - */ - public void printStackTrace() { - printStackTrace(System.err); - } - - /** - * Prints the stack trace of this exception and any nested - * exception to the specified PrintStream. - * - * @param ps The PrintStream to print the stack trace to. - * Must not be null. - */ - public void printStackTrace(PrintStream ps) { - synchronized (ps) { - super.printStackTrace(ps); - if (cause != null) { - ps.println("--- Nested Exception ---"); - cause.printStackTrace(ps); - } - } - } - - /** - * Prints the stack trace of this exception and any nested - * exception to the specified PrintWriter. - * - * @param pw The PrintWriter to print the stack trace to. - * Must not be null. - */ - public void printStackTrace(PrintWriter pw) { - synchronized (pw) { - super.printStackTrace(pw); - if (cause != null) { - pw.println("--- Nested Exception ---"); - cause.printStackTrace(pw); - } - } - } } diff --git a/src/main/org/apache/tools/ant/DefaultLogger.java b/src/main/org/apache/tools/ant/DefaultLogger.java index 16356c27f..132ac49f3 100644 --- a/src/main/org/apache/tools/ant/DefaultLogger.java +++ b/src/main/org/apache/tools/ant/DefaultLogger.java @@ -132,7 +132,7 @@ public class DefaultLogger implements BuildLogger { static void throwableMessage(StringBuffer m, Throwable error, boolean verbose) { while (error instanceof BuildException) { // #43398 - Throwable cause = ((BuildException) error).getCause(); + Throwable cause = error.getCause(); if (cause == null) { break; } diff --git a/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java b/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java index 766faf93f..518a03560 100644 --- a/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/AbstractCvsTask.java @@ -368,7 +368,7 @@ public abstract class AbstractCvsTask extends Task { if (failOnError) { throw(e); } - Throwable t = e.getException(); + Throwable t = e.getCause(); if (t == null) { t = e; } diff --git a/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java b/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java index 26a0156a7..d0003a931 100644 --- a/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java +++ b/src/tests/junit/org/apache/tools/ant/IntrospectionHelperTest.java @@ -67,7 +67,7 @@ public class IntrospectionHelperTest extends TestCase { ih.addText(p, this, "test2"); fail("test2 shouldn\'t be equal to test"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih = IntrospectionHelper.getHelper(String.class); @@ -166,14 +166,14 @@ public class IntrospectionHelperTest extends TestCase { ih.createElement(p, this, "fourteen"); fail("fourteen throws NullPointerException"); } catch (BuildException be) { - assertTrue(be.getException() instanceof NullPointerException); + assertTrue(be.getCause() instanceof NullPointerException); } try { ih.createElement(p, this, "fourteen"); fail("fifteen throws NullPointerException"); } catch (BuildException be) { - assertTrue(be.getException() instanceof NullPointerException); + assertTrue(be.getCause() instanceof NullPointerException); } } @@ -322,97 +322,97 @@ public class IntrospectionHelperTest extends TestCase { ih.setAttribute(p, this, "seven", "3"); fail("2 shouldn't be equals to three"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "eight", "2"); try { ih.setAttribute(p, this, "eight", "3"); fail("2 shouldn't be equals to three - as int"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "nine", "2"); try { ih.setAttribute(p, this, "nine", "3"); fail("2 shouldn't be equals to three - as Integer"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "ten", "2"); try { ih.setAttribute(p, this, "ten", "3"); fail(projectBasedir+"2 shouldn't be equals to "+projectBasedir+"3"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "eleven", "2"); try { ih.setAttribute(p, this, "eleven", "on"); fail("on shouldn't be false"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "twelve", "2"); try { ih.setAttribute(p, this, "twelve", "on"); fail("on shouldn't be false"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project"); try { ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.ProjectHelper"); fail("org.apache.tools.ant.Project shouldn't be equal to org.apache.tools.ant.ProjectHelper"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } try { ih.setAttribute(p, this, "thirteen", "org.apache.tools.ant.Project2"); fail("org.apache.tools.ant.Project2 doesn't exist"); } catch (BuildException be) { - assertTrue(be.getException() instanceof ClassNotFoundException); + assertTrue(be.getCause() instanceof ClassNotFoundException); } ih.setAttribute(p, this, "fourteen", "2"); try { ih.setAttribute(p, this, "fourteen", "on"); fail("2 shouldn't be equals to three - as StringBuffer"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "fifteen", "abcd"); try { ih.setAttribute(p, this, "fifteen", "on"); fail("o shouldn't be equal to a"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "sixteen", "abcd"); try { ih.setAttribute(p, this, "sixteen", "on"); fail("o shouldn't be equal to a"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "seventeen", "17"); try { ih.setAttribute(p, this, "seventeen", "3"); fail("17 shouldn't be equals to three"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "eightteen", "18"); try { ih.setAttribute(p, this, "eightteen", "3"); fail("18 shouldn't be equals to three"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } ih.setAttribute(p, this, "nineteen", "19"); try { ih.setAttribute(p, this, "nineteen", "3"); fail("19 shouldn't be equals to three"); } catch (BuildException be) { - assertTrue(be.getException() instanceof AssertionFailedError); + assertTrue(be.getCause() instanceof AssertionFailedError); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java index 96f18e4dc..4871b2d5f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java @@ -70,7 +70,7 @@ public class SQLExecTest extends TestCase { try { sql.execute(); } catch (BuildException e){ - assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1); + assertTrue(e.getCause().getMessage().indexOf("No suitable Driver") != -1); } assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); @@ -83,7 +83,7 @@ public class SQLExecTest extends TestCase { try { sql.execute(); } catch (BuildException e){ - assertTrue(e.getException().getMessage().indexOf("No suitable Driver") != -1); + assertTrue(e.getCause().getMessage().indexOf("No suitable Driver") != -1); } assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER));