You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2009/09/28 17:00:34 UTC

svn commit: r819570 - /ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java

Author: bodewig
Date: Mon Sep 28 15:00:34 2009
New Revision: 819570

URL: http://svn.apache.org/viewvc?rev=819570&view=rev
Log:
create fresh instances in each invocation of getHelpers.  Needed by ProjectHelper implementations that keep state and cannot be reused, like ProjectHelper2

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java?rev=819570&r1=819569&r2=819570&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/ProjectHelperRepository.java Mon Sep 28 15:00:34 2009
@@ -4,6 +4,7 @@
 import java.io.File;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.lang.reflect.Constructor;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -14,10 +15,10 @@
 import org.apache.tools.ant.util.LoaderUtils;
 
 /**
- * Repository of {@link ProjectHelper} found in the classpath or via some System
- * properties.
- * <p>
- * See the ProjectHelper documentation in the manual.
+ * Repository of {@link ProjectHelper} found in the classpath or via
+ * some System properties.
+
+ * <p>See the ProjectHelper documentation in the manual.</p>
  * 
  * @since Ant 1.8.0
  */
@@ -34,7 +35,10 @@
     private static ProjectHelperRepository instance =
         new ProjectHelperRepository();
 
-    private List/* <ProjectHelper> */helpers = new ArrayList();
+    private List/* <Constructor> */ helpers = new ArrayList();
+
+    private static final Class[] NO_CLASS = new Class[0];
+    private static final Object[] NO_OBJECT = new Object[0];
 
     public static ProjectHelperRepository getInstance() {
         return instance;
@@ -91,9 +95,16 @@
         }
         if (DEBUG) {
             System.out.println("ProjectHelper " +
-                    projectHelper.getClass().getName() + " registered.");
+                               projectHelper.getClass().getName()
+                               + " registered.");
+        }
+        try {
+            helpers.add(projectHelper.getClass().getConstructor(NO_CLASS));
+        } catch (NoSuchMethodException nse) {
+            // impossible to get here
+            throw new BuildException("Couldn't find no-arg constructor in "
+                                     + projectHelper.getClass().getName());
         }
-        helpers.add(projectHelper);
     }
 
     private ProjectHelper getProjectHelperBySystemProperty() {
@@ -184,7 +195,7 @@
      * @return the first ProjectHelper that fit the requirement (never <code>null</code>).
      */
     public ProjectHelper getProjectHelper(File buildFile) throws BuildException {
-        Iterator it = helpers.iterator();
+        Iterator it = getHelpers();
         while (it.hasNext()) {
             ProjectHelper helper = (ProjectHelper) it.next();
             if (helper.supportsBuildFile(buildFile)) {
@@ -209,6 +220,32 @@
      * @return an iterator of {@link ProjectHelper}
      */
     public Iterator getHelpers() {
-        return helpers.iterator();
+        return new ConstructingIterator(helpers.iterator());
+    }
+
+    private static class ConstructingIterator implements Iterator {
+        private final Iterator nested;
+
+        ConstructingIterator(Iterator nested) {
+            this.nested = nested;
+        }
+
+        public boolean hasNext() {
+            return nested.hasNext();
+        }
+
+        public Object next() {
+            Constructor c = (Constructor) nested.next();
+            try {
+                return c.newInstance(NO_OBJECT);
+            } catch (Exception e) {
+                throw new BuildException("Failed to invoke no-arg constructor"
+                                         + " on " + c.getName());
+            }
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException("remove is not supported");
+        }
     }
 }