From 74434ce5d074e3da0dd29528e6ea7c62be9e20e8 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 11 Sep 2006 18:53:44 +0000 Subject: [PATCH] Push setLocation down to ProjectComponent, reflect it into adapted tasks git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@442287 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++ src/etc/testcases/core/location.xml | 38 +++++++++++++ .../apache/tools/ant/ProjectComponent.java | 34 ++++++++++++ src/main/org/apache/tools/ant/Task.java | 34 ------------ .../org/apache/tools/ant/TaskAdapter.java | 18 ++++++- .../org/apache/tools/ant/UnknownElement.java | 7 ++- .../org/apache/tools/ant/LocationTest.java | 54 +++++++++++++++++++ 7 files changed, 152 insertions(+), 37 deletions(-) create mode 100644 src/etc/testcases/core/location.xml create mode 100644 src/testcases/org/apache/tools/ant/LocationTest.java diff --git a/WHATSNEW b/WHATSNEW index 4426f7790..1e21c4097 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -38,6 +38,10 @@ Other changes: Bugzilla report 36772. * added searchparents attribute to . Bugzilla report 39549. +* tasks that don't extend Ant's Task class can will now get the build file + location reflected into a method of the signature void setLocation(Location) + - if such a method exists. + Changes from Ant 1.6.5 to Ant 1.7.0Beta1 ======================================== diff --git a/src/etc/testcases/core/location.xml b/src/etc/testcases/core/location.xml new file mode 100644 index 000000000..80ae9d7ba --- /dev/null +++ b/src/etc/testcases/core/location.xml @@ -0,0 +1,38 @@ + + + + + Only use this build file from within tests + + + + Hello + + + + Hello + + + + + + + + + + \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/ProjectComponent.java b/src/main/org/apache/tools/ant/ProjectComponent.java index 4ac90322b..5bcdef5c4 100644 --- a/src/main/org/apache/tools/ant/ProjectComponent.java +++ b/src/main/org/apache/tools/ant/ProjectComponent.java @@ -34,6 +34,14 @@ public abstract class ProjectComponent { */ protected Project project; + /** + * Location within the build file of this task definition. + * @deprecated since 1.6.x. + * You should not be accessing this variable directly. + * Please use the {@link #getLocation()} method. + */ + protected Location location = Location.UNKNOWN_LOCATION; + /** Sole constructor. */ public ProjectComponent() { } @@ -60,6 +68,32 @@ public abstract class ProjectComponent { return project; } + /** + * Returns the file/location where this task was defined. + * + * @return the file/location where this task was defined. + * Should not return null. Location.UNKNOWN_LOCATION + * is used for unknown locations. + * + * @see Location#UNKNOWN_LOCATION + */ + public Location getLocation() { + return location; + } + + /** + * Sets the file/location where this task was defined. + * + * @param location The file/location where this task was defined. + * Should not be null--use + * Location.UNKNOWN_LOCATION if the location isn't known. + * + * @see Location#UNKNOWN_LOCATION + */ + public void setLocation(Location location) { + this.location = location; + } + /** * Logs a message with the default (INFO) priority. * diff --git a/src/main/org/apache/tools/ant/Task.java b/src/main/org/apache/tools/ant/Task.java index 11dacd14e..6d5514ba5 100644 --- a/src/main/org/apache/tools/ant/Task.java +++ b/src/main/org/apache/tools/ant/Task.java @@ -47,14 +47,6 @@ public abstract class Task extends ProjectComponent { */ protected String description; - /** - * Location within the build file of this task definition. - * @deprecated since 1.6.x. - * You should not be accessing this variable directly. - * Please use the {@link #getLocation()} method. - */ - protected Location location = Location.UNKNOWN_LOCATION; - /** * Name of this task to be used for logging purposes. * This defaults to the same as the type, but may be @@ -190,32 +182,6 @@ public abstract class Task extends ProjectComponent { public void execute() throws BuildException { } - /** - * Returns the file/location where this task was defined. - * - * @return the file/location where this task was defined. - * Should not return null. Location.UNKNOWN_LOCATION - * is used for unknown locations. - * - * @see Location#UNKNOWN_LOCATION - */ - public Location getLocation() { - return location; - } - - /** - * Sets the file/location where this task was defined. - * - * @param location The file/location where this task was defined. - * Should not be null--use - * Location.UNKNOWN_LOCATION if the location isn't known. - * - * @see Location#UNKNOWN_LOCATION - */ - public void setLocation(Location location) { - this.location = location; - } - /** * Returns the wrapper used for runtime configuration. * diff --git a/src/main/org/apache/tools/ant/TaskAdapter.java b/src/main/org/apache/tools/ant/TaskAdapter.java index e57c480ce..db1c53882 100644 --- a/src/main/org/apache/tools/ant/TaskAdapter.java +++ b/src/main/org/apache/tools/ant/TaskAdapter.java @@ -100,9 +100,23 @@ public class TaskAdapter extends Task implements TypeAdapter { * or the method could not be executed. */ public void execute() throws BuildException { - Method setProjectM = null; try { - setProjectM = proxy.getClass().getMethod( + Method setLocationM = proxy.getClass().getMethod( + "setLocation", new Class[] {Location.class}); + if (setLocationM != null) { + setLocationM.invoke(proxy, new Object[] {getLocation()}); + } + } catch (NoSuchMethodException e) { + // ignore this if the class being used as a task does not have + // a set location method. + } catch (Exception ex) { + log("Error setting location in " + proxy.getClass(), + Project.MSG_ERR); + throw new BuildException(ex); + } + + try { + Method setProjectM = proxy.getClass().getMethod( "setProject", new Class[] {Project.class}); if (setProjectM != null) { setProjectM.invoke(proxy, new Object[] {getProject()}); diff --git a/src/main/org/apache/tools/ant/UnknownElement.java b/src/main/org/apache/tools/ant/UnknownElement.java index cf1316e2e..b34d28575 100644 --- a/src/main/org/apache/tools/ant/UnknownElement.java +++ b/src/main/org/apache/tools/ant/UnknownElement.java @@ -424,6 +424,9 @@ public class UnknownElement extends Task { if (o instanceof Task) { ((Task) o).setOwningTarget(getOwningTarget()); } + if (o instanceof ProjectComponent) { + ((ProjectComponent) o).setLocation(getLocation()); + } return o; } @@ -545,7 +548,9 @@ public class UnknownElement extends Task { childTask.setRuntimeConfigurableWrapper(childWrapper); childTask.setTaskName(childName); childTask.setTaskType(childName); - childTask.setLocation(child.getLocation()); + } + if (realChild instanceof ProjectComponent) { + ((ProjectComponent) realChild).setLocation(child.getLocation()); } child.handleChildren(realChild, childWrapper); return true; diff --git a/src/testcases/org/apache/tools/ant/LocationTest.java b/src/testcases/org/apache/tools/ant/LocationTest.java new file mode 100644 index 000000000..808c6f4f2 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/LocationTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant; + +import org.apache.tools.ant.taskdefs.ConditionTask; +import org.apache.tools.ant.taskdefs.Echo; +import org.apache.tools.ant.types.FileSet; + +public class LocationTest extends BuildFileTest { + + public void setUp() { + configureProject("src/etc/testcases/core/location.xml"); + } + + public void testPlainTask() { + executeTarget("testPlainTask"); + Echo e = (Echo) getProject().getReference("echo"); + assertFalse(e.getLocation() == Location.UNKNOWN_LOCATION); + assertFalse(e.getLocation().getLineNumber() == 0); + } + + public void testStandaloneType() { + executeTarget("testStandaloneType"); + Echo e = (Echo) getProject().getReference("echo2"); + FileSet f = (FileSet) getProject().getReference("fs"); + assertFalse(f.getLocation() == Location.UNKNOWN_LOCATION); + assertEquals(e.getLocation().getLineNumber() + 1, + f.getLocation().getLineNumber()); + } + + public void testConditionTask() { + executeTarget("testConditionTask"); + TaskAdapter ta = (TaskAdapter) getProject().getReference("cond"); + ConditionTask c = (ConditionTask) ta.getProxy(); + assertFalse(c.getLocation() == Location.UNKNOWN_LOCATION); + assertFalse(c.getLocation().getLineNumber() == 0); + } +} \ No newline at end of file