FileDocCategorySizeDatePackage
TaskAdapter.javaAPI DocApache Ant 1.706107Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant

TaskAdapter

public class TaskAdapter extends Task implements TypeAdapter
Uses introspection to "adapt" an arbitrary Bean which doesn't itself extend Task, but still contains an execute method and optionally a setProject method.

Fields Summary
private Object
proxy
Object to act as a proxy for.
Constructors Summary
Methods Summary
public voidcheckProxyClass(java.lang.Class proxyClass)
Check if the proxy class is a valid class to use with this adapter. The class must have a public no-arg "execute()" method.

param
proxyClass the class to check.

        checkTaskClass(proxyClass, getProject());
    
public static voidcheckTaskClass(java.lang.Class taskClass, Project project)
Checks whether or not a class is suitable to be adapted by TaskAdapter. If the class is of type Dispatchable, the check is not performed because the method that will be executed will be determined only at runtime of the actual task and not during parse time. This only checks conditions which are additionally required for tasks adapted by TaskAdapter. Thus, this method should be called by Project.checkTaskClass. Throws a BuildException and logs as Project.MSG_ERR for conditions that will cause the task execution to fail. Logs other suspicious conditions with Project.MSG_WARN.

param
taskClass Class to test for suitability. Must not be null.
param
project Project to log warnings/errors to. Must not be null.
see
Project#checkTaskClass(Class)

        if (!Dispatchable.class.isAssignableFrom(taskClass)) {
            // don't have to check for interface, since then
            // taskClass would be abstract too.
            try {
                final Method executeM = taskClass.getMethod("execute", (Class[]) null);
                // don't have to check for public, since
                // getMethod finds public method only.
                // don't have to check for abstract, since then
                // taskClass would be abstract too.
                if (!Void.TYPE.equals(executeM.getReturnType())) {
                    final String message = "return type of execute() should be "
                        + "void but was \"" + executeM.getReturnType() + "\" in "
                        + taskClass;
                    project.log(message, Project.MSG_WARN);
                }
            } catch (NoSuchMethodException e) {
                final String message = "No public execute() in " + taskClass;
                project.log(message, Project.MSG_ERR);
                throw new BuildException(message);
            } catch (LinkageError e) {
                String message = "Could not load " + taskClass + ": " + e;
                project.log(message, Project.MSG_ERR);
                throw new BuildException(message, e);
            }
        }
    
public voidexecute()
Executes the proxied task.

exception
BuildException if the project could not be set or the method could not be executed.

        try {
            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()});
            }
        } catch (NoSuchMethodException e) {
            // ignore this if the class being used as a task does not have
            // a set project method.
        } catch (Exception ex) {
            log("Error setting project in " + proxy.getClass(),
                Project.MSG_ERR);
            throw new BuildException(ex);
        }

        try {
            DispatchUtils.execute(proxy);
        } catch (BuildException be) {
            throw be;
        } catch (Exception ex) {
            log("Error in " + proxy.getClass(), Project.MSG_VERBOSE);
            throw new BuildException(ex);
        }
    
public java.lang.ObjectgetProxy()
Returns the target object being proxied.

return
the target proxy object.

        return proxy;
    
public voidsetProxy(java.lang.Object o)
Sets the target object to proxy for.

param
o The target object. Must not be null.

        this.proxy = o;