ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peterrei...@apache.org
Subject svn commit: r439801 - in /ant/core/trunk: ./ src/main/org/apache/tools/ant/ src/main/org/apache/tools/ant/types/selectors/modifiedselector/ src/main/org/apache/tools/ant/util/ src/testcases/org/apache/tools/ant/
Date Sun, 03 Sep 2006 16:45:01 GMT
Author: peterreilly
Date: Sun Sep  3 09:45:00 2006
New Revision: 439801

URL: http://svn.apache.org/viewvc?view=rev&rev=439801
Log:
Set the parent of the antClassLoader correctly: bugzilla 35436

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java
    ant/core/trunk/src/main/org/apache/tools/ant/Project.java
    ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunner.java
    ant/core/trunk/src/testcases/org/apache/tools/ant/AntClassLoaderTest.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Sun Sep  3 09:45:00 2006
@@ -5,6 +5,7 @@
 -------------------------------------------
 
 * <script> and <scriptdef> now set the current thread context.
+* AntClassLoader now sets the parent classloader. Bugzilla report 35436.
 
 Fixed bugs:
 -----------

Modified: ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java Sun Sep  3 09:45:00 2006
@@ -26,6 +26,7 @@
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collections;
@@ -59,6 +60,19 @@
 public class AntClassLoader extends ClassLoader implements SubBuildListener {
 
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+    /**
+     * Work around for deprecated constructors that did
+     * not set the parent classloader.
+     */
+    private static Field parentField;
+    static {
+        try {
+            parentField = ClassLoader.class.getDeclaredField("parent");
+            parentField.setAccessible(true);
+        } catch (Throwable t) {
+            // Ignore
+        }
+    }
 
     /**
      * An enumeration of all resources of a given name found within the
@@ -217,7 +231,30 @@
     private boolean isContextLoaderSaved = false;
 
     /**
+     * Create an Ant ClassLoader for a given project, with
+     * a parent classloader and an initial classpath.
+     * <p>
+     * This constructor has been added in ant 1.7, it
+     * sets the parent classloader correctly. All the
+     * other constructors are deprecated.
+     * </p>
+     * @since Ant 1.7.
+     * @param parent the parent for this classloader.
+     * @param project The project to which this classloader is to
+     *                belong.
+     * @param classpath The classpath to use to load classes.
+     */
+    public AntClassLoader(
+        ClassLoader parent, Project project, Path classpath) {
+        super(parent == null ? AntClassLoader.class.getClassLoader() : parent);
+        this.parent = getParent();
+        setClassPath(classpath);
+        setProject(project);
+    }
+
+    /**
      * Create an Ant Class Loader
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public AntClassLoader() {
         setParent(null);
@@ -233,6 +270,7 @@
      *                determined by the value of ${build.sysclasspath}.
      *                May be <code>null</code>, in which case no path
      *                elements are set up to start with.
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public AntClassLoader(Project project, Path classpath) {
         setParent(null);
@@ -255,6 +293,7 @@
      * @param parentFirst If <code>true</code>, indicates that the parent
      *                    classloader should be consulted  before trying to
      *                    load the a class through this loader.
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public AntClassLoader(ClassLoader parent, Project project, Path classpath,
                           boolean parentFirst) {
@@ -278,6 +317,7 @@
      * @param parentFirst If <code>true</code>, indicates that the parent
      *                    classloader should be consulted before trying to
      *                    load the a class through this loader.
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public AntClassLoader(Project project, Path classpath,
                           boolean parentFirst) {
@@ -296,6 +336,7 @@
      * @param parentFirst If <code>true</code>, indicates that the parent
      *                    classloader should be consulted before trying to
      *                    load the a class through this loader.
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public AntClassLoader(ClassLoader parent, boolean parentFirst) {
         setParent(parent);
@@ -307,6 +348,7 @@
      * Set the project associated with this class loader
      *
      * @param project the project instance
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
     public void setProject(Project project) {
         this.project = project;
@@ -343,12 +385,28 @@
      * this class loader will delegate to load classes
      *
      * @param parent the parent class loader.
+     * @deprecated by AntClassLoader(parent, project, classpath) since Ant 1.7.
      */
-    public void setParent(ClassLoader parent) {
+    private void setParent(ClassLoader parent) {
         if (parent == null) {
             this.parent = AntClassLoader.class.getClassLoader();
         } else {
             this.parent = parent;
+        }
+        // ClassLoader.parent is private and there is
+        // no accessor to set it, there is an accessor
+        // to get it, but it is final.
+        // This method setParent sets the parent of
+        // this classloader, and that is the way that the
+        // class behaves - so use a bit of reflection
+        // to set the field.
+        if (parentField == null) {
+            return; // Unable to get access to the parent field
+        }
+        try {
+            parentField.set(this, parent);
+        } catch (Throwable t) {
+            // Ignore - unable to set the parent
         }
     }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/Project.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/Project.java?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/Project.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/Project.java Sun Sep  3 09:45:00 2006
@@ -300,7 +300,8 @@
     }
 
     private void setAntLib() {
-        File antlib = org.apache.tools.ant.launch.Locator.getClassSource(Project.class);
+        File antlib = org.apache.tools.ant.launch.Locator.getClassSource(
+            Project.class);
         if (antlib != null) {
             setPropertyInternal(MagicNames.ANT_LIB, antlib.getAbsolutePath());
         }
@@ -314,10 +315,22 @@
      * @return an appropriate classloader.
      */
     public AntClassLoader createClassLoader(Path path) {
-        AntClassLoader loader = new AntClassLoader();
-        loader.setProject(this);
-        loader.setClassPath(path);
-        return loader;
+        return new AntClassLoader(
+            getClass().getClassLoader(), this, path);
+    }
+
+    /**
+     * Factory method to create a class loader for loading classes from
+     * a given path.
+     *
+     * @param parent the parent classloader for the new loader.
+     * @param path the path from which classes are to be loaded.
+     *
+     * @return an appropriate classloader.
+     */
+    public AntClassLoader createClassLoader(
+        ClassLoader parent, Path path) {
+        return new AntClassLoader(parent, this, path);
     }
 
     /**

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java
Sun Sep  3 09:45:00 2006
@@ -736,10 +736,10 @@
     public ClassLoader getClassLoader() {
         if (myClassLoader == null) {
             myClassLoader = (classpath == null)
-                          // the usual classloader
-                          ? getClass().getClassLoader()
-                          // additional use the provided classpath
-                          : new org.apache.tools.ant.AntClassLoader(getProject(), classpath);
+                // the usual classloader
+                ? getClass().getClassLoader()
+                // additional use the provided classpath
+                : getProject().createClassLoader(classpath);
         }
         return myClassLoader;
     }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunner.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunner.java?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunner.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/ScriptRunner.java Sun Sep  3 09:45:00
2006
@@ -124,9 +124,8 @@
             Thread.currentThread().getContextClassLoader();
         ClassLoader scriptLoader = getClass().getClassLoader();
         if (classpath != null && project != null) {
-            AntClassLoader loader = project.createClassLoader(classpath);
-            loader.setParent(scriptLoader);
-            scriptLoader = loader;
+            scriptLoader = project.createClassLoader(
+                scriptLoader, classpath);
         }
         try {
             Thread.currentThread().setContextClassLoader(scriptLoader);

Modified: ant/core/trunk/src/testcases/org/apache/tools/ant/AntClassLoaderTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/testcases/org/apache/tools/ant/AntClassLoaderTest.java?view=diff&rev=439801&r1=439800&r2=439801
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/AntClassLoaderTest.java (original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/AntClassLoaderTest.java Sun Sep  3 09:45:00
2006
@@ -66,7 +66,7 @@
     }
     public void testCleanup() throws BuildException {
         Path path = new Path(p, ".");
-        AntClassLoader loader = new AntClassLoader(p, path);
+        AntClassLoader loader = p.createClassLoader(path);
         try {
             // we don't expect to find this
             loader.findClass("fubar");



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message