ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ludovic Claude ...@websitewatchers.com>
Subject Re: Ant Principles
Date Fri, 21 Apr 2000 11:09:05 GMT
Thomas Haas wrote:

>
> > From: Kuiper, Arnout <Arnout.Kuiper@nl.origin-it.com>
> >
> > Nested objects
> > ==============
> >
> >   Suppose the type is Foo.
> >   1. Check if Foo(String) exists. If so, use it, and we're done.
> >   2. Check if Foo(double) exists. If so, cast String to double,
> >      use it, and we're done.
> >   3. iterate step 2 for each primitive type.
> >   4. Failure...
>

I wrote some time ago in my experimental version of ant something similar. Here
goes the code.
If you like it, tell me, i will integrate it in the current version of ant
(unless we do further developments
in the utah branch). Little benefit of this aditional class: you can cache it
and save time because you don't
have to run reflexion more than one time on a class.

And Sam, i like your <style> proposal, there's nothing like a good example to
clear ideas.

My £0.02
Ludovic Claude.


public class TaskHelper {

    private Class taskClass;
    private Hashtable propertySetters = new Hashtable();

    public TaskHelper(Class taskClass) {
      super();
      this.taskClass = taskClass;
      init();
    }

    public void init() {
        BeanInfo beanInfo;
        try {
            beanInfo = Introspector.getBeanInfo(taskClass);
        } catch (IntrospectionException ie) {
            String msg = "Can't introspect class: " + taskClass.getName();
            throw new BuildException(msg);
        }

        PropertyDescriptor[] pda = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < pda.length; i++) {
            PropertyDescriptor pd = pda[i];
            String property = pd.getName();
            Method setMethod = pd.getWriteMethod();
            if (setMethod != null) {

                // make sure that there's only 1 param and that it
                // takes a supported parameter (String, boolean, int, float),
                // all other setMethods need to get screened out

                Class[] ma = setMethod.getParameterTypes();
                if (ma.length == 1) {
                    Class c = ma[0];
                    if (c.getName().equals("java.lang.String")
                          || c.getName().equals("boolean")
                          || c.getName().equals("int")
                          || c.getName().equals("float") ) {
                        propertySetters.put(property, setMethod);
                    }
                }
            }
        }
    }

    public Class getTaskClass() {
        return taskClass;
    }

    /**
     * Return true if the property is an attribute of the task.
     */
    public boolean isTaskAttribute(String property) {
        return (null != propertySetters.get(property));
    }

    public String getPropertyType(String property) {
        Method setMethod = getSetterMethod(property);
        String paramType = setMethod.getParameterTypes()[0].getName();
        return paramType;
    }

    public Task createTask(Project project) throws BuildException {

        try {
            Object o = taskClass.newInstance();
            Task task = null;
            if( o instanceof Task ) {
               task=(Task)o;
            } else {
                // "Generic" Bean - use the setter pattern
                // and an Adapter
                TaskAdapter taskA=new TaskAdapter();
                taskA.setProxy( o );
                task=taskA;
            }
            task.setProject(project);
            String msg = "   +Task: " + taskClass.getName();
            project.log (msg, Project.MSG_VERBOSE);
            return task;
        } catch (Exception e) {
            String msg = "Could not create task of type: "
                 + taskClass.getName() + " due to " + e;
            throw new BuildException(msg);
        }
    }

    public void configure(Object target, String property, String value) {
        configure(target, property, new Object[] {value});
    }

    public void configure(Object target, String property, boolean value) {
        configure(target, property, new Object[] {new Boolean(value)});
    }

    public void configure(Object target, String property, int value) {
        configure(target, property, new Object[] {new Integer(value)});
    }

    public void configure(Object target, String property, float value) {
        configure(target, property, new Object[] {new Float(value)});
    }

    private void configure(Object target, String property, Object[] paramValue)
throws BuildException {

        if ( target instanceof TaskAdapter )
            target=((TaskAdapter)target).getProxy();

        // reflect the property into the target
        Method setMethod = getSetterMethod(property);
        try {
            setMethod.invoke(target, paramValue);

        } catch (IllegalAccessException iae) {
            String msg = "Error setting value for property: " +
                property;
            iae.printStackTrace();
            throw new BuildException(msg);
        } catch (InvocationTargetException ie) {
            String msg = "Error setting value for property: " +
                property + " in " + target.getClass().getName();
            ie.printStackTrace();
            ie.getTargetException().printStackTrace();
            throw new BuildException(msg);
        }
    }

    private Method getSetterMethod(String property) throws BuildException {
        Method setMethod = (Method)propertySetters.get(property);
        if (setMethod == null) {
            String msg = "Configuration property \"" + property +
                "\" does not have a setMethod in " + taskClass.getName();
            throw new BuildException(msg);
        }
        return setMethod;
    }
}





Mime
View raw message