Browse Source

enforce property immutability when resolving properties read as a map

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@915863 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
9caa9ff440
3 changed files with 39 additions and 1 deletions
  1. +26
    -0
      src/main/org/apache/tools/ant/property/ResolvePropertyMap.java
  2. +2
    -1
      src/main/org/apache/tools/ant/taskdefs/Property.java
  3. +11
    -0
      src/tests/antunit/taskdefs/property-test.xml

+ 26
- 0
src/main/org/apache/tools/ant/property/ResolvePropertyMap.java View File

@@ -35,6 +35,7 @@ public class ResolvePropertyMap implements GetProperty {
private final ParseProperties parseProperties; private final ParseProperties parseProperties;
private final GetProperty master; private final GetProperty master;
private Map map; private Map map;
private String prefix;


/** /**
* Constructor with a master getproperty and a collection of expanders. * Constructor with a master getproperty and a collection of expanders.
@@ -57,6 +58,19 @@ public class ResolvePropertyMap implements GetProperty {
throw new BuildException( throw new BuildException(
"Property " + name + " was circularly " + "defined."); "Property " + name + " was circularly " + "defined.");
} }

// if the property has already been set to the name it will
// have in the end, then return the existing value to ensure
// properties remain immutable
String masterPropertyName = name;
if (prefix != null) {
masterPropertyName = prefix + name;
}
Object masterProperty = master.getProperty(masterPropertyName);
if (masterProperty != null) {
return masterProperty;
}

try { try {
seen.add(name); seen.add(name);
return parseProperties.parseProperties((String) map.get(name)); return parseProperties.parseProperties((String) map.get(name));
@@ -68,9 +82,21 @@ public class ResolvePropertyMap implements GetProperty {
/** /**
* The action method - resolves all the properties in a map. * The action method - resolves all the properties in a map.
* @param map the map to resolve properties in. * @param map the map to resolve properties in.
* @deprecated since Ant 1.8.1, use the two-arg method instead.
*/ */
public void resolveAllProperties(Map map) { public void resolveAllProperties(Map map) {
resolveAllProperties(map, null);
}

/**
* The action method - resolves all the properties in a map.
* @param map the map to resolve properties in.
* @param prefix the prefix the properties defined inside the map
* will finally receive - may be null.
*/
public void resolveAllProperties(Map map, String prefix) {
this.map = map; // The map gets used in the getProperty callback this.map = map; // The map gets used in the getProperty callback
this.prefix = prefix;
for (Iterator i = map.keySet().iterator(); i.hasNext();) { for (Iterator i = map.keySet().iterator(); i.hasNext();) {
String key = (String) i.next(); String key = (String) i.next();
Object result = getProperty(key); Object result = getProperty(key);


+ 2
- 1
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -715,7 +715,8 @@ public class Property extends Task {
new ResolvePropertyMap( new ResolvePropertyMap(
getProject(), getProject(),
propertyHelper, propertyHelper,
propertyHelper.getExpanders()).resolveAllProperties(props);
propertyHelper.getExpanders())
.resolveAllProperties(props, prefix);
} }


} }

+ 11
- 0
src/tests/antunit/taskdefs/property-test.xml View File

@@ -77,4 +77,15 @@
<property name="foo" location="${testfile}" relative="true" basedir=".."/> <property name="foo" location="${testfile}" relative="true" basedir=".."/>
<au:assertPropertyEquals name="foo" value="taskdefs${file.separator}${testfile}"/> <au:assertPropertyEquals name="foo" value="taskdefs${file.separator}${testfile}"/>
</target> </target>

<target name="testNestedExpansionHonorsImmutability">
<mkdir dir="${input}"/>
<property name="x" value="x"/>
<echo file="${input}/x.properties"><![CDATA[
x=y
y=$${x}
]]></echo>
<property file="${input}/x.properties"/>
<au:assertPropertyEquals name="y" value="x"/>
</target>
</project> </project>

Loading…
Cancel
Save