You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Wolfgang Häfelinger <wh...@epo.org> on 2006/06/06 19:16:28 UTC

how to access a MacroDef?

Hi,

a simple question - how can I retrieve a macro definition using 
ComponentHelper?

I tried something like

 ComponentHelper componenthelper = 
ComponentHelper.getComponentHelper(project());
 MacroDef def = (MacroDef) 
componenthelper.getTaskDefinitions().get(mymacroname);

just in order to understand that Hastable getTaskDefinitions() contains a 
String => Class
relation. 

So where are those MacroDef's hidden?

Sorry for this stupid question,
Wolfgang.

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


Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
Allright - thanks for the hints.

Would be nice to clean things up in a further version of Ant ( simple API 
treating
macrodef/task/presetdef/target similar) regarding "dynamic invocation". My
scripts/framework is now shorter and much easier to understand. However, 
this
is likely not happen as I'm probably the only one looking for this feature 
:-)))

Cheers,
Wolfgang.




"Peter Reilly" <pe...@gmail.com> 
12-06-2006 19:30
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






The work-around for preset is what has to be done in createTask.

After create task, or createComponent() is done,
one is has a normal java object.
It is too late in the life cycle of the object to use unknownelements.

One could create unknown elements and runtime configuratables (
see the code in macrodef to do this)  but is is quite tricky.

In your case it may be easier just to use java reflection to
call the setter methods (see introspection helper).

It has been a while since I was messing with UnknownElement/
RuntimeConfiguratable/IntrospectionHelper/ProjectHelper2 but I
can remember that it was painful!

Peter


On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> Hi Peter,
>
> I worked around the problem by implementing something like
>
> obj = componentHelper.createComponent (mymacroname);
> if (obj instanceof PreSetDef.PreSetDefinition) {
>       PreSetDef.PreSetDefinition psd;
>       psd = (PreSetDef.PreSetDefinition)obj;
>       obj = psd.createObject(getProject());
>   }
>   else
>   {
>       /* try to create task */
>       obj = getProject().createTask(this.name);
>   }
>
> if(obj instanceof MacroInstance)
> {
>     /* invoke Macro .. */
> }
>
> which works fine so far.
>
> Would you mind to have a look how I can pass paramters to a non-macro
> Task. It's
> not working for me. Here's my code where 'args' just contains a list of
> (key,value)
> pairs. I'm trying to pass those parameters via this RuntimeConfigurable
> class. That's
> probably wrong or something is missing. Give me a hint please ..
>
> if(obj instanceof  org.apache.tools.ant.Task)   // and   n o t   a
> marcodef
> {
>       RuntimeConfigurable rtc;
>       org.apache.tools.ant.Task T;
>
>       T = (org.apache.tools.ant.Task)obj;
>       rtc = T.getRuntimeConfigurableWrapper();
>
>       for(int i=0;i<args.length;++i)
>       {
>           P = (Param)args[i];
>           rtc.setAttribute(P.k,P.v);
>       }
>       T.execute();
>     }
>
> Cheers,
> Wolfgang.
>
>
>
>
> "Peter Reilly" <pe...@gmail.com>
> 12-06-2006 17:08
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <de...@ant.apache.org>
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> Thanks,
>
> I have entered a bug report on this. When I was writing presetdef,
> I had a lot of similar problems and as a result there is a number
> of places in the code that contain checks for presets being the
> result of createCompopent. The code here needs to create the
> task that preset points to. Due to change in job, I am not currently
> able to modify ant code (need to set up svn at home on windross ;-),
> but I will a look.
>
> Peter
>
>
> On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> >
> > <macrodef name="hello">
> >   <attribute name="msg" default="world" />
> >   <sequential>
> >     <echo>hello, @{msg}</echo>
> >   </sequential>
> > </macrodef>
> >
> > <presetdef name="hello-there">
> >   <hello msg="there" />
> > </presetdef>
> >
> > <script language="beanshell">
> >    import org.apache.tools.ant.taskdefs.MacroInstance ;
> >    import org.apache.tools.ant.BuildException;
> >
> >    task = project.createTask("hello-there");  // error
> > </script>
> >
> > This will throw a
> >
> > java.lang.ClassCastException
> >      at
> > org.apache.tools.ant.ComponentHelper.createNewTask 
(ComponentHelper.java
> > :462)
> >
> >
> > here
> >
> >     private Task createNewTask(String taskType) throws BuildException 
{
> >         Class c = getComponentClass(taskType);
> >         if (c == null || !(Task.class.isAssignableFrom (c))) {
> >             return null;
> >         }
> >         Task task = (Task) createComponent(taskType);  //=>
> > ComponentHelper.java:462
> >         if (task == null) {
> >             return null;
> >         }
> >          task.setTaskType(taskType);
> >
> >         // set default value, can be changed by the user
> >         task.setTaskName(taskType);
> >
> >         project.log("   +Task: " + taskType, Project.MSG_DEBUG);
> >         return task;
> >     }
> >
> > It works for "normal" macros.
> >
> >
> >
> >
> >
> >
> >
> >
> > "Peter Reilly" <pe...@gmail.com>
> > 07-06-2006 12:20
> > Please respond to
> > "Ant Developers List" <de...@ant.apache.org>
> >
> >
> > To
> > "Ant Developers List" <dev@ant.apache.org >
> > cc
> >
> > Subject
> > Re: how to access a MacroDef?
> >
> >
> >
> >
> >
> >
> > Since macro defs are tasks, it would be better to use the
> > project.createTask() method, and not try to use the
> > internal (although exposed trough the public classes and methods)
> > mechanizes of Ant.
> >
> >     <macrodef name="atest">
> >         <sequential>
> >            <echo>This is the atest macro</echo>
> >         </sequential>
> >     </macrodef>
> >
> >     <script language="beanshell">
> >        import org.apache.tools.ant.taskdefs.MacroInstance;
> >        import org.apache.tools.ant.BuildException;
> >        atest = project.createTask("atest");
> >        if (! (atest instanceof MacroInstance)) {
> >            throw new BuildException("atest is not a macro");
> >        }
> >        atest.execute();
> >
> >     </script>
> >
> > It would be necessary to call setOwningTarget as project.createTask()
> > does not know the current target.
> >
> > Peter
> >
> >
> > On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> > >
> > > Allright, it appears much more easier than expected.
> > >
> > > ComponentHelper componenthelper;
> > > Object obj;
> > > MacroInstance instance;
> > >
> > > componenthelper = ComponentHelper.getComponentHelper(project());
> > > obj             = componentHelper.createComponent (mymacroname);
> > > instance        = (MacroInstance)obj;
> > > instance.execute();
> > >
> > > Appears that there's no need to call setProject() or 
setOwningTarget()
> > on
> > > the macro's
> > > instance.
> > >
> > > However, not sure whether this is the "right" way to do. Perhaps 
Peter
> > can
> > > comment
> > > on this.
> > >
> > > Thanks for all help so far.
> > >
> > > Wolfgang.
> > >
> > >
> > >
> > >
> > > "Dominique Devienne" <dd...@gmail.com>
> > > 06-06-2006 19:41
> > > Please respond to
> > > "Ant Developers List" < dev@ant.apache.org>
> > >
> > >
> > > To
> > > "Ant Developers List" <de...@ant.apache.org>
> > > cc
> > >
> > > Subject
> > > Re: how to access a MacroDef?
> > >
> > >
> > >
> > >
> > >
> > >
> > > > I tried something like
> > > >
> > > >  ComponentHelper componenthelper =
> > > > ComponentHelper.getComponentHelper(project());
> > > >  MacroDef def = (MacroDef)
> > > > componenthelper.getTaskDefinitions().get(mymacroname);
> > > >
> > > > just in order to understand that Hastable getTaskDefinitions()
> > contains
> > > a
> > > > String => Class relation.
> > > >
> > > > So where are those MacroDef's hidden?
> > >
> > > So your 'def' is null, right?
> > >
> > > Try not casting it to MacroDef, and see which kind of Java Class is
> > > returned, if any. It may be an UnknownElement (my guess), a
> > > MacroInstance, etc...
> > >
> > > From the UE, you may be able to get a MacroInstance or a MacroDef, 
and
> > > if the later, configure it into a MacroInstance, which is what you
> > > want to run.
> > >
> > > I vaguely know this code only. Peter's the expert. --D
> > >
> > > 
---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: dev-help@ant.apache.org
> > >
> > >
> > >
> > >
> > > 
---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: dev-help@ant.apache.org
> > >
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



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


Re: how to access a MacroDef?

Posted by Peter Reilly <pe...@gmail.com>.
The work-around for preset is what has to be done in createTask.

After create task, or createComponent() is done,
one is has a normal java object.
It is too late in the life cycle of the object to use unknownelements.

One could create unknown elements and runtime configuratables (
see the code in macrodef to do this)  but is is quite tricky.

In your case it may be easier just to use java reflection to
call the setter methods (see introspection helper).

It has been a while since I was messing with UnknownElement/
RuntimeConfiguratable/IntrospectionHelper/ProjectHelper2 but I
can remember that it was painful!

Peter


On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> Hi Peter,
>
> I worked around the problem by implementing something like
>
> obj = componentHelper.createComponent (mymacroname);
> if (obj instanceof PreSetDef.PreSetDefinition) {
>       PreSetDef.PreSetDefinition psd;
>       psd = (PreSetDef.PreSetDefinition)obj;
>       obj = psd.createObject(getProject());
>   }
>   else
>   {
>       /* try to create task */
>       obj = getProject().createTask(this.name);
>   }
>
> if(obj instanceof MacroInstance)
> {
>     /* invoke Macro .. */
> }
>
> which works fine so far.
>
> Would you mind to have a look how I can pass paramters to a non-macro
> Task. It's
> not working for me. Here's my code where 'args' just contains a list of
> (key,value)
> pairs. I'm trying to pass those parameters via this RuntimeConfigurable
> class. That's
> probably wrong or something is missing. Give me a hint please ..
>
> if(obj instanceof  org.apache.tools.ant.Task)   // and   n o t   a
> marcodef
> {
>       RuntimeConfigurable rtc;
>       org.apache.tools.ant.Task T;
>
>       T = (org.apache.tools.ant.Task)obj;
>       rtc = T.getRuntimeConfigurableWrapper();
>
>       for(int i=0;i<args.length;++i)
>       {
>           P = (Param)args[i];
>           rtc.setAttribute(P.k,P.v);
>       }
>       T.execute();
>     }
>
> Cheers,
> Wolfgang.
>
>
>
>
> "Peter Reilly" <pe...@gmail.com>
> 12-06-2006 17:08
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <de...@ant.apache.org>
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> Thanks,
>
> I have entered a bug report on this. When I was writing presetdef,
> I had a lot of similar problems and as a result there is a number
> of places in the code that contain checks for presets being the
> result of createCompopent. The code here needs to create the
> task that preset points to. Due to change in job, I am not currently
> able to modify ant code (need to set up svn at home on windross ;-),
> but I will a look.
>
> Peter
>
>
> On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> >
> > <macrodef name="hello">
> >   <attribute name="msg" default="world" />
> >   <sequential>
> >     <echo>hello, @{msg}</echo>
> >   </sequential>
> > </macrodef>
> >
> > <presetdef name="hello-there">
> >   <hello msg="there" />
> > </presetdef>
> >
> > <script language="beanshell">
> >    import org.apache.tools.ant.taskdefs.MacroInstance ;
> >    import org.apache.tools.ant.BuildException;
> >
> >    task = project.createTask("hello-there");  // error
> > </script>
> >
> > This will throw a
> >
> > java.lang.ClassCastException
> >      at
> > org.apache.tools.ant.ComponentHelper.createNewTask (ComponentHelper.java
> > :462)
> >
> >
> > here
> >
> >     private Task createNewTask(String taskType) throws BuildException {
> >         Class c = getComponentClass(taskType);
> >         if (c == null || !(Task.class.isAssignableFrom (c))) {
> >             return null;
> >         }
> >         Task task = (Task) createComponent(taskType);  //=>
> > ComponentHelper.java:462
> >         if (task == null) {
> >             return null;
> >         }
> >          task.setTaskType(taskType);
> >
> >         // set default value, can be changed by the user
> >         task.setTaskName(taskType);
> >
> >         project.log("   +Task: " + taskType, Project.MSG_DEBUG);
> >         return task;
> >     }
> >
> > It works for "normal" macros.
> >
> >
> >
> >
> >
> >
> >
> >
> > "Peter Reilly" <pe...@gmail.com>
> > 07-06-2006 12:20
> > Please respond to
> > "Ant Developers List" <de...@ant.apache.org>
> >
> >
> > To
> > "Ant Developers List" <dev@ant.apache.org >
> > cc
> >
> > Subject
> > Re: how to access a MacroDef?
> >
> >
> >
> >
> >
> >
> > Since macro defs are tasks, it would be better to use the
> > project.createTask() method, and not try to use the
> > internal (although exposed trough the public classes and methods)
> > mechanizes of Ant.
> >
> >     <macrodef name="atest">
> >         <sequential>
> >            <echo>This is the atest macro</echo>
> >         </sequential>
> >     </macrodef>
> >
> >     <script language="beanshell">
> >        import org.apache.tools.ant.taskdefs.MacroInstance;
> >        import org.apache.tools.ant.BuildException;
> >        atest = project.createTask("atest");
> >        if (! (atest instanceof MacroInstance)) {
> >            throw new BuildException("atest is not a macro");
> >        }
> >        atest.execute();
> >
> >     </script>
> >
> > It would be necessary to call setOwningTarget as project.createTask()
> > does not know the current target.
> >
> > Peter
> >
> >
> > On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> > >
> > > Allright, it appears much more easier than expected.
> > >
> > > ComponentHelper componenthelper;
> > > Object obj;
> > > MacroInstance instance;
> > >
> > > componenthelper = ComponentHelper.getComponentHelper(project());
> > > obj             = componentHelper.createComponent (mymacroname);
> > > instance        = (MacroInstance)obj;
> > > instance.execute();
> > >
> > > Appears that there's no need to call setProject() or setOwningTarget()
> > on
> > > the macro's
> > > instance.
> > >
> > > However, not sure whether this is the "right" way to do. Perhaps Peter
> > can
> > > comment
> > > on this.
> > >
> > > Thanks for all help so far.
> > >
> > > Wolfgang.
> > >
> > >
> > >
> > >
> > > "Dominique Devienne" <dd...@gmail.com>
> > > 06-06-2006 19:41
> > > Please respond to
> > > "Ant Developers List" < dev@ant.apache.org>
> > >
> > >
> > > To
> > > "Ant Developers List" <de...@ant.apache.org>
> > > cc
> > >
> > > Subject
> > > Re: how to access a MacroDef?
> > >
> > >
> > >
> > >
> > >
> > >
> > > > I tried something like
> > > >
> > > >  ComponentHelper componenthelper =
> > > > ComponentHelper.getComponentHelper(project());
> > > >  MacroDef def = (MacroDef)
> > > > componenthelper.getTaskDefinitions().get(mymacroname);
> > > >
> > > > just in order to understand that Hastable getTaskDefinitions()
> > contains
> > > a
> > > > String => Class relation.
> > > >
> > > > So where are those MacroDef's hidden?
> > >
> > > So your 'def' is null, right?
> > >
> > > Try not casting it to MacroDef, and see which kind of Java Class is
> > > returned, if any. It may be an UnknownElement (my guess), a
> > > MacroInstance, etc...
> > >
> > > From the UE, you may be able to get a MacroInstance or a MacroDef, and
> > > if the later, configure it into a MacroInstance, which is what you
> > > want to run.
> > >
> > > I vaguely know this code only. Peter's the expert. --D
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: dev-help@ant.apache.org
> > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: dev-help@ant.apache.org
> > >
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
Hi Peter,

I worked around the problem by implementing something like

 obj = componentHelper.createComponent (mymacroname);
 if (obj instanceof PreSetDef.PreSetDefinition) {
      PreSetDef.PreSetDefinition psd;
      psd = (PreSetDef.PreSetDefinition)obj;
      obj = psd.createObject(getProject());
  }
  else
  {
      /* try to create task */
      obj = getProject().createTask(this.name);
  }

 if(obj instanceof MacroInstance)
 {
    /* invoke Macro .. */
 }

which works fine so far.

Would you mind to have a look how I can pass paramters to a non-macro 
Task. It's
not working for me. Here's my code where 'args' just contains a list of 
(key,value)
pairs. I'm trying to pass those parameters via this RuntimeConfigurable 
class. That's
probably wrong or something is missing. Give me a hint please ..

if(obj instanceof  org.apache.tools.ant.Task)   // and   n o t   a 
marcodef
{
      RuntimeConfigurable rtc;
      org.apache.tools.ant.Task T;
 
      T = (org.apache.tools.ant.Task)obj;
      rtc = T.getRuntimeConfigurableWrapper();

      for(int i=0;i<args.length;++i)
      {
          P = (Param)args[i];
          rtc.setAttribute(P.k,P.v);
      }
      T.execute();
    }

Cheers,
Wolfgang.
 



"Peter Reilly" <pe...@gmail.com> 
12-06-2006 17:08
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






Thanks,

I have entered a bug report on this. When I was writing presetdef,
I had a lot of similar problems and as a result there is a number
of places in the code that contain checks for presets being the
result of createCompopent. The code here needs to create the
task that preset points to. Due to change in job, I am not currently
able to modify ant code (need to set up svn at home on windross ;-),
but I will a look.

Peter


On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> <macrodef name="hello">
>   <attribute name="msg" default="world" />
>   <sequential>
>     <echo>hello, @{msg}</echo>
>   </sequential>
> </macrodef>
>
> <presetdef name="hello-there">
>   <hello msg="there" />
> </presetdef>
>
> <script language="beanshell">
>    import org.apache.tools.ant.taskdefs.MacroInstance ;
>    import org.apache.tools.ant.BuildException;
>
>    task = project.createTask("hello-there");  // error
> </script>
>
> This will throw a
>
> java.lang.ClassCastException
>      at
> org.apache.tools.ant.ComponentHelper.createNewTask (ComponentHelper.java
> :462)
>
>
> here
>
>     private Task createNewTask(String taskType) throws BuildException {
>         Class c = getComponentClass(taskType);
>         if (c == null || !(Task.class.isAssignableFrom (c))) {
>             return null;
>         }
>         Task task = (Task) createComponent(taskType);  //=>
> ComponentHelper.java:462
>         if (task == null) {
>             return null;
>         }
>          task.setTaskType(taskType);
>
>         // set default value, can be changed by the user
>         task.setTaskName(taskType);
>
>         project.log("   +Task: " + taskType, Project.MSG_DEBUG);
>         return task;
>     }
>
> It works for "normal" macros.
>
>
>
>
>
>
>
>
> "Peter Reilly" <pe...@gmail.com>
> 07-06-2006 12:20
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <dev@ant.apache.org >
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> Since macro defs are tasks, it would be better to use the
> project.createTask() method, and not try to use the
> internal (although exposed trough the public classes and methods)
> mechanizes of Ant.
>
>     <macrodef name="atest">
>         <sequential>
>            <echo>This is the atest macro</echo>
>         </sequential>
>     </macrodef>
>
>     <script language="beanshell">
>        import org.apache.tools.ant.taskdefs.MacroInstance;
>        import org.apache.tools.ant.BuildException;
>        atest = project.createTask("atest");
>        if (! (atest instanceof MacroInstance)) {
>            throw new BuildException("atest is not a macro");
>        }
>        atest.execute();
>
>     </script>
>
> It would be necessary to call setOwningTarget as project.createTask()
> does not know the current target.
>
> Peter
>
>
> On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> >
> > Allright, it appears much more easier than expected.
> >
> > ComponentHelper componenthelper;
> > Object obj;
> > MacroInstance instance;
> >
> > componenthelper = ComponentHelper.getComponentHelper(project());
> > obj             = componentHelper.createComponent (mymacroname);
> > instance        = (MacroInstance)obj;
> > instance.execute();
> >
> > Appears that there's no need to call setProject() or setOwningTarget()
> on
> > the macro's
> > instance.
> >
> > However, not sure whether this is the "right" way to do. Perhaps Peter
> can
> > comment
> > on this.
> >
> > Thanks for all help so far.
> >
> > Wolfgang.
> >
> >
> >
> >
> > "Dominique Devienne" <dd...@gmail.com>
> > 06-06-2006 19:41
> > Please respond to
> > "Ant Developers List" < dev@ant.apache.org>
> >
> >
> > To
> > "Ant Developers List" <de...@ant.apache.org>
> > cc
> >
> > Subject
> > Re: how to access a MacroDef?
> >
> >
> >
> >
> >
> >
> > > I tried something like
> > >
> > >  ComponentHelper componenthelper =
> > > ComponentHelper.getComponentHelper(project());
> > >  MacroDef def = (MacroDef)
> > > componenthelper.getTaskDefinitions().get(mymacroname);
> > >
> > > just in order to understand that Hastable getTaskDefinitions()
> contains
> > a
> > > String => Class relation.
> > >
> > > So where are those MacroDef's hidden?
> >
> > So your 'def' is null, right?
> >
> > Try not casting it to MacroDef, and see which kind of Java Class is
> > returned, if any. It may be an UnknownElement (my guess), a
> > MacroInstance, etc...
> >
> > From the UE, you may be able to get a MacroInstance or a MacroDef, and
> > if the later, configure it into a MacroInstance, which is what you
> > want to run.
> >
> > I vaguely know this code only. Peter's the expert. --D
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



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


Re: how to access a MacroDef?

Posted by Peter Reilly <pe...@gmail.com>.
Thanks,

I have entered a bug report on this. When I was writing presetdef,
I had a lot of similar problems and as a result there is a number
of places in the code that contain checks for presets being the
result of createCompopent. The code here needs to create the
task that preset points to. Due to change in job, I am not currently
able to modify ant code (need to set up svn at home on windross ;-),
but I will a look.

Peter


On 6/12/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> <macrodef name="hello">
>   <attribute name="msg" default="world" />
>   <sequential>
>     <echo>hello, @{msg}</echo>
>   </sequential>
> </macrodef>
>
> <presetdef name="hello-there">
>   <hello msg="there" />
> </presetdef>
>
> <script language="beanshell">
>    import org.apache.tools.ant.taskdefs.MacroInstance ;
>    import org.apache.tools.ant.BuildException;
>
>    task = project.createTask("hello-there");  // error
> </script>
>
> This will throw a
>
> java.lang.ClassCastException
>      at
> org.apache.tools.ant.ComponentHelper.createNewTask (ComponentHelper.java
> :462)
>
>
> here
>
>     private Task createNewTask(String taskType) throws BuildException {
>         Class c = getComponentClass(taskType);
>         if (c == null || !(Task.class.isAssignableFrom (c))) {
>             return null;
>         }
>         Task task = (Task) createComponent(taskType);  //=>
> ComponentHelper.java:462
>         if (task == null) {
>             return null;
>         }
>          task.setTaskType(taskType);
>
>         // set default value, can be changed by the user
>         task.setTaskName(taskType);
>
>         project.log("   +Task: " + taskType, Project.MSG_DEBUG);
>         return task;
>     }
>
> It works for "normal" macros.
>
>
>
>
>
>
>
>
> "Peter Reilly" <pe...@gmail.com>
> 07-06-2006 12:20
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <dev@ant.apache.org >
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> Since macro defs are tasks, it would be better to use the
> project.createTask() method, and not try to use the
> internal (although exposed trough the public classes and methods)
> mechanizes of Ant.
>
>     <macrodef name="atest">
>         <sequential>
>            <echo>This is the atest macro</echo>
>         </sequential>
>     </macrodef>
>
>     <script language="beanshell">
>        import org.apache.tools.ant.taskdefs.MacroInstance;
>        import org.apache.tools.ant.BuildException;
>        atest = project.createTask("atest");
>        if (! (atest instanceof MacroInstance)) {
>            throw new BuildException("atest is not a macro");
>        }
>        atest.execute();
>
>     </script>
>
> It would be necessary to call setOwningTarget as project.createTask()
> does not know the current target.
>
> Peter
>
>
> On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
> >
> > Allright, it appears much more easier than expected.
> >
> > ComponentHelper componenthelper;
> > Object obj;
> > MacroInstance instance;
> >
> > componenthelper = ComponentHelper.getComponentHelper(project());
> > obj             = componentHelper.createComponent (mymacroname);
> > instance        = (MacroInstance)obj;
> > instance.execute();
> >
> > Appears that there's no need to call setProject() or setOwningTarget()
> on
> > the macro's
> > instance.
> >
> > However, not sure whether this is the "right" way to do. Perhaps Peter
> can
> > comment
> > on this.
> >
> > Thanks for all help so far.
> >
> > Wolfgang.
> >
> >
> >
> >
> > "Dominique Devienne" <dd...@gmail.com>
> > 06-06-2006 19:41
> > Please respond to
> > "Ant Developers List" < dev@ant.apache.org>
> >
> >
> > To
> > "Ant Developers List" <de...@ant.apache.org>
> > cc
> >
> > Subject
> > Re: how to access a MacroDef?
> >
> >
> >
> >
> >
> >
> > > I tried something like
> > >
> > >  ComponentHelper componenthelper =
> > > ComponentHelper.getComponentHelper(project());
> > >  MacroDef def = (MacroDef)
> > > componenthelper.getTaskDefinitions().get(mymacroname);
> > >
> > > just in order to understand that Hastable getTaskDefinitions()
> contains
> > a
> > > String => Class relation.
> > >
> > > So where are those MacroDef's hidden?
> >
> > So your 'def' is null, right?
> >
> > Try not casting it to MacroDef, and see which kind of Java Class is
> > returned, if any. It may be an UnknownElement (my guess), a
> > MacroInstance, etc...
> >
> > From the UE, you may be able to get a MacroInstance or a MacroDef, and
> > if the later, configure it into a MacroInstance, which is what you
> > want to run.
> >
> > I vaguely know this code only. Peter's the expert. --D
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
Hmm, I have seen that project.createTask() does more or less the same as I 
do. However,
I do not understand what caching my task and invalidation means. Perhaps I 
do not to care
about it anyway.

Another thing I tried is to call my macro or task with attributes. I got 
it working for macros:

        <macrodef name="atest">
          <attribute name="foo" />
        <sequential>
           <echo>This is the atest macro where foo = @{foo}</echo>
        </sequential>
      </macrodef>

        atest = project.createTask("atest");
      if (! (atest instanceof MacroInstance)) {
         throw new BuildException("atest is not a macro");
      }
      MacroInstance inst = (MacroInstance)atest;
      inst.setDynamicAttribute("foo",bar");
      atest.execute();

I tried then something similar for (ordinary?) tasks without success. 
That's what I
want to do be able to do:

 <run-macro name="echo">
   <arg name="message" value="hello" />
 </run-macro>

I implemented this somehow like

task = project.createTask("echo");
if (task instanceof MacroInstance)
{
   ..
}
else
{
  /* call echo with attribute message */
  RuntimeConfigurable rtc = task.getRuntimeConfigurableWrapper();
  rtc.setAttribute("message","hello");
}
task.execute();
 
I can see that indeed echo is called but it's being called without
my arguments.

Any hint on this as well?

Wolfgang.






"Peter Reilly" <pe...@gmail.com> 
07-06-2006 12:20
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






Since macro defs are tasks, it would be better to use the
project.createTask() method, and not try to use the
internal (although exposed trough the public classes and methods)
mechanizes of Ant.

    <macrodef name="atest">
        <sequential>
           <echo>This is the atest macro</echo>
        </sequential>
    </macrodef>

    <script language="beanshell">
       import org.apache.tools.ant.taskdefs.MacroInstance;
       import org.apache.tools.ant.BuildException;
       atest = project.createTask("atest");
       if (! (atest instanceof MacroInstance)) {
           throw new BuildException("atest is not a macro");
       }
       atest.execute();

    </script>

It would be necessary to call setOwningTarget as project.createTask()
does not know the current target.

Peter


On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> Allright, it appears much more easier than expected.
>
> ComponentHelper componenthelper;
> Object obj;
> MacroInstance instance;
>
> componenthelper = ComponentHelper.getComponentHelper(project());
> obj             = componentHelper.createComponent(mymacroname);
> instance        = (MacroInstance)obj;
> instance.execute();
>
> Appears that there's no need to call setProject() or setOwningTarget() 
on
> the macro's
> instance.
>
> However, not sure whether this is the "right" way to do. Perhaps Peter 
can
> comment
> on this.
>
> Thanks for all help so far.
>
> Wolfgang.
>
>
>
>
> "Dominique Devienne" <dd...@gmail.com>
> 06-06-2006 19:41
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <de...@ant.apache.org>
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> > I tried something like
> >
> >  ComponentHelper componenthelper =
> > ComponentHelper.getComponentHelper(project());
> >  MacroDef def = (MacroDef)
> > componenthelper.getTaskDefinitions().get(mymacroname);
> >
> > just in order to understand that Hastable getTaskDefinitions() 
contains
> a
> > String => Class relation.
> >
> > So where are those MacroDef's hidden?
>
> So your 'def' is null, right?
>
> Try not casting it to MacroDef, and see which kind of Java Class is
> returned, if any. It may be an UnknownElement (my guess), a
> MacroInstance, etc...
>
> From the UE, you may be able to get a MacroInstance or a MacroDef, and
> if the later, configure it into a MacroInstance, which is what you
> want to run.
>
> I vaguely know this code only. Peter's the expert. --D
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



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


Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
<macrodef name="hello">
  <attribute name="msg" default="world" />
  <sequential>
    <echo>hello, @{msg}</echo>
  </sequential>
</macrodef>

<presetdef name="hello-there">
  <hello msg="there" />
</presetdef>

<script language="beanshell">
   import org.apache.tools.ant.taskdefs.MacroInstance;
   import org.apache.tools.ant.BuildException;

   task = project.createTask("hello-there");  // error
</script>

This will throw a 

 java.lang.ClassCastException
     at 
org.apache.tools.ant.ComponentHelper.createNewTask(ComponentHelper.java:462)


here

    private Task createNewTask(String taskType) throws BuildException {
        Class c = getComponentClass(taskType);
        if (c == null || !(Task.class.isAssignableFrom(c))) {
            return null;
        }
        Task task = (Task) createComponent(taskType);  //=> 
ComponentHelper.java:462
        if (task == null) {
            return null;
        }
        task.setTaskType(taskType);

        // set default value, can be changed by the user
        task.setTaskName(taskType);

        project.log("   +Task: " + taskType, Project.MSG_DEBUG);
        return task;
    }

It works for "normal" macros.








"Peter Reilly" <pe...@gmail.com> 
07-06-2006 12:20
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






Since macro defs are tasks, it would be better to use the
project.createTask() method, and not try to use the
internal (although exposed trough the public classes and methods)
mechanizes of Ant.

    <macrodef name="atest">
        <sequential>
           <echo>This is the atest macro</echo>
        </sequential>
    </macrodef>

    <script language="beanshell">
       import org.apache.tools.ant.taskdefs.MacroInstance;
       import org.apache.tools.ant.BuildException;
       atest = project.createTask("atest");
       if (! (atest instanceof MacroInstance)) {
           throw new BuildException("atest is not a macro");
       }
       atest.execute();

    </script>

It would be necessary to call setOwningTarget as project.createTask()
does not know the current target.

Peter


On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> Allright, it appears much more easier than expected.
>
> ComponentHelper componenthelper;
> Object obj;
> MacroInstance instance;
>
> componenthelper = ComponentHelper.getComponentHelper(project());
> obj             = componentHelper.createComponent(mymacroname);
> instance        = (MacroInstance)obj;
> instance.execute();
>
> Appears that there's no need to call setProject() or setOwningTarget() 
on
> the macro's
> instance.
>
> However, not sure whether this is the "right" way to do. Perhaps Peter 
can
> comment
> on this.
>
> Thanks for all help so far.
>
> Wolfgang.
>
>
>
>
> "Dominique Devienne" <dd...@gmail.com>
> 06-06-2006 19:41
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <de...@ant.apache.org>
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> > I tried something like
> >
> >  ComponentHelper componenthelper =
> > ComponentHelper.getComponentHelper(project());
> >  MacroDef def = (MacroDef)
> > componenthelper.getTaskDefinitions().get(mymacroname);
> >
> > just in order to understand that Hastable getTaskDefinitions() 
contains
> a
> > String => Class relation.
> >
> > So where are those MacroDef's hidden?
>
> So your 'def' is null, right?
>
> Try not casting it to MacroDef, and see which kind of Java Class is
> returned, if any. It may be an UnknownElement (my guess), a
> MacroInstance, etc...
>
> From the UE, you may be able to get a MacroInstance or a MacroDef, and
> if the later, configure it into a MacroInstance, which is what you
> want to run.
>
> I vaguely know this code only. Peter's the expert. --D
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



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


Re: how to access a MacroDef?

Posted by Peter Reilly <pe...@gmail.com>.
Since macro defs are tasks, it would be better to use the
project.createTask() method, and not try to use the
internal (although exposed trough the public classes and methods)
mechanizes of Ant.

    <macrodef name="atest">
        <sequential>
           <echo>This is the atest macro</echo>
        </sequential>
    </macrodef>

    <script language="beanshell">
       import org.apache.tools.ant.taskdefs.MacroInstance;
       import org.apache.tools.ant.BuildException;
       atest = project.createTask("atest");
       if (! (atest instanceof MacroInstance)) {
           throw new BuildException("atest is not a macro");
       }
       atest.execute();

    </script>

It would be necessary to call setOwningTarget as project.createTask()
does not know the current target.

Peter


On 6/7/06, Wolfgang Häfelinger <wh...@epo.org> wrote:
>
> Allright, it appears much more easier than expected.
>
> ComponentHelper componenthelper;
> Object obj;
> MacroInstance instance;
>
> componenthelper = ComponentHelper.getComponentHelper(project());
> obj             = componentHelper.createComponent(mymacroname);
> instance        = (MacroInstance)obj;
> instance.execute();
>
> Appears that there's no need to call setProject() or setOwningTarget() on
> the macro's
> instance.
>
> However, not sure whether this is the "right" way to do. Perhaps Peter can
> comment
> on this.
>
> Thanks for all help so far.
>
> Wolfgang.
>
>
>
>
> "Dominique Devienne" <dd...@gmail.com>
> 06-06-2006 19:41
> Please respond to
> "Ant Developers List" <de...@ant.apache.org>
>
>
> To
> "Ant Developers List" <de...@ant.apache.org>
> cc
>
> Subject
> Re: how to access a MacroDef?
>
>
>
>
>
>
> > I tried something like
> >
> >  ComponentHelper componenthelper =
> > ComponentHelper.getComponentHelper(project());
> >  MacroDef def = (MacroDef)
> > componenthelper.getTaskDefinitions().get(mymacroname);
> >
> > just in order to understand that Hastable getTaskDefinitions() contains
> a
> > String => Class relation.
> >
> > So where are those MacroDef's hidden?
>
> So your 'def' is null, right?
>
> Try not casting it to MacroDef, and see which kind of Java Class is
> returned, if any. It may be an UnknownElement (my guess), a
> MacroInstance, etc...
>
> From the UE, you may be able to get a MacroInstance or a MacroDef, and
> if the later, configure it into a MacroInstance, which is what you
> want to run.
>
> I vaguely know this code only. Peter's the expert. --D
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
>> So your 'def' is null, right?


Actually it's the case that 

        componenthelper.getTaskDefinitions().get(mymacroname);

returns a object of type Class:

        Class C = (Class) 
componenthelper.getTaskDefinitions().get(mymacroname);

In case 'mymacroname' contanis the name of a macrodef I'm getting


        C.getName() => 'org.apache.tools.ant.taskdefs.MacroInstance'


Still no clue how to get hold of my macro's definition. Peter, any hint?







"Dominique Devienne" <dd...@gmail.com> 
06-06-2006 19:41
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






> I tried something like
>
>  ComponentHelper componenthelper =
> ComponentHelper.getComponentHelper(project());
>  MacroDef def = (MacroDef)
> componenthelper.getTaskDefinitions().get(mymacroname);
>
> just in order to understand that Hastable getTaskDefinitions() contains 
a
> String => Class relation.
>
> So where are those MacroDef's hidden?

So your 'def' is null, right?

Try not casting it to MacroDef, and see which kind of Java Class is
returned, if any. It may be an UnknownElement (my guess), a
MacroInstance, etc...

>From the UE, you may be able to get a MacroInstance or a MacroDef, and
if the later, configure it into a MacroInstance, which is what you
want to run.

I vaguely know this code only. Peter's the expert. --D

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




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


Re: how to access a MacroDef?

Posted by Wolfgang Häfelinger <wh...@epo.org>.
Allright, it appears much more easier than expected.

ComponentHelper componenthelper;
Object obj;
MacroInstance instance;

componenthelper = ComponentHelper.getComponentHelper(project());
obj             = componentHelper.createComponent(mymacroname);
instance        = (MacroInstance)obj;
instance.execute();

Appears that there's no need to call setProject() or setOwningTarget() on 
the macro's 
instance.

However, not sure whether this is the "right" way to do. Perhaps Peter can 
comment
on this.

Thanks for all help so far.

Wolfgang.




"Dominique Devienne" <dd...@gmail.com> 
06-06-2006 19:41
Please respond to
"Ant Developers List" <de...@ant.apache.org>


To
"Ant Developers List" <de...@ant.apache.org>
cc

Subject
Re: how to access a MacroDef?






> I tried something like
>
>  ComponentHelper componenthelper =
> ComponentHelper.getComponentHelper(project());
>  MacroDef def = (MacroDef)
> componenthelper.getTaskDefinitions().get(mymacroname);
>
> just in order to understand that Hastable getTaskDefinitions() contains 
a
> String => Class relation.
>
> So where are those MacroDef's hidden?

So your 'def' is null, right?

Try not casting it to MacroDef, and see which kind of Java Class is
returned, if any. It may be an UnknownElement (my guess), a
MacroInstance, etc...

>From the UE, you may be able to get a MacroInstance or a MacroDef, and
if the later, configure it into a MacroInstance, which is what you
want to run.

I vaguely know this code only. Peter's the expert. --D

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




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


Re: how to access a MacroDef?

Posted by Dominique Devienne <dd...@gmail.com>.
> I tried something like
>
>  ComponentHelper componenthelper =
> ComponentHelper.getComponentHelper(project());
>  MacroDef def = (MacroDef)
> componenthelper.getTaskDefinitions().get(mymacroname);
>
> just in order to understand that Hastable getTaskDefinitions() contains a
> String => Class relation.
>
> So where are those MacroDef's hidden?

So your 'def' is null, right?

Try not casting it to MacroDef, and see which kind of Java Class is
returned, if any. It may be an UnknownElement (my guess), a
MacroInstance, etc...

>From the UE, you may be able to get a MacroInstance or a MacroDef, and
if the later, configure it into a MacroInstance, which is what you
want to run.

I vaguely know this code only. Peter's the expert. --D

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