<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" <[EMAIL PROTECTED]> 
07-06-2006 12:20
Please respond to
"Ant Developers List" <dev@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 <[EMAIL PROTECTED]> 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" <[EMAIL PROTECTED]>
> 06-06-2006 19:41
> Please respond to
> "Ant Developers List" <dev@ant.apache.org>
>
>
> To
> "Ant Developers List" <dev@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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to