Return "null"" instead of NPE in

ClassLoaderUtils.toString(URLClassLoader)

Return "null"" instead of NPE in ClassLoaderUtils.toString(ClassLoader)
This commit is contained in:
Gary Gregory 2023-09-20 09:59:16 -04:00
parent 4ab7add381
commit fd6fbe280d
3 changed files with 8 additions and 2 deletions

View File

@ -56,6 +56,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="hduelme">Improve performance of StringUtils.isMixedCase() #1096.</action>
<action issue="LANG-1706" type="fix" dev="ggregory" due-to="Alberto Fernández">ThreadUtils find methods should not return null items #1098.</action>
<action issue="LANG-1710" type="fix" dev="ggregory" due-to="Shashank Sharma, Gary Gregory, Oksana">ReflectionToStringBuilder changes in version 3.13.0 has broken the logic for overriding classes.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Return "null"" instead of NPE in ClassLoaderUtils.toString(ClassLoader).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Return "null"" instead of NPE in ClassLoaderUtils.toString(URLClassLoader).</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add Functions#function(Function).</action>
<action type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add FailableFunction#function(FailableFunction).</action>

View File

@ -20,6 +20,7 @@ package org.apache.commons.lang3;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Objects;
/**
* Helps work with {@link ClassLoader}.
@ -64,7 +65,7 @@ public class ClassLoaderUtils {
if (classLoader instanceof URLClassLoader) {
return toString((URLClassLoader) classLoader);
}
return classLoader.toString();
return Objects.toString(classLoader);
}
/**
@ -74,6 +75,6 @@ public class ClassLoaderUtils {
* @return the formatted string.
*/
public static String toString(final URLClassLoader classLoader) {
return classLoader + Arrays.toString(classLoader.getURLs());
return classLoader != null ? classLoader + Arrays.toString(classLoader.getURLs()) : "null";
}
}

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.IOException;
@ -51,6 +52,7 @@ public class ClassLoaderUtilsTest extends AbstractLangTest {
final ClassLoader classLoader = urlClassLoader;
Assertions.assertEquals(String.format("%s[%s]", classLoader, url), ClassLoaderUtils.toString(classLoader));
}
assertEquals("null", ClassLoaderUtils.toString((ClassLoader) null));
}
@Test
@ -59,5 +61,6 @@ public class ClassLoaderUtilsTest extends AbstractLangTest {
try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { url })) {
Assertions.assertEquals(String.format("%s[%s]", urlClassLoader, url), ClassLoaderUtils.toString(urlClassLoader));
}
assertEquals("null", ClassLoaderUtils.toString((URLClassLoader) null));
}
}