You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Carlton Brown <cb...@gmail.com> on 2011/03/16 19:09:13 UTC

How to reference a custom type via refid?

In a custom type, if I want to define it and reference it later, what's the
Java code to do that?

<my:type refid="foo" name="alpha"/>

<my:task>
    <my:type name="omega">
      <my:type refid="foo"/>
    </my:type>
</my:task>

The MyType class extends org.apache.tools.ant.types.DataType and it has a
method addConfigured(MyType).  But the above sample code creates a new
my:type instance, it does not seem to dereference to foo.

I read the docs but I didn't recognize anything that explains this.

Thanks in advance,

Carlton

Re: How to reference a custom type via refid?

Posted by Carlton Brown <cb...@gmail.com>.
On Thu, Mar 17, 2011 at 4:12 AM, Antoine Levy-Lambert <an...@gmx.de>wrote:

> On 3/16/11 2:09 PM, Carlton Brown wrote:
> > In a custom type, if I want to define it and reference it later, what's
> the
> > Java code to do that?
> >
> > <my:type refid="foo" name="alpha"/>
> this is probably what you want to write
>
> <my:type id="foo" name="alpha"/>
>
> > <my:task>
> >     <my:type name="omega">
> >       <my:type refid="foo"/>
> >     </my:type>
> > </my:task>
> >
> > The MyType class extends org.apache.tools.ant.types.DataType and it has a
> > method addConfigured(MyType).  But the above sample code creates a new
> > my:type instance, it does not seem to dereference to foo.
> Because MyType extends DataType it has a default setRefid(Reference r)
> method.
>

Thanks for your thoughts... this is the approach I ended up taking, not sure
if it's the best way, but it works:

  public void addConfigured(MyType arg) {
    if (arg.isReference()) {
      element = (MyType) arg.getRefid().getReferencedObject();
    } else {
      element = arg;
    }
// proceed to do stuff with element

Re: How to reference a custom type via refid?

Posted by Dominique Devienne <dd...@gmail.com>.
On Thu, Mar 17, 2011 at 3:12 AM, Antoine Levy-Lambert <an...@gmx.de> wrote:
> You may want to override this default method to do some extra checks,
> for instance to throw exceptions if the build file writer uses both refid
> and other attributes or nested elements.

Indeed. I've always thought this is one of the wrinkles of Ant, and
that this should be transparent to task writers, dealt with in the
framework. Would also serve users since they could then use refid's
inside any task, as nested elements, and even perhaps as attributes
with a little naming convention. Maybe one day ;) --DD

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How to reference a custom type via refid?

Posted by Antoine Levy-Lambert <an...@gmx.de>.
On 3/16/11 2:09 PM, Carlton Brown wrote:
> In a custom type, if I want to define it and reference it later, what's the
> Java code to do that?
>
> <my:type refid="foo" name="alpha"/>
this is probably what you want to write

<my:type id="foo" name="alpha"/>

> <my:task>
>     <my:type name="omega">
>       <my:type refid="foo"/>
>     </my:type>
> </my:task>
>
> The MyType class extends org.apache.tools.ant.types.DataType and it has a
> method addConfigured(MyType).  But the above sample code creates a new
> my:type instance, it does not seem to dereference to foo.
Because MyType extends DataType it has a default setRefid(Reference r)
method.

You may want to override this default method to do some extra checks,
for instance to throw exceptions
if the build file writer uses both refid and other attributes or nested
elements.

Here is the setRefid method of AbstractFileSet in the code of ant :

    public void setRefid(Reference r) throws BuildException {
        if (dir != null || defaultPatterns.hasPatterns(getProject())) {
            throw tooManyAttributes();
        }
        if (!additionalPatterns.isEmpty()) {
            throw noChildrenAllowed();
        }
        if (!selectors.isEmpty()) {
            throw noChildrenAllowed();
        }
        super.setRefid(r);
    }

> I read the docs but I didn't recognize anything that explains this.
>
> Thanks in advance,
>
> Carlton
>
Regards,

Antoine

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org