You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by François Wolff <ej...@noos.fr> on 2002/05/02 18:56:36 UTC

Proxy support in Ant

  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.


Re: Proxy support in Ant

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Have a look at the new DynamicConfigurator interface in Ant 1.5's API.  It
will likely be the answer the problem you're trying to solve - if your goal
is to support dynamic elements and attributes, that is.

    Erik


----- Original Message -----
From: "François Wolff" <ej...@noos.fr>
To: <an...@jakarta.apache.org>
Sent: Thursday, May 02, 2002 12:56 PM
Subject: Proxy support in Ant


>   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.
>
>


----------------------------------------------------------------------------
----


> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Proxy support in Ant

Posted by François Wolff <ej...@noos.fr>.
Stefan Bodewig wrote:

>On Thu, 02 May 2002, François Wolff <ej...@noos.fr> wrote:
>
>>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).
>>
>
>This is the one I had a look at.  Ant 1.x is supposed to be JDK 1.1
>compatible (and even Ant2 will settle on 1.2, not 1.3).  Do you see
>any chance to rewrite your changes so that they use refelection and
>compile/run using JDK 1.1?
>
Thanks for your reply !

I have made a new patch for the Project class (still 1.108 CVS revision) 
that should be JDK 1.1 compatible (only tested with JDK 1.4 and Ant 
1.4.1 in the IntrospectionHelper class, because I don't have the entire 
CVS tree...).

The added code is:

    private static java.lang.reflect.Method isProxyClass;
    static {
        try {
            Class proxyClass = Class.forName("java.lang.reflect.Proxy");
            isProxyClass = proxyClass.getMethod("isProxyClass",
                                                new Class[]{ Class.class });
        } catch (Throwable t) {
            isProxyClass = null;
        }
    }

and, in getElementName(...) method,

        if (isProxyClass != null) {

            boolean isProxyInstance = false;

            try {
                Object o
                    = isProxyClass.invoke(null, new Object[]{ elementClass });
                isProxyInstance = ((Boolean)o).booleanValue();
            } catch (Throwable t) {
                isProxyInstance = false;
            }

            if (isProxyInstance) {
                Class[] interfaces = elementClass.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();
                }
            }
        }

See attachment.


>>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...).
>>
>
>Supporting it as an option is OK, but it cannot become part of Ant's
>core.
>
>Cheers
>
>        Stefan
>
Cheers,
F. Wolff.


Re: Proxy support in Ant

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 02 May 2002, François Wolff <ej...@noos.fr> wrote:

> 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).

This is the one I had a look at.  Ant 1.x is supposed to be JDK 1.1
compatible (and even Ant2 will settle on 1.2, not 1.3).  Do you see
any chance to rewrite your changes so that they use refelection and
compile/run using JDK 1.1?

> 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...).

Supporting it as an option is OK, but it cannot become part of Ant's
core.

Cheers

        Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>