View | Details | Raw Unified | Return to bug 40140
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/IntrospectionHelper.java (+73 lines)
Lines 279-284 Link Here
279
    }
279
    }
280
280
281
    /**
281
    /**
282
     * Returns a list containing the (US lowercased) names of the 
283
     * attributes for which property expansion should not take place.
284
     * 
285
     * <p>The attribute names are extracted from the name of the Methods
286
     * returned by the invocation of the bean's method 
287
     * <code>public java.lang.reflect.Method [] 
288
     * getUnexpandedAttrSetters()</code>, if defined.
289
     * The methods must start with <code>set</code>.</p>
290
     * 
291
     * <p>Ex.: (supposing the caller of this method is RuntimeConfigurable) 
292
     * if an element has method <code>setMyAttr(String str)</code>
293
     * and a method <code>getUnexpandedAttrSetters()</code> returning an array 
294
     * containing the method <code>setUnexAttr</code> and for an element
295
     * you define the attribute <code>unexattr=&quot;${path.separator}&quot;</code>, 
296
     * the method's <var>str</var> parameter will contain the unexpanded string,
297
     * that is &quot;${path.separator}&quot; instead of &quot;:&quot;.</p>
298
     * 
299
     * @param bean The element. Must not be <code>null</code>.
300
     * @return The list of attribute names for which
301
     *         property expansion shouldn't take place,
302
     *         or <code>null</code> if the bean
303
     *         doesn't define the list of corresponding methods
304
     *         or the list is empty.
305
     */
306
    protected List unexpandedAttributes(Object bean) {
307
        
308
        Method m = null;
309
        try {
310
            m = bean.getClass().getMethod("getUnexpandedAttrSetters",null);
311
        } catch (NoSuchMethodException nsme) {
312
            return null;
313
        }
314
315
        Class returnType = m.getReturnType();
316
317
        if ((returnType.getComponentType() != null)
318
                && Method.class.isAssignableFrom(
319
                    returnType.getComponentType())) {
320
            // it's the right method
321
            Object result = null;
322
            try {
323
                result = m.invoke(bean, new Object[] {});
324
            } catch (InvocationTargetException ite) {
325
            } catch (IllegalAccessException iae) {
326
            }
327
            if (result != null) {
328
                Method [] ma = (Method []) result;
329
                if (ma.length > 0) {
330
                    List names = null;
331
                    int curM = 0;
332
                    while (curM < ma.length) {
333
                        if (ma[curM] != null) {
334
                            String mname = ma[curM].getName();
335
                            if (mname.startsWith("set")) {
336
                                String aname = getPropertyName(
337
                                        mname,"set");
338
                                if (names == null) {
339
                                    names = new ArrayList();
340
                                }
341
                                names.add(aname);
342
                            }
343
                        }
344
                        curM++;
345
                    }
346
                    return names;
347
                }
348
            }
349
        }
350
        
351
        return null;
352
    }
353
354
    /**
282
     * Certain set methods are part of the Ant core interface to tasks and
355
     * Certain set methods are part of the Ant core interface to tasks and
283
     * therefore not to be considered for introspection
356
     * therefore not to be considered for introspection
284
     *
357
     *
(-)src/main/org/apache/tools/ant/RuntimeConfigurable.java (-1 / +6 lines)
Lines 357-369 Link Here
357
        IntrospectionHelper ih =
357
        IntrospectionHelper ih =
358
            IntrospectionHelper.getHelper(p, target.getClass());
358
            IntrospectionHelper.getHelper(p, target.getClass());
359
359
360
        List unexpandedAttrs = ih.unexpandedAttributes(target);
361
            
360
        if (attributeNames != null) {
362
        if (attributeNames != null) {
361
            for (int i = 0; i < attributeNames.size(); i++) {
363
            for (int i = 0; i < attributeNames.size(); i++) {
362
                String name = (String) attributeNames.get(i);
364
                String name = (String) attributeNames.get(i);
363
                String value = (String) attributeMap.get(name);
365
                String value = (String) attributeMap.get(name);
364
366
365
                // reflect these into the target
367
                // reflect these into the target
366
                value = p.replaceProperties(value);
368
                if ((unexpandedAttrs == null) || !unexpandedAttrs.contains(
369
                        name.toLowerCase(Locale.US))) {
370
                    value = p.replaceProperties(value);
371
                }
367
                try {
372
                try {
368
                    ih.setAttribute(p, target, name, value);
373
                    ih.setAttribute(p, target, name, value);
369
                } catch (UnsupportedAttributeException be) {
374
                } catch (UnsupportedAttributeException be) {
(-)src/main/org/apache/tools/ant/ProjectHelper.java (-4 / +10 lines)
Lines 22-27 Link Here
22
import java.io.InputStream;
22
import java.io.InputStream;
23
import java.io.InputStreamReader;
23
import java.io.InputStreamReader;
24
import java.util.Hashtable;
24
import java.util.Hashtable;
25
import java.util.List;
25
import java.util.Locale;
26
import java.util.Locale;
26
import java.util.Vector;
27
import java.util.Vector;
27
import org.apache.tools.ant.helper.ProjectHelper2;
28
import org.apache.tools.ant.helper.ProjectHelper2;
Lines 307-319 Link Here
307
        IntrospectionHelper ih =
308
        IntrospectionHelper ih =
308
            IntrospectionHelper.getHelper(project, target.getClass());
309
            IntrospectionHelper.getHelper(project, target.getClass());
309
310
311
        List unexpandedAttrs = ih.unexpandedAttributes(target);
312
310
        for (int i = 0; i < attrs.getLength(); i++) {
313
        for (int i = 0; i < attrs.getLength(); i++) {
311
            // reflect these into the target
314
            // reflect these into the target
312
            String value = replaceProperties(project, attrs.getValue(i),
315
            String name = attrs.getName(i).toLowerCase(Locale.US);
313
                                             project.getProperties());
316
            String value = attrs.getValue(i);
317
            if ((unexpandedAttrs == null) 
318
                    || !unexpandedAttrs.contains(name)) {
319
                value = project.replaceProperties(value);
320
            }
314
            try {
321
            try {
315
                ih.setAttribute(project, target,
322
                ih.setAttribute(project, target, name, value);
316
                                attrs.getName(i).toLowerCase(Locale.US), value);
317
323
318
            } catch (BuildException be) {
324
            } catch (BuildException be) {
319
                // id attribute must be set externally
325
                // id attribute must be set externally
(-)src/etc/testcases/core/customtasks.xml (+25 lines)
Line 0 Link Here
1
<project name="customtasks-test" basedir=".">
2
3
    <property name="tmp.dir" location="tmp"/>
4
5
    <target name="unexpandedsetup">
6
      <typedef name="notexpandeda"
7
          classname="org.apache.tools.ant.CustomTasksTest$NotExpandedATask"
8
          classpath="${build.tests.value}"/>
9
    </target>
10
11
    <property name="foo" value="bar"/>
12
13
    <target name="unexpandedattr" depends="unexpandedsetup">
14
      <notexpandeda prefix="prop1" expstring="${foo}" unexpstring="${foo}"/>
15
      <fail>
16
        <condition>
17
          <not><and>
18
            <equals arg1="${prop1.exp}" arg2="bar"/>
19
            <equals arg1="${prop1.unexp}" arg2="$${foo}"/>
20
          </and></not>
21
        </condition>
22
      </fail>
23
    </target>
24
25
</project>
(-)src/testcases/org/apache/tools/ant/CustomTasksTest.java (+78 lines)
Line 0 Link Here
1
/*
2
 * Copyright 2006 The Apache Software Foundation
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17
18
package org.apache.tools.ant;
19
20
import org.apache.tools.ant.BuildFileTest;
21
import java.lang.reflect.Method;
22
23
/**
24
 * 
25
 */
26
public class CustomTasksTest extends BuildFileTest {
27
28
    public CustomTasksTest(String name) {
29
        super(name);
30
    }
31
32
    public void setUp() {
33
        configureProject("src/etc/testcases/core/customtasks.xml");
34
    }
35
36
    public static class NotExpandedATask extends Task {
37
    	private String expString = null;
38
    	private String unexpString = null;
39
    	private String prefix = null;
40
41
    	public void setExpString(String expString) {
42
    		this.expString = expString;
43
    	}
44
45
    	public void setUnexpString(String unexpString) {
46
    		this.unexpString = unexpString;
47
    	}
48
49
    	public void setPrefix(String prefix) {
50
    		this.prefix = prefix;
51
    	}
52
53
    	public Method [] getUnexpandedAttrSetters() {
54
            Method method = null;
55
            try {
56
                method = getClass().getMethod("setUnexpString", 
57
                		new Class[] {String.class});
58
            } catch (NoSuchMethodException nsme) {
59
                throw new BuildException(nsme);
60
            }
61
    		return new Method[] {method};
62
    	}
63
64
    	public void execute() throws BuildException {
65
    		if ((prefix == null) || (expString == null) || (unexpString == null)) {
66
    			throw new BuildException("Bad test initialisation");
67
    		}
68
    		getProject().setNewProperty(prefix + ".exp", expString);
69
    		getProject().setNewProperty(prefix + ".unexp", unexpString);
70
    	}
71
    }
72
    /**
73
     */
74
    public void testUnexpandedAttr() {
75
        executeTarget("unexpandedattr");
76
    }
77
}
78

Return to bug 40140