You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/03/04 14:05:24 UTC
[commons-lang] branch master updated: Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new e9f3ece02 Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().
new 2cf648619 Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-lang.git
e9f3ece02 is described below
commit e9f3ece022854a7c415b6ce8435ca803dc8aee8b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Mar 4 09:04:47 2023 -0500
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 <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Arturo Bernal">Add CalendarUtils#getDayOfYear() #968</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add NumberRange, DoubleRange, IntegerRange, LongRange.</action>
<action type="add" dev="ggregory" due-to="Diego Marcilio, Bruno P. Kinoshita, Gary Gregory">Add missing exception javadoc/tests for some null arguments #869.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.10 #742, #752, #764, #833, #867, #959, #964.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963.</action>
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));
}
}
}