From e9f3ece022854a7c415b6ce8435ca803dc8aee8b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 4 Mar 2023 09:04:47 -0500 Subject: Add ClassLoaderUtils.getSystemURLs() and getThreadURLs(). --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ClassLoaderUtils.java | 30 ++++++++++++++++++++-- .../apache/commons/lang3/ClassLoaderUtilsTest.java | 17 ++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 34e5c9aec..e12e39408 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -188,6 +188,7 @@ The type attribute can be add,update,fix,remove. Add CalendarUtils#getDayOfYear() #968 Add NumberRange, DoubleRange, IntegerRange, LongRange. Add missing exception javadoc/tests for some null arguments #869. + Add ClassLoaderUtils.getSystemURLs() and getThreadURLs(). Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964. Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963. diff --git a/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java b/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java index a87156a08..dda4828a2 100644 --- a/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java +++ b/src/main/java/org/apache/commons/lang3/ClassLoaderUtils.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3; +import java.net.URL; import java.net.URLClassLoader; import java.util.Arrays; @@ -27,6 +28,32 @@ import java.util.Arrays; */ public class ClassLoaderUtils { + private static final URL[] EMPTY_URL_ARRAY = new URL[] {}; + + /** + * Gets the system class loader's URLs, if any. + * + * @return the system class loader's URLs, if any. + * @since 3.13.0 + */ + public static URL[] getSystemURLs() { + return getURLs(ClassLoader.getSystemClassLoader()); + } + + /** + * Gets the current thread's context class loader's URLs, if any. + * + * @return the current thread's context class loader's URLs, if any. + * @since 3.13.0 + */ + public static URL[] getThreadURLs() { + return getURLs(Thread.currentThread().getContextClassLoader()); + } + + private static URL[] getURLs(final ClassLoader cl) { + return cl instanceof URLClassLoader ? ((URLClassLoader) cl).getURLs() : EMPTY_URL_ARRAY; + } + /** * Converts the given class loader to a String calling {@link #toString(URLClassLoader)}. * @@ -41,8 +68,7 @@ public class ClassLoaderUtils { } /** - * Converts the given URLClassLoader to a String in the format - * {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}. + * Converts the given URLClassLoader to a String in the format {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}. * * @param classLoader to URLClassLoader to convert. * @return the formatted string. diff --git a/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java index ea56f50c8..b51ed3635 100644 --- a/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ClassLoaderUtilsTest.java @@ -17,6 +17,8 @@ package org.apache.commons.lang3; +import static org.junit.jupiter.api.Assertions.assertNotNull; + import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; @@ -29,6 +31,18 @@ import org.junit.jupiter.api.Test; */ public class ClassLoaderUtilsTest extends AbstractLangTest { + @Test + public void testGetSystemURLs() { + // TODO How to better test considering this test may be called from an IDE and Maven? + assertNotNull(ClassLoaderUtils.getSystemURLs()); + } + + @Test + public void testGetThreadURLs() { + // TODO How to better test considering this test may be called from an IDE and Maven? + assertNotNull(ClassLoaderUtils.getThreadURLs()); + } + @Test public void testToString_ClassLoader() throws IOException { final URL url = new URL("http://localhost"); @@ -43,8 +57,7 @@ public class ClassLoaderUtilsTest extends AbstractLangTest { public void testToString_URLClassLoader() throws IOException { final URL url = new URL("http://localhost"); try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) { - Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url), - ClassLoaderUtils.toString(urlClassLoader)); + Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url), ClassLoaderUtils.toString(urlClassLoader)); } } } -- cgit v1.2.3