Make Validate.isAssignableFrom() check null inputs.

This commit is contained in:
Gary Gregory 2022-05-02 09:28:15 -04:00
parent 75a6aac07e
commit 9bf6b2fb41
3 changed files with 32 additions and 7 deletions

View File

@ -75,6 +75,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1679" type="fix" dev="ggregory" due-to="clover">Improve performance of StringUtils.unwrap(String, String) #844.</action>
<action issue="LANG-1675" type="fix" dev="ggregory" due-to="clover">Improve performance of StringUtils.join for primitives #812.</action>
<action issue="LANG-1675" type="fix" dev="ggregory" due-to="Arturo Bernal">Fixed NPE getting Stack Trace if Throwable is null #733.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory, Arturo Bernal">Make Validate.isAssignableFrom() check null inputs.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>

View File

@ -1278,9 +1278,6 @@ public class Validate {
}
}
// isAssignableFrom
//---------------------------------------------------------------------------------
/**
* Validates that the argument can be converted to the specified class, if not, throws an exception.
*
@ -1299,9 +1296,9 @@ public class Validate {
*/
public static void isAssignableFrom(final Class<?> superType, final Class<?> type) {
// TODO when breaking BC, consider returning type
if (!superType.isAssignableFrom(type)) {
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, type == null ? "null" : type.getName(),
superType.getName()));
if (type == null || superType == null || !superType.isAssignableFrom(type)) {
throw new IllegalArgumentException(
String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, ClassUtils.getName(type, "null type"), ClassUtils.getName(superType, "null type")));
}
}

View File

@ -1849,7 +1849,34 @@ class ValidateTest {
assertEquals("Cannot assign a java.lang.String to a java.util.List", ex.getMessage());
}
}
@Test
void shouldThrowIllegalArgumentExceptionWithNullSuperType() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isAssignableFrom(null, String.class));
assertEquals("Cannot assign a java.lang.String to a null type", ex.getMessage());
}
@Test
void shouldThrowIllegalArgumentExceptionWithNullType() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isAssignableFrom(List.class, null));
assertEquals("Cannot assign a null type to a java.util.List", ex.getMessage());
}
@Test
void shouldThrowIllegalArgumentExceptionWithNullTypes() {
final IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> Validate.isAssignableFrom(null, null));
assertEquals("Cannot assign a null type to a null type", ex.getMessage());
}
}
@Nested
class WithMessage {