Java Mailing List Archive

http://www.ant-tasks.com/

Apache Ant Archive

» Ant Users List
» Ant Developers List
How do I get at the project base dir inside a subclass of BaseExtendSelector

How do I get at the project base dir inside a subclass of BaseExtendSelector

2003-03-31       - By peter reilly
Reply:     1     2  

It is (I think) a bug of ant.

I made a patch to get UnknownElement.java to set
the project on created children. I use this for
custom filters (and custom conditions and selectors).

The patch is part of bugzilla: 18312.

The relevant part follows:

Peter

Index: src/main/org/apache/tools/ant/UnknownElement.java
==================================================================RCS file:
/home/cvspublic/ant/src/main/org/apache/tools/ant/UnknownElement.java,v
retrieving revision 1.41
diff -c -r1.41 UnknownElement.java
*** src/main/org/apache/tools/ant/UnknownElement.java   7 Mar 2003 11:22:59
-0000   1.41
--- src/main/org/apache/tools/ant/UnknownElement.java   27 Mar 2003 17:52:32
-0000
***************
*** 56,61 ****
--- 56,62 ----

 import java.util.Vector;
 import java.io.IOException;
+ import java.lang.reflect.Method;

 /**
  * Wrapper class that holds all the information necessary to create a task
***************
*** 259,264 ****
--- 260,266 ----
         throws BuildException {
         if (parent instanceof TaskAdapter) {
             parent = ((TaskAdapter) parent).getProxy();
+             setProjectOnObject(parent);
         }

         Class parentClass = parent.getClass();
***************
*** 277,282 ****
--- 279,285 ----
                     throw getNotFoundException("task", child.getTag());
                 }

+                 setProjectOnObject(realChild);
                 // XXX DataTypes will be wrapped or treated like normal
components
                 if (realChild instanceof Task) {
                     Task task = (Task) realChild;
***************
*** 291,298 ****
--- 294,304 ----
             } else {
                 realChild
                     = ih.createElement(getProject(), parent,
child.getTag());
+                 if (realChild != null)
+                     setProjectOnObject(realChild);
             }

+
             childWrapper.setProxy(realChild);
             if (parent instanceof TaskContainer
                 && realChild instanceof Task) {
***************
*** 433,437 ****
--- 439,464 ----
         }
         return null;
     }
+
+     /**
+      * set the project on a created object
+      * Need to set the project before other set/add elements
+      * are called
+      */
+     private void setProjectOnObject(Object obj) {
+         try {
+             Method method =
+                 obj.getClass().getMethod(
+                     "setProject", new Class[] {Project.class});
+             if (method != null) {
+                 method.invoke(obj, new Object[] {getProject()});
+             }
+         } catch (Throwable e) {
+             // ignore this if the object does not have
+             // a set project method or the method
+             // is private/protected.
+         }
+     }
+

 }// UnknownElement

On Monday 31 March 2003 18:40, Tim Gordon wrote:
> Hi
>
> I'm passing <param name="somedir" value="${some.relative.directory}">
> parameter into a custom selector, which is a subclass of
> BaseExtendSelector. ${some.relative.directory} is defined relative to the
> project ${basedir}, so inside my code I'll get a string value for the
> "somedir" parameter which is basically useless to me unless I can do
>
> new File(project.getBasedir(), somedir);
>
> However, setProject() never seems to get called on my custom selector, even
> though it's a ProjectComponent. Is this a bug in ANT BaseExtendSelector or
> AbstractFileSet, not setting the project on the component?
>
> I can acheive the effect I'm after by passing in <param name="somedir"
> value="${basedir}/${some.relative.directory}"> - do I have to live with
> that? I'd rather pass in paths defined relative to the project basedir,
> which is consistent with passing around directories in the rest of ANT.
>
> Tim Gordon
>
> Allustra Limited
> 1 Royal Exchange Avenue
> London
> EC3V 3LT
> Tel 020 7464 4190
> Tel 020 7464 4194
> http://www.allustra.com/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@(protected)
> For additional commands, e-mail: user-help@(protected)



©2008 ant-tasks.com - Jax Systems, LLC, U.S.A.