From b5c4bdbec2f8a8c26b6466a588ba64d0f375152e Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Thu, 6 Nov 2003 14:57:10 +0000 Subject: [PATCH] Provide error stack for [*]ant[*] and macro call use same code for import and antlib git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275621 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/ProjectHelper.java | 28 ++++++++++++++++++- .../org/apache/tools/ant/taskdefs/Ant.java | 12 ++++++-- .../apache/tools/ant/taskdefs/Definer.java | 11 ++------ .../apache/tools/ant/taskdefs/ImportTask.java | 13 ++------- .../tools/ant/taskdefs/MacroInstance.java | 10 +++++-- 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 7c420e7ba..92765e5b8 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -531,5 +531,31 @@ public class ProjectHelper { } return componentName.substring(0, index); } -//end class + + /** + * Add location to build exception. + * @param ex the build exception, if the build exception + * does not include + * @param newLocation the location of the calling task (may be null) + * @return a new build exception based in the build exception with + * location set to newLocation. If the original exception + * did not have a location, just return the build exception + */ + public static BuildException addLocationToBuildException( + BuildException ex, Location newLocation) { + if (ex.getLocation() == null || ex.getMessage() == null) { + return ex; + } + String errorMessage + = "Following error occured while executing this line" + + System.getProperty("line.separator") + + ex.getLocation().toString() + + ex.getMessage(); + if (newLocation == null) { + return new BuildException(errorMessage); + } else { + return new BuildException( + errorMessage, newLocation); + } + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index a67b3ba0b..0876f8e0b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -378,7 +378,12 @@ public class Ant extends Task { } } - ProjectHelper.configureProject(newProject, new File(antFile)); + try { + ProjectHelper.configureProject(newProject, new File(antFile)); + } catch (BuildException ex) { + throw ProjectHelper.addLocationToBuildException( + ex, getLocation()); + } if (target == null) { target = newProject.getDefaultTarget(); @@ -413,7 +418,10 @@ public class Ant extends Task { try { log("Entering " + antFile + "...", Project.MSG_VERBOSE); newProject.executeTarget(target); - } finally { + } catch (BuildException ex) { + throw ProjectHelper.addLocationToBuildException( + ex, getLocation()); + } finally { log("Exiting " + antFile + ".", Project.MSG_VERBOSE); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Definer.java b/src/main/org/apache/tools/ant/taskdefs/Definer.java index 66d177b2d..ec19b207f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Definer.java +++ b/src/main/org/apache/tools/ant/taskdefs/Definer.java @@ -343,15 +343,8 @@ public abstract class Definer extends DefBase { antlib.setURI(getURI()); antlib.perform(); } catch (BuildException ex) { - Location exLocation = ex.getLocation(); - if (exLocation == null) { - throw ex; - } - throw new BuildException( - "Error executing antlib" - + System.getProperty("line.separator") - + exLocation.toString() - + " " + ex.getMessage()); + throw ProjectHelper.addLocationToBuildException( + ex, getLocation()); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java index 159160b99..f8a84921f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java @@ -55,7 +55,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Location; +import org.apache.tools.ant.ProjectHelper; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectHelper; import org.apache.tools.ant.Task; @@ -183,15 +183,8 @@ public class ImportTask extends Task { try { helper.parse(getProject(), importedFile); } catch (BuildException ex) { - Location exLocation = ex.getLocation(); - if (exLocation == null) { - throw ex; - } - throw new BuildException( - "Error executing import file" - + System.getProperty("line.separator") - + exLocation.toString() - + " " + ex.getMessage()); + throw ProjectHelper.addLocationToBuildException( + ex, getLocation()); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java b/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java index eeb9f7c06..3795afb59 100644 --- a/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java +++ b/src/main/org/apache/tools/ant/taskdefs/MacroInstance.java @@ -66,10 +66,11 @@ import java.util.Enumeration; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DynamicConfigurator; +import org.apache.tools.ant.ProjectHelper; +import org.apache.tools.ant.RuntimeConfigurable; import org.apache.tools.ant.Task; import org.apache.tools.ant.TaskContainer; import org.apache.tools.ant.UnknownElement; -import org.apache.tools.ant.RuntimeConfigurable; /** * The class to be placed in the ant type definition. @@ -264,6 +265,11 @@ public class MacroInstance extends Task implements DynamicConfigurator { // need to set the project on unknown element UnknownElement c = copy(macroDef.getNestedTask()); c.init(); - c.perform(); + try { + c.perform(); + } catch (BuildException ex) { + throw ProjectHelper.addLocationToBuildException( + ex, getLocation()); + } } }