From b6ec4af9dd33b9a37fe18f1c2a043e67bdf0d67b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 11 Jul 2000 11:14:48 +0000 Subject: [PATCH] Rewritten the introspection part of ProjectHelper. 1. Moved all introspection activities to a helper class. 2. setter methods can now use parameters of a far richer set of types. See the modified patch task as an example. 3. Nested Elements can be created by the task with createElement() or by IntrospectionHelper if the task implements void addElement(ObjectType). 4. Documented addText(String) and nested elements in the "Writing Your own Task" section and changed the documentation for the setter methods. 5. Added getLocation method to BuildException. 6. Changed the return type of Ant.createProperty from Task to Property - as this is what gets returned anyway. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267753 13f79535-47bb-0310-9956-ffa450edef68 --- docs/index.html | 19 +- .../org/apache/tools/ant/BuildException.java | 7 + .../apache/tools/ant/IntrospectionHelper.java | 482 ++++++++++++++++++ .../org/apache/tools/ant/ProjectHelper.java | 133 ++--- .../org/apache/tools/ant/taskdefs/Ant.java | 2 +- .../org/apache/tools/ant/taskdefs/Patch.java | 43 +- 6 files changed, 588 insertions(+), 98 deletions(-) create mode 100644 src/main/org/apache/tools/ant/IntrospectionHelper.java diff --git a/docs/index.html b/docs/index.html index a2381c9de..d71c07c77 100644 --- a/docs/index.html +++ b/docs/index.html @@ -3143,11 +3143,24 @@ output.

It is very easy to write your own task:

  1. Create a Java class that extends org.apache.tools.ant.Task.
  2. -
  3. For each attribute, write a setter method. The setter method must be a public - void method that takes one String as an argument. The +
  4. For each attribute, write a setter method. The setter method must be a + public void method that takes a single argument. The name of the method must begin with "set", followed by the attribute name, with the first character in uppercase, and the rest in - lowercase.
  5. + lowercase. The type of the attribute can be String, any + primitive type, Class, File (in which case the + value of the attribute is interpreted relative to the project's basedir) + or any other type that has a constructor with a single String + argument +
  6. If the task should support character data, write a public void + addText(String) method.
  7. +
  8. For each nested element, write a create or add method. A create method + must be a public method that takes no arguments and returns + an Object type. The name of the create method must begin with + "create", followed by the element name. An add method must be + a public void method that takes a single argument of an + Object type with a no argument constructor. The name of the add method + must begin with "add", followed by the element name.
  9. Write a public void execute method, with no arguments, that throws a BuildException. This method implements the task itself.
  10. diff --git a/src/main/org/apache/tools/ant/BuildException.java b/src/main/org/apache/tools/ant/BuildException.java index ee74c0d0b..b0e6c185a 100644 --- a/src/main/org/apache/tools/ant/BuildException.java +++ b/src/main/org/apache/tools/ant/BuildException.java @@ -152,4 +152,11 @@ public class BuildException extends RuntimeException { public void setLocation(Location location) { this.location = location; } + + /** + * Returns the file location where the error occured. + */ + public Location getLocation() { + return location; + } } diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java new file mode 100644 index 000000000..dcb44feda --- /dev/null +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -0,0 +1,482 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000 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", "Tomcat", 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; + +import java.lang.reflect.*; +import java.io.File; +import java.util.*; + +/** + * Helper class that collects the methods a task or nested element + * holds to set attributes, create nested elements or hold PCDATA + * elements. + * + * @author Stefan Bodewig stefan.bodewig@megabit.net + */ +public class IntrospectionHelper { + + /** + * holds the types of the attributes that could be set. + */ + private Hashtable attributeTypes; + + /** + * holds the attribute setter methods. + */ + private Hashtable attributeSetters; + + /** + * Holds the types of nested elements that could be created. + */ + private Hashtable nestedTypes; + + /** + * Holds methods to create nested elements. + */ + private Hashtable nestedCreators; + + /** + * The method to add PCDATA stuff. + */ + private Method addText = null; + + /** + * The Class that's been introspected. + */ + private Class bean; + + /** + * instances we've already created + */ + private static Hashtable helpers = new Hashtable(); + + private IntrospectionHelper(final Class bean) { + attributeTypes = new Hashtable(); + attributeSetters = new Hashtable(); + nestedTypes = new Hashtable(); + nestedCreators = new Hashtable(); + this.bean = bean; + + Method[] methods = bean.getMethods(); + for (int i=0; ipatch's -p option. */ - public void setStrip(String num) { - strip = Integer.parseInt(num); + public void setStrip(int num) throws BuildException { + if (strip < 0) { + throw new BuildException("strip has to be >= 0", location); + } + strip = num; } /** * Work silently unless an error occurs. */ - public void setQuiet(String q) { - quiet = Project.toBoolean(q); + public void setQuiet(boolean q) { + quiet = q; } /** * Assume patch was created with old and new files swapped. */ - public void setReverse(String r) { - reverse = Project.toBoolean(r); + public void setReverse(boolean r) { + reverse = r; + } + + public final void setCommand(String command) throws BuildException { + throw new BuildException("Cannot set attribute command in patch task", + location); } public void execute() throws BuildException { if (patchFile == null) { - throw new BuildException("patchfile argument is required"); + throw new BuildException("patchfile argument is required", + location); } + if (!patchFile.exists()) { + throw new BuildException("patchfile "+patchFile+" doesn\'t exist", + location); + } StringBuffer command = new StringBuffer("patch -i "+patchFile+" ");