Yup, its me again!  :)

The issue that Peter brought up with ant/antcall is being addressed in this
patch.  Property was modified to allow setting user properties without
warning, but that mode cannot be enabled from a <property> statement (I
removed the setUserProperty method as it was undocumented and probably only
used by ant/antcall under the covers).

Project had several calls to setProperty in it, which in theory could
generate a deprecated warning.  I added an internal method to allow setting
"basedir" and a couple of other places to set a property silently if it
wasn't a user property (setPropertyInternal, its protected, although private
would have been ok with me too).

I modified getProperties and getUserProperties to return copies of the
collections to prevent modifications of properties that way.

I updated WHATSNEW (maybe the <available> back door shouldn't be mentioned
there - maybe we should just close that open door :).

This should be yet another step forward in the battle against property
mutability.  Let me know if there are any problems with this patch or if
I've missed something.

Thanks,
    Erik

Index: src/main/org/apache/tools/ant/taskdefs/Ant.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Ant.java,v
retrieving revision 1.33
diff -u -r1.33 Ant.java
--- src/main/org/apache/tools/ant/taskdefs/Ant.java     2001/11/22 08:40:03     
1.33
+++ src/main/org/apache/tools/ant/taskdefs/Ant.java     2001/12/04 01:45:07
@@ -330,8 +330,9 @@
         if (newProject == null) {
             reinit();
         }
-        Property p=(Property)newProject.createTask("property");
-        p.setUserProperty(true);
+        Property p = new Property(true);
+        p.setProject(newProject);
+        p.setTaskName("property");
         properties.addElement( p );
         return p;
     }
Index: src/main/org/apache/tools/ant/taskdefs/Property.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Property.java,v
retrieving revision 1.36
diff -u -r1.36 Property.java
--- src/main/org/apache/tools/ant/taskdefs/Property.java        2001/12/01 
03:34:35     1.36
+++ src/main/org/apache/tools/ant/taskdefs/Property.java        2001/12/04 
01:45:08
@@ -88,7 +88,16 @@
     protected Reference ref = null;
 
     protected boolean userProperty=false; // set read-only properties
-
+    
+    public Property() {
+        super();
+    }
+    
+    public Property(boolean userProperty) {
+        this();
+        this.userProperty = userProperty;
+    }
+    
     public void setName(String name) {
         this.name = name;
     }
@@ -160,11 +169,8 @@
         createClasspath().setRefid(r);
     }
 
-    /**
-    * @deprecated
-    */
-    public void setUserProperty(boolean userProperty) {
-        this.userProperty = userProperty;
+    public boolean isUserProperty() {
+        return userProperty;
     }
 
     public String toString() {
@@ -287,12 +293,11 @@
 
     protected void addProperty(String n, String v) {
         if( userProperty ) {
-            log("DEPRECATED - Setting user properties through the Property 
task has been deprecated.");
             if (project.getUserProperty(n) == null) {
                 project.setUserProperty(n, v);
             } else {
                 log("Override ignored for " + n, Project.MSG_VERBOSE);
-            } 
+            }
         } else {
             project.setNewProperty(n, v);
         }
Index: src/main/org/apache/tools/ant/Project.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.82
diff -u -r1.82 Project.java
--- src/main/org/apache/tools/ant/Project.java  2001/12/01 03:31:40     1.82
+++ src/main/org/apache/tools/ant/Project.java  2001/12/04 01:45:11
@@ -159,7 +159,7 @@
     public Project() {
         fileUtils = FileUtils.newFileUtils();
     }
-
+    
     /**
      * Initialise the project.
      *
@@ -353,6 +353,18 @@
         userProperties.put(name, value);
         properties.put(name, value);
     }
+    
+    /**
+     * Allows Project and subclasses to set a property unless its
+     * already defined as a user property. There are a few cases 
+     * internally to Project that need to do this currently.
+     */
+    protected void setPropertyInternal(String name, String value) {
+        if (null != userProperties.get(name)) {
+            return;
+        }
+        properties.put(name, value);
+    }
 
     /**
      * query a property.
@@ -377,19 +389,37 @@
     }
 
     /**
-     * get the property hashtable
+     * get a copy of the property hashtable
      * @return the hashtable containing all properties, user included
      */
     public Hashtable getProperties() {
-        return properties;
+        Hashtable propertiesCopy = new Hashtable();
+        
+        Enumeration e = properties.keys();
+        while (e.hasMoreElements()) {
+            Object name = e.nextElement();
+            Object value = properties.get(name);
+            propertiesCopy.put(name, value);
+        }
+        
+        return propertiesCopy;
     }
 
     /**
-     * get the user property hashtable
+     * get a copy of the user property hashtable
      * @return the hashtable user properties only
      */
     public Hashtable getUserProperties() {
-        return userProperties;
+        Hashtable propertiesCopy = new Hashtable();
+        
+        Enumeration e = userProperties.keys();
+        while (e.hasMoreElements()) {
+            Object name = e.nextElement();
+            Object value = properties.get(name);
+            propertiesCopy.put(name, value);
+        }
+        
+        return propertiesCopy;
     }
 
     /**
@@ -486,7 +516,7 @@
         if (!baseDir.isDirectory()) 
             throw new BuildException("Basedir " + baseDir.getAbsolutePath() + 
" is not a directory");
         this.baseDir = baseDir;
-        setProperty( "basedir", this.baseDir.getPath());
+        setPropertyInternal( "basedir", this.baseDir.getPath());
         String msg = "Project base dir set to: " + this.baseDir;
         log(msg, MSG_VERBOSE);
     }
@@ -521,7 +551,7 @@
      * @throws BuildException if this Java version is not supported
      */
     public void setJavaVersionProperty() throws BuildException {
-        setProperty("ant.java.version", javaVersion);
+        setPropertyInternal("ant.java.version", javaVersion);
 
         // sanity check
         if (javaVersion == JAVA_1_0) {
@@ -543,7 +573,7 @@
         while (e.hasMoreElements()) {
             Object name = e.nextElement();
             String value = systemP.get(name).toString();
-            this.setProperty(name.toString(), value);
+            this.setPropertyInternal(name.toString(), value);
         }
     }
 
Index: WHATSNEW
===================================================================
RCS file: /home/cvspublic/jakarta-ant/WHATSNEW,v
retrieving revision 1.183
diff -u -r1.183 WHATSNEW
--- WHATSNEW    2001/11/30 21:09:03     1.183
+++ WHATSNEW    2001/12/04 01:45:13
@@ -17,7 +17,9 @@
   instead.
 
 * Some loopholes in the immutability rule have been closed. It is no longer
-  possible to overwrite a property using tasks like <available> or 
<condition>.  
+  possible to overwrite a property using tasks like <checksum>, <condition>,
+  <exec>, <pathconvert>, or <tstamp>.  <available>'s hole remains, although
+  a deprecation warning has been added if a property is overriden using it.
 
 Fixed bugs:
 -----------

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

Reply via email to