You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2021/07/14 02:48:05 UTC

[ant] branch master updated: Implement ClassLoader#findResource(String)

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new ef85f49  Implement ClassLoader#findResource(String)
     new 4c49cd5  Merge pull request #150 from basil/findResource
ef85f49 is described below

commit ef85f491ee3fa0152e46a708253bda1435b45a35
Author: Basil Crow <me...@basilcrow.com>
AuthorDate: Sun Jul 11 12:29:22 2021 -0700

    Implement ClassLoader#findResource(String)
---
 CONTRIBUTORS                                      |  1 +
 contributors.xml                                  |  4 ++
 src/main/org/apache/tools/ant/AntClassLoader.java | 45 ++++++++++++++++++-----
 3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 064e6ad..bc935a7 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -43,6 +43,7 @@ Avik Sengupta
 Balazs Fejes 2
 barney2k7
 Bart Vanhaute
+Basil Crow
 Ben Galbraith
 Ben Gertzfield
 Benjamin Burgess
diff --git a/contributors.xml b/contributors.xml
index cff2bf7..c84ec14 100644
--- a/contributors.xml
+++ b/contributors.xml
@@ -200,6 +200,10 @@
     <last>Vanhaute</last>
   </name>
   <name>
+    <first>Basil</first>
+    <last>Crow</last>
+  </name>
+  <name>
     <first>Benjamin</first>
     <last>Burgess</last>
   </name>
diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java
index 189351f..9791fb3 100644
--- a/src/main/org/apache/tools/ant/AntClassLoader.java
+++ b/src/main/org/apache/tools/ant/AntClassLoader.java
@@ -889,15 +889,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
         if (url != null) {
             log("Resource " + name + " loaded from parent loader", Project.MSG_DEBUG);
         } else {
-            // try and load from this loader if the parent either didn't find
-            // it or wasn't consulted.
-            for (final File pathComponent : pathComponents) {
-                url = getResourceURL(pathComponent, name);
-                if (url != null) {
-                    log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
-                    break;
-                }
-            }
+            url = getUrl(name);
         }
         if (url == null && !isParentFirst(name)) {
             // this loader was first but it didn't find it - try the parent
@@ -917,6 +909,29 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
     }
 
     /**
+     * Finds a matching file by iterating through path components.
+     *
+     * @param name File to find
+     * @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
+     *     resource could not be found
+     */
+    private URL getUrl(String name) {
+        URL url = null;
+
+        // try and load from this loader if the parent either didn't find
+        // it or wasn't consulted.
+        for (final File pathComponent : pathComponents) {
+            url = getResourceURL(pathComponent, name);
+            if (url != null) {
+                log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
+                break;
+            }
+        }
+
+        return url;
+    }
+
+    /**
      * Finds all the resources with the given name. A resource is some
      * data (images, audio, text, etc) that can be accessed by class
      * code in a way that is independent of the location of the code.
@@ -936,6 +951,18 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
     }
 
     /**
+     * Finds the resource with the given name.
+     *
+     * @param name The resource name
+     * @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
+     *     resource could not be found
+     */
+    @Override
+    protected URL findResource(final String name) {
+        return getUrl(name);
+    }
+
+    /**
      * Returns an enumeration of URLs representing all the resources with the
      * given name by searching the class loader's classpath.
      *