From 40a387aa615457a153ca11abcf9ce59e214b95df Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Sat, 4 Aug 2001 14:30:36 +0000 Subject: [PATCH] Give data types some of the capabailities of Tasks, namely access to the project object and the ability to log messages through that project. The IntrospectionHelper cooperates to ensure DataTypes created as nested elements are correctly setup git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269458 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/IntrospectionHelper.java | 12 +- .../apache/tools/ant/ProjectComponent.java | 110 ++++++++++++++++++ src/main/org/apache/tools/ant/Task.java | 44 +------ .../org/apache/tools/ant/types/DataType.java | 23 +++- 4 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 src/main/org/apache/tools/ant/ProjectComponent.java diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index bd437e794..9a93c5fb6 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -322,7 +322,11 @@ public class IntrospectionHelper implements BuildListener { throw new BuildException(msg); } try { - return nc.create(element); + Object nestedElement = nc.create(element); + if (nestedElement instanceof ProjectComponent) { + ((ProjectComponent)nestedElement).setProject(project); + } + return nestedElement; } catch (IllegalAccessException ie) { // impossible as getMethods should only return public methods throw new BuildException(ie); @@ -565,7 +569,11 @@ public class IntrospectionHelper implements BuildListener { String value) throws InvocationTargetException, IllegalAccessException, BuildException { try { - m.invoke(parent, new Object[] {c.newInstance(new String[] {value})}); + Object attribute = c.newInstance(new String[] {value}); + if (attribute instanceof ProjectComponent) { + ((ProjectComponent)attribute).setProject(p); + } + m.invoke(parent, new Object[] {attribute}); } catch (InstantiationException ie) { throw new BuildException(ie); } diff --git a/src/main/org/apache/tools/ant/ProjectComponent.java b/src/main/org/apache/tools/ant/ProjectComponent.java new file mode 100644 index 000000000..8a3fd6e85 --- /dev/null +++ b/src/main/org/apache/tools/ant/ProjectComponent.java @@ -0,0 +1,110 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant; + +/** + * Base class for components of a project, including tasks and data types. Provides + * common facilities. + * + * @author Conor MacNeill + */ + +public abstract class ProjectComponent { + + protected Project project = null; + + /** + * Sets the project object of this component. This method is used by + * project when a component is added to it so that the component has + * access to the functions of the project. It should not be used + * for any other purpose. + * + * @param project Project in whose scope this component belongs. + */ + public void setProject(Project project) { + this.project = project; + } + + /** + * Get the Project to which this component belongs + * + * @return the components's project. + */ + public Project getProject() { + return project; + } + + /** + * Log a message with the default (INFO) priority. + * + * @param the message to be logged. + */ + public void log(String msg) { + log(msg, Project.MSG_INFO); + } + + /** + * Log a mesage with the give priority. + * + * @param the message to be logged. + * @param msgLevel the message priority at which this message is to be logged. + */ + public void log(String msg, int msgLevel) { + if (project != null) { + project.log(msg, msgLevel); + } + } +} + diff --git a/src/main/org/apache/tools/ant/Task.java b/src/main/org/apache/tools/ant/Task.java index 4ef176a00..cdccdb070 100644 --- a/src/main/org/apache/tools/ant/Task.java +++ b/src/main/org/apache/tools/ant/Task.java @@ -60,9 +60,8 @@ package org.apache.tools.ant; *

Use {@link Project#createTask Project.createTask} to create a new Task. */ -public abstract class Task { +public abstract class Task extends ProjectComponent { - protected Project project = null; protected Target target = null; protected String description=null; protected Location location = Location.UNKNOWN_LOCATION; @@ -70,27 +69,6 @@ public abstract class Task { protected String taskType = null; protected RuntimeConfigurable wrapper; - /** - * Sets the project object of this task. This method is used by - * project when a task is added to it so that the task has - * access to the functions of the project. It should not be used - * for any other purpose. - * - * @param project Project in whose scope this task belongs. - */ - public void setProject(Project project) { - this.project = project; - } - - /** - * Get the Project to which this task belongs - * - * @return the task's project. - */ - public Project getProject() { - return project; - } - /** * Sets the target object of this task. * @@ -136,26 +114,6 @@ public abstract class Task { this.taskType = type; } - /** - * Log a message with the default (INFO) priority. - * - * @param the message to be logged. - */ - public void log(String msg) { - log(msg, Project.MSG_INFO); - } - - /** - * Log a mesage with the give priority. - * - * @param the message to be logged. - * @param msgLevel the message priority at which this message is to be logged. - */ - public void log(String msg, int msgLevel) { - project.log(this, msg, msgLevel); - } - - /** Sets a description of the current action. It will be usefull in commenting * what we are doing. */ diff --git a/src/main/org/apache/tools/ant/types/DataType.java b/src/main/org/apache/tools/ant/types/DataType.java index a9f83dfeb..f23763807 100644 --- a/src/main/org/apache/tools/ant/types/DataType.java +++ b/src/main/org/apache/tools/ant/types/DataType.java @@ -58,6 +58,7 @@ package org.apache.tools.ant.types; import java.util.Stack; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectComponent; /** * Base class for those classes that can appear inside the build file @@ -71,7 +72,7 @@ import org.apache.tools.ant.Project; * * @author Stefan Bodewig */ -public abstract class DataType { +public abstract class DataType extends ProjectComponent { /** * The descriptin the user has set. */ @@ -161,6 +162,26 @@ public abstract class DataType { checked = true; } + /** + * Performs the check for circular references and returns the + * referenced object. + */ + protected Object getCheckedRef(Class requiredClass, String dataTypeName) { + if (!checked) { + Stack stk = new Stack(); + stk.push(this); + dieOnCircularReference(stk, getProject()); + } + + Object o = ref.getReferencedObject(getProject()); + if (!(requiredClass.isAssignableFrom(o.getClass()))) { + String msg = ref.getRefId()+" doesn\'t denote a " + dataTypeName; + throw new BuildException(msg); + } else { + return o; + } + } + /** * Creates an exception that indicates that refid has to be the * only attribute if it is set.