From 9bf6b2fb41704f3a3eadd4816bdff3c4ec24604e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 2 May 2022 09:28:15 -0400 Subject: [PATCH] Make Validate.isAssignableFrom() check null inputs. --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/Validate.java | 9 ++---- .../apache/commons/lang3/ValidateTest.java | 29 ++++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c50d8201c..4dba5e42b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -75,6 +75,7 @@ The type attribute can be add,update,fix,remove. Improve performance of StringUtils.unwrap(String, String) #844. Improve performance of StringUtils.join for primitives #812. Fixed NPE getting Stack Trace if Throwable is null #733. + Make Validate.isAssignableFrom() check null inputs. Add EnumUtils.getEnumSystemProperty(...). Add TriConsumer. diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index 06294abf7..9733a4739 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -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"))); } } diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index ff1a99a0e..8e970a851 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -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 {