Add ClassLoaderUtils.getSystemURLs() and getThreadURLs().
This commit is contained in:
parent
8ba794181d
commit
e9f3ece022
|
@ -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="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="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="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 -->
|
<!-- 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, 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>
|
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.1.0 #819, #825, #859, #963.</action>
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@ -27,6 +28,32 @@ import java.util.Arrays;
|
||||||
*/
|
*/
|
||||||
public class ClassLoaderUtils {
|
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)}.
|
* 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
|
* Converts the given URLClassLoader to a String in the format {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}.
|
||||||
* {@code "URLClassLoader.toString() + [URL1, URL2, ...]"}.
|
|
||||||
*
|
*
|
||||||
* @param classLoader to URLClassLoader to convert.
|
* @param classLoader to URLClassLoader to convert.
|
||||||
* @return the formatted string.
|
* @return the formatted string.
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
package org.apache.commons.lang3;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
@ -29,6 +31,18 @@ import org.junit.jupiter.api.Test;
|
||||||
*/
|
*/
|
||||||
public class ClassLoaderUtilsTest extends AbstractLangTest {
|
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
|
@Test
|
||||||
public void testToString_ClassLoader() throws IOException {
|
public void testToString_ClassLoader() throws IOException {
|
||||||
final URL url = new URL("http://localhost");
|
final URL url = new URL("http://localhost");
|
||||||
|
@ -43,8 +57,7 @@ public class ClassLoaderUtilsTest extends AbstractLangTest {
|
||||||
public void testToString_URLClassLoader() throws IOException {
|
public void testToString_URLClassLoader() throws IOException {
|
||||||
final URL url = new URL("http://localhost");
|
final URL url = new URL("http://localhost");
|
||||||
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) {
|
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) {
|
||||||
Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url),
|
Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url), ClassLoaderUtils.toString(urlClassLoader));
|
||||||
ClassLoaderUtils.toString(urlClassLoader));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue