From ef85f491ee3fa0152e46a708253bda1435b45a35 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sun, 11 Jul 2021 12:29:22 -0700 Subject: [PATCH] Implement ClassLoader#findResource(String) --- CONTRIBUTORS | 1 + contributors.xml | 4 ++ .../org/apache/tools/ant/AntClassLoader.java | 45 +++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 064e6ad34..bc935a7c2 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 cff2bf718..c84ec148b 100644 --- a/contributors.xml +++ b/contributors.xml @@ -199,6 +199,10 @@ Bart Vanhaute + + Basil + Crow + Benjamin Burgess diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index 189351f24..9791fb327 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 @@ -916,6 +908,29 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo return url; } + /** + * Finds a matching file by iterating through path components. + * + * @param name File to find + * @return A URL object for reading the resource, or null 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 @@ -935,6 +950,18 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo return findResources(name, false); } + /** + * Finds the resource with the given name. + * + * @param name The resource name + * @return A URL object for reading the resource, or null 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.