You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Ashley Williams <ag...@mac.com> on 2005/10/28 18:04:59 UTC

How do I walk the task tree from Java?

Hi,

I'm trying to write a method in java that takes a root target and  
then walks down the task tree until it comes to the desired object,  
but don't seem to find the api to do this. I sort of got it to work  
in Ant 1.6.2 but wasn't really happy with the results as I still  
don't fully understand what I've written:

However I need to use Ant 1.5.2 which is missing two crucial methods:  
RuntimeConfigurableWrapper.getChildren and  
RuntimeConfigurable.getProxy() - is there anything I should be using  
in it's place? Is my strategy the right one?

I have a hunch that I'm doing the wrong thing as no tree should be  
this difficult to walk as i seem to have made it.

Many Thanks
- AW

/**
          * Recursively searches the given task until a match is  
found for the given path.
          * @param searchTask
          * @param pathComponents
          * @param pathIndex
          * @return
          */
         private Object findInTask(Task searchTask, String[]  
pathComponents, int pathIndex) {
             Object resultObj;

             boolean morePathComponents = pathIndex <  
pathComponents.length;
             if (morePathComponents) {
                 resultObj = null;
                 Enumeration children =  
searchTask.getRuntimeConfigurableWrapper().getChildren();

                 search:
                 while (children.hasMoreElements()) {
                     RuntimeConfigurable child =  
(RuntimeConfigurable) children.nextElement();
                     if (child.getElementTag().equals(pathComponents 
[pathIndex])) {
                         // we have a match but need to check if  
there are more children to recurse
                         Object childProxy = child.getProxy();
                         if (childProxy instanceof Task) {
                             resultObj = findInTask((Task)  
childProxy, pathComponents, pathIndex + 1);
                         } else {
                             resultObj = childProxy;
                         }
                         break search;
                     }
                 }
             } else {
                 resultObj = searchTask;
             }

             return resultObj;
         }



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


Re: How do I walk the task tree from Java?

Posted by Ashley Williams <ag...@mac.com>.
Yes this helps a great deal - it helps me to decide to cut my losses ;)
I have one more throw of the dice, which is to take a look at the  
BuildListener
code - maybe, just maybe Ant will be kind enough to deliver me the  
correct
target/task so that I can change the attributes before execution.

We shall see...

Thanks
- Ashley


On 28 Oct 2005, at 18:02, Dale Anson wrote:

> Hi Ashley,
>
> It is that hard, and maybe even harder.  Antelope has a progress  
> bar to show how progress of a build.  It tries to count the number  
> of tasks that will be executed prior to the build starting, then  
> updates the progress bar based on the taskFinished build events, so  
> basically, it is traversing the tree as you're attempting. Here are  
> the comments at the top of that class:
>
> "The progress bar show progress of a running Ant target by counting  
> the number
> of tasks that the target performs and incrementing the progress bar  
> as each
> task is completed. The assumption is that a single target will be  
> called.
> While this target may call other targets, the progress is based on  
> the initial
> target. The initial target must be set prior to the build starting  
> by calling
> <code>setExecutingTarget</code>.
> <p>
> Ant is not particularly friendly about providing information about  
> itself --
> Project does not tell which target is currently executing,  
> BuildEvent does not
> tell which target started the execution cycle, TaskContainer  
> provides no
> information about nested tasks, and so on. This class has a number  
> of hacks
> to get around these limitiations, some of which are strictly  
> against the
> spirit of Java programming (e.g., I use PrivilegedAccessor to read  
> the private
> fields in RuntimeConfigurable). These hacks make this a fragile  
> class as it
> is not strictly based on the public Ant API.
> <p>
> Ant 1.6 made it even harder. This class needs a bit more work."
>
> This progress listener worked fairly well with Ant 1.5, not as well  
> with Ant 1.6, but you might get some pointers from it since you're  
> using 1.5.2.  You do need access to the "children" field in  
> RuntimeConfigurable, I didn't find a way to get it without  
> resorting to cheating with PrivilegedAccessor.
>
> You can see the code for the progress listener at:
>
> http://antelope.tigris.org/source/browse/antelope/src/ise/antelope/ 
> common/AntProgressListener.java?rev=1.5&view=markup
>
> The PrivilegedAccessor is at:
>
> http://antelope.tigris.org/source/browse/antelope/src/ise/library/ 
> PrivilegedAccessor.java?rev=1.4&view=markup
>
>
> Hope this helps!
>
> Dale
>
>
> Ashley Williams wrote:
>
>
>> Hi,
>>
>> I'm trying to write a method in java that takes a root target and   
>> then walks down the task tree until it comes to the desired  
>> object,  but don't seem to find the api to do this. I sort of got  
>> it to work  in Ant 1.6.2 but wasn't really happy with the results  
>> as I still  don't fully understand what I've written:
>>
>> However I need to use Ant 1.5.2 which is missing two crucial  
>> methods:  RuntimeConfigurableWrapper.getChildren and   
>> RuntimeConfigurable.getProxy() - is there anything I should be  
>> using  in it's place? Is my strategy the right one?
>>
>> I have a hunch that I'm doing the wrong thing as no tree should  
>> be  this difficult to walk as i seem to have made it.
>>
>> Many Thanks
>> - AW
>>
>> /**
>>          * Recursively searches the given task until a match is   
>> found for the given path.
>>          * @param searchTask
>>          * @param pathComponents
>>          * @param pathIndex
>>          * @return
>>          */
>>         private Object findInTask(Task searchTask, String[]   
>> pathComponents, int pathIndex) {
>>             Object resultObj;
>>
>>             boolean morePathComponents = pathIndex <   
>> pathComponents.length;
>>             if (morePathComponents) {
>>                 resultObj = null;
>>                 Enumeration children =   
>> searchTask.getRuntimeConfigurableWrapper().getChildren();
>>
>>                 search:
>>                 while (children.hasMoreElements()) {
>>                     RuntimeConfigurable child =   
>> (RuntimeConfigurable) children.nextElement();
>>                     if (child.getElementTag().equals 
>> (pathComponents [pathIndex])) {
>>                         // we have a match but need to check if   
>> there are more children to recurse
>>                         Object childProxy = child.getProxy();
>>                         if (childProxy instanceof Task) {
>>                             resultObj = findInTask((Task)   
>> childProxy, pathComponents, pathIndex + 1);
>>                         } else {
>>                             resultObj = childProxy;
>>                         }
>>                         break search;
>>                     }
>>                 }
>>             } else {
>>                 resultObj = searchTask;
>>             }
>>
>>             return resultObj;
>>         }
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


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


Re: How do I walk the task tree from Java?

Posted by Dale Anson <da...@grafidog.com>.
Hi Ashley,

It is that hard, and maybe even harder.  Antelope has a progress bar to 
show how progress of a build.  It tries to count the number of tasks 
that will be executed prior to the build starting, then updates the 
progress bar based on the taskFinished build events, so basically, it is 
traversing the tree as you're attempting. Here are the comments at the 
top of that class:

"The progress bar show progress of a running Ant target by counting the 
number
of tasks that the target performs and incrementing the progress bar as each
task is completed. The assumption is that a single target will be called.
While this target may call other targets, the progress is based on the 
initial
target. The initial target must be set prior to the build starting by 
calling
<code>setExecutingTarget</code>.
<p>
Ant is not particularly friendly about providing information about itself --
Project does not tell which target is currently executing, BuildEvent 
does not
tell which target started the execution cycle, TaskContainer provides no
information about nested tasks, and so on. This class has a number of hacks
to get around these limitiations, some of which are strictly against the
spirit of Java programming (e.g., I use PrivilegedAccessor to read the 
private
fields in RuntimeConfigurable). These hacks make this a fragile class as it
is not strictly based on the public Ant API.
<p>
Ant 1.6 made it even harder. This class needs a bit more work."

This progress listener worked fairly well with Ant 1.5, not as well with 
Ant 1.6, but you might get some pointers from it since you're using 
1.5.2.  You do need access to the "children" field in 
RuntimeConfigurable, I didn't find a way to get it without resorting to 
cheating with PrivilegedAccessor.

You can see the code for the progress listener at:

http://antelope.tigris.org/source/browse/antelope/src/ise/antelope/common/AntProgressListener.java?rev=1.5&view=markup

The PrivilegedAccessor is at:

http://antelope.tigris.org/source/browse/antelope/src/ise/library/PrivilegedAccessor.java?rev=1.4&view=markup


Hope this helps!

Dale


Ashley Williams wrote:

> Hi,
>
> I'm trying to write a method in java that takes a root target and  
> then walks down the task tree until it comes to the desired object,  
> but don't seem to find the api to do this. I sort of got it to work  
> in Ant 1.6.2 but wasn't really happy with the results as I still  
> don't fully understand what I've written:
>
> However I need to use Ant 1.5.2 which is missing two crucial methods:  
> RuntimeConfigurableWrapper.getChildren and  
> RuntimeConfigurable.getProxy() - is there anything I should be using  
> in it's place? Is my strategy the right one?
>
> I have a hunch that I'm doing the wrong thing as no tree should be  
> this difficult to walk as i seem to have made it.
>
> Many Thanks
> - AW
>
> /**
>          * Recursively searches the given task until a match is  found 
> for the given path.
>          * @param searchTask
>          * @param pathComponents
>          * @param pathIndex
>          * @return
>          */
>         private Object findInTask(Task searchTask, String[]  
> pathComponents, int pathIndex) {
>             Object resultObj;
>
>             boolean morePathComponents = pathIndex <  
> pathComponents.length;
>             if (morePathComponents) {
>                 resultObj = null;
>                 Enumeration children =  
> searchTask.getRuntimeConfigurableWrapper().getChildren();
>
>                 search:
>                 while (children.hasMoreElements()) {
>                     RuntimeConfigurable child =  (RuntimeConfigurable) 
> children.nextElement();
>                     if (child.getElementTag().equals(pathComponents 
> [pathIndex])) {
>                         // we have a match but need to check if  there 
> are more children to recurse
>                         Object childProxy = child.getProxy();
>                         if (childProxy instanceof Task) {
>                             resultObj = findInTask((Task)  childProxy, 
> pathComponents, pathIndex + 1);
>                         } else {
>                             resultObj = childProxy;
>                         }
>                         break search;
>                     }
>                 }
>             } else {
>                 resultObj = searchTask;
>             }
>
>             return resultObj;
>         }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>

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