source: release-kits/lirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/ForTask.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 10.1 KB
Line 
1/*
2* The Apache Software License, Version 1.1
3*
4* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
5* reserved.
6*
7* Redistribution and use in source and binary forms, with or without
8* modification, are permitted provided that the following conditions
9* are met:
10*
11* 1. Redistributions of source code must retain the above copyright
12* notice, this list of conditions and the following disclaimer.
13*
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in
16* the documentation and/or other materials provided with the
17* distribution.
18*
19* 3. The end-user documentation included with the redistribution, if
20* any, must include the following acknowlegement:
21* "This product includes software developed by the
22* Apache Software Foundation (http://www.apache.org/)."
23* Alternately, this acknowlegement may appear in the software itself,
24* if and wherever such third-party acknowlegements normally appear.
25*
26* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27* Foundation" must not be used to endorse or promote products derived
28* from this software without prior written permission. For written
29* permission, please contact [email protected].
30*
31* 5. Products derived from this software may not be called "Apache"
32* nor may "Apache" appear in their names without prior written
33* permission of the Apache Group.
34*
35* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46* SUCH DAMAGE.
47* ====================================================================
48*
49* This software consists of voluntary contributions made by many
50* individuals on behalf of the Apache Software Foundation. For more
51* information on the Apache Software Foundation, please see
52* <http://www.apache.org/>.
53*/
54package ise.antelope.tasks;
55
56import java.lang.reflect.Field;
57import java.lang.reflect.Method;
58import java.util.Enumeration;
59import java.util.Vector;
60import org.apache.tools.ant.TaskAdapter;
61import org.apache.tools.ant.IntrospectionHelper;
62import org.apache.tools.ant.BuildException;
63import org.apache.tools.ant.RuntimeConfigurable;
64import org.apache.tools.ant.Target;
65import org.apache.tools.ant.Task;
66import org.apache.tools.ant.TaskContainer;
67import org.apache.tools.ant.UnknownElement;
68
69/**
70 * @author Dale Anson, [email protected]
71 * @version $Revision: 1.3 $
72 * @since Ant 1.5
73 * @ant.task category="control"
74 */
75public class ForTask extends Task implements TaskContainer {
76
77 /** Vector to hold the nested tasks */
78 private Vector tasks = new Vector();
79 private String initName = null;
80 private int initValue = 0;
81 private int maxValue = 1;
82 private int inc = 1;
83
84 /**
85 * Sets the initName attribute of the ForTask object
86 *
87 * @param name The new initName value
88 */
89 public void setInitName( String name ) {
90 initName = name;
91 }
92
93 /**
94 * Sets the initvalue attribute of the ForTask object
95 *
96 * @param i The new initvalue value
97 */
98 public void setInitvalue( int i ) {
99 initValue = i;
100 }
101
102 /**
103 * Sets the maxvalue attribute of the ForTask object
104 *
105 * @param i The new maxvalue value
106 */
107 public void setMaxvalue( int i ) {
108 maxValue = i;
109 }
110
111 /**
112 * Sets the inc attribute of the ForTask object
113 *
114 * @param i The new inc value
115 */
116 public void setInc( int i ) {
117 inc = i;
118 }
119
120
121 /**
122 * Add a nested task
123 *
124 * @param task Nested task
125 */
126 public void addTask( Task task ) {
127 //task.maybeConfigure();
128 tasks.addElement( task );
129 }
130
131 public void maybeConfigure() {
132 if ( isInvalid() ) {
133 super.maybeConfigure();
134 }
135 else {
136 getRuntimeConfigurableWrapper().maybeConfigure( getProject(), false );
137 }
138 }
139
140 /**
141 * Execute all tasks.
142 *
143 * @exception BuildException Description of Exception
144 */
145 public void execute() throws BuildException {
146 for ( int i = initValue; i < maxValue; i += inc ) {
147 if ( initName != null ) {
148 getProject().setUserProperty( initName, String.valueOf( i ) );
149 }
150 Target target = new Target();
151 target.setName( "for.subtarget" );
152 getProject().addOrReplaceTarget( target );
153 for ( Enumeration e = tasks.elements(); e.hasMoreElements(); ) {
154 Task task = (Task)e.nextElement();
155 addTaskToTarget( target, task );
156 }
157
158 target.execute();
159 }
160 }
161
162 private void addTaskToTarget( Target target, Task task ) {
163 UnknownElement replacement = new UnknownElement( taskType ); // shouldn't do taskType, for Ant 1.6 and later there is a getTaskType method
164 replacement.setProject( getProject() );
165 invokeMethod( replacement, "setTaskType", taskType );
166 replacement.setTaskName( task.getTaskName() );
167 replacement.setLocation( task.getLocation() );
168 replacement.setOwningTarget( target );
169 replacement.setRuntimeConfigurableWrapper( task.getRuntimeConfigurableWrapper() );
170 invokeMethod( task.getRuntimeConfigurableWrapper(), "setProxy", replacement );
171 replacement.maybeConfigure();
172 log("replacement is a " + replacement.getTaskName() + ", " + replacement.getClass().getName());
173 if (replacement instanceof TaskContainer) {
174 log("replacement is a TaskContainer");
175 invokeMethod(replacement, "handleChildren", new Object[]{this, this.getRuntimeConfigurableWrapper()});
176 }
177 target.addTask(replacement);
178 }
179
180 /**
181 * Calls a method on the given object instance with the given argument.
182 *
183 * @param instance the object instance
184 * @param methodName the name of the method to invoke
185 * @param arg the argument to pass to the method
186 * @return Description of the Returned Value
187 * @see PrivilegedAccessor#invokeMethod(Object,String,Object[])
188 */
189 private Object invokeMethod( Object instance, String methodName, Object arg ) {
190 try {
191 Object[] args = new Object[1];
192 args[0] = arg;
193 return invokeMethod( instance, methodName, args );
194 }
195 catch ( Exception e ) {
196 e.printStackTrace();
197 return null;
198 }
199 }
200
201 /**
202 * Calls a method on the given object instance with the given arguments.
203 *
204 * @param instance the object instance
205 * @param methodName the name of the method to invoke
206 * @param args an array of objects to pass as arguments
207 * @return Description of the Returned Value
208 * @see PrivilegedAccessor#invokeMethod(Object,String,Object)
209 */
210 private Object invokeMethod( Object instance, String methodName, Object[] args ) {
211 try {
212 Class[] classTypes = null;
213 if ( args != null ) {
214 classTypes = new Class[args.length];
215 for ( int i = 0; i < args.length; i++ ) {
216 if ( args[i] != null )
217 classTypes[i] = args[i].getClass();
218 }
219 }
220 return getMethod( instance, methodName, classTypes ).invoke( instance, args );
221 }
222 catch ( Exception e ) {
223 return null;
224 }
225 }
226
227 /**
228 * @param instance the object instance
229 * @param methodName the
230 * @param classTypes
231 * @return The method value
232 */
233 private Method getMethod( Object instance, String methodName, Class[] classTypes ) {
234 try {
235 Method accessMethod = getMethod( instance.getClass(), methodName, classTypes );
236 accessMethod.setAccessible( true );
237 return accessMethod;
238 }
239 catch ( Exception e ) {
240 return null;
241 }
242 }
243
244 /**
245 * Return the named method with a method signature matching classTypes from
246 * the given class.
247 *
248 * @param thisClass
249 * @param methodName
250 * @param classTypes
251 * @return The method value
252 * @exception NoSuchMethodException Description of Exception
253 */
254 private Method getMethod( Class thisClass, String methodName, Class[] classTypes ) throws NoSuchMethodException {
255 if ( thisClass == null )
256 throw new NoSuchMethodException( "Invalid method : " + methodName );
257 try {
258 return thisClass.getDeclaredMethod( methodName, classTypes );
259 }
260 catch ( NoSuchMethodException e ) {
261 return getMethod( thisClass.getSuperclass(), methodName, classTypes );
262 }
263 }
264
265 /**
266 * Gets the value of the named field and returns it as an object.
267 *
268 * @param instance the object instance
269 * @param fieldName the name of the field
270 * @return an object representing the value of the field
271 */
272 private Object getValue( Object instance, String fieldName ) {
273 try {
274 Field field = getField( instance.getClass(), fieldName );
275 field.setAccessible( true );
276 return field.get( instance );
277 }
278 catch ( Exception e ) {
279 return null;
280 }
281 }
282
283 /**
284 * Return the named field from the given class.
285 *
286 * @param thisClass
287 * @param fieldName
288 * @return The field value
289 */
290 private Field getField( Class thisClass, String fieldName ) {
291 try {
292 if ( thisClass == null )
293 throw new NoSuchFieldException( "Invalid field : " + fieldName );
294 try {
295 return thisClass.getDeclaredField( fieldName );
296 }
297 catch ( NoSuchFieldException e ) {
298 return getField( thisClass.getSuperclass(), fieldName );
299 }
300 }
301 catch ( Exception e ) {
302 return null;
303 }
304 }
305}
306
Note: See TracBrowser for help on using the repository browser.