ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fran├žois Wolff <e...@noos.fr>
Subject Proxy support in Ant
Date Thu, 02 May 2002 16:56:36 GMT
  Hello,

I'm the developer of Ejen <http://ejen.sourceforge.net>, a Java/XSLT 
based code generation system. Next release of this system implements an 
Ant task that uses 
java.lang.reflect.Proxy/java.lang.reflect.InvocationHandler classes for 
sub-node creation.

For example, there is this kind of declaration:

public class Ejen extends Task (...) {
    ...
    public Source createSource() {
        return (Source)(Proxy.newProxyInstance(Source.class.getClassLoader(),
			                       new Class[] { Source.class },
			                       new NodeInvocationHandler(...)));
    }
    ...
}

with,

public interface Source {
    public void setUri(String uri);
}

NodeInvocationHandler implementation doesn't matter here...

With this kind of build file:

...
<taskdef name="ejen" classname="org.ejen.ant.Ejen"/>

<target name="ejen-target">
  <ejen>
    <source badattr="..."/>
    ...
  </ejen>
</target>
...

I get the following error:

...: Class $Proxy0 doesn't support the "badattr" attribute.

I would prefer of course:

...: Class Source doesn't support the "badattr" attribute.

I wrote a patch for the org.apache.tools.ant.IntrospectionHelper class 
(getElementName method) with this code (for Ant 1.4.1 release):

...
// New --------------------------------------------------------
Class eltClass = element.getClass();
if (java.lang.reflect.Proxy.isProxyClass(eltClass))
{
    Class[] interfaces = eltClass.getInterfaces();
    if (interfaces.length == 1)
        return "Class " + interfaces[0].getName();
    else if (interfaces.length > 1)
    {
        StringBuffer sb = new StringBuffer("Classes ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                sb.append(", ");
            sb.append(interfaces[i].getName());
        }
        return sb.toString();
    }
}
return "Class " + eltClass.getName();
// -------------------------------------------------------- New
	
/* Old --------------------------------------------------------
return "Class " + element.getClass().getName();
-------------------------------------------------------- Old */
...

Error message is then correct.

The patch is included as an attachment. I have written also a patch for 
the next 1.5 release of Ant (based on the org.apache.tools.ant.Project 
class, revision 1.108). I think it could be very interesting to add 
Proxy support in the next release of Ant, because use of Proxys can be 
very useful when adapting existing API to specific Ant constraints 
(create..., set...).

Thanks a lot for the excellent Ant,
F. Wolff.


Mime
View raw message