Skip to content

Commit

Permalink
Refine null-safety for additional Assert methods
Browse files Browse the repository at this point in the history
Closes gh-33530
  • Loading branch information
sdeleuze committed Sep 13, 2024
1 parent 5985800 commit 52f0e8f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions spring-core/src/main/java/org/springframework/util/Assert.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,6 +58,7 @@
* @author Sam Brannen
* @author Colin Sampaleanu
* @author Rob Harrop
* @author Sebastien Deleuze
* @since 1.1.2
*/
public abstract class Assert {
Expand Down Expand Up @@ -209,6 +210,7 @@ public static void notNull(@Nullable Object object, Supplier<String> messageSupp
* @throws IllegalArgumentException if the text is empty
* @see StringUtils#hasLength
*/
@Contract("null, _ -> fail")
public static void hasLength(@Nullable String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
Expand All @@ -229,6 +231,7 @@ public static void hasLength(@Nullable String text, String message) {
* @since 5.0
* @see StringUtils#hasLength
*/
@Contract("null, _ -> fail")
public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
Expand All @@ -244,6 +247,7 @@ public static void hasLength(@Nullable String text, Supplier<String> messageSupp
* @throws IllegalArgumentException if the text does not contain valid text content
* @see StringUtils#hasText
*/
@Contract("null, _ -> fail")
public static void hasText(@Nullable String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
Expand All @@ -264,6 +268,7 @@ public static void hasText(@Nullable String text, String message) {
* @since 5.0
* @see StringUtils#hasText
*/
@Contract("null, _ -> fail")
public static void hasText(@Nullable String text, Supplier<String> messageSupplier) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
Expand Down Expand Up @@ -387,6 +392,7 @@ public static void noNullElements(@Nullable Object[] array, Supplier<String> mes
* @throws IllegalArgumentException if the collection is {@code null} or
* contains no elements
*/
@Contract("null, _ -> fail")
public static void notEmpty(@Nullable Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
Expand All @@ -406,6 +412,7 @@ public static void notEmpty(@Nullable Collection<?> collection, String message)
* contains no elements
* @since 5.0
*/
@Contract("null, _ -> fail")
public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
Expand Down Expand Up @@ -461,6 +468,7 @@ public static void noNullElements(@Nullable Collection<?> collection, Supplier<S
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
*/
@Contract("null, _ -> fail")
public static void notEmpty(@Nullable Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
Expand All @@ -479,6 +487,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, String message) {
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
* @since 5.0
*/
@Contract("null, _ -> fail")
public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
Expand All @@ -497,6 +506,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSup
* of the offending object's type will be appended.
* @throws IllegalArgumentException if the object is not an instance of type
*/
@Contract("_, null, _ -> fail")
public static void isInstanceOf(Class<?> type, @Nullable Object obj, String message) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
Expand All @@ -516,6 +526,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, String mess
* @throws IllegalArgumentException if the object is not an instance of type
* @since 5.0
*/
@Contract("_, null, _ -> fail")
public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
Expand All @@ -530,6 +541,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<St
* @param obj the object to check
* @throws IllegalArgumentException if the object is not an instance of type
*/
@Contract("_, null -> fail")
public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
isInstanceOf(type, obj, "");
}
Expand All @@ -546,6 +558,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
* offending subtype will be appended.
* @throws IllegalArgumentException if the classes are not assignable
*/
@Contract("_, null, _ -> fail")
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, String message) {
notNull(superType, "Supertype to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
Expand All @@ -565,6 +578,7 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
* @throws IllegalArgumentException if the classes are not assignable
* @since 5.0
*/
@Contract("_, null, _ -> fail")
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, Supplier<String> messageSupplier) {
notNull(superType, "Supertype to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
Expand All @@ -579,7 +593,8 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
* @param subType the subtype to check
* @throws IllegalArgumentException if the classes are not assignable
*/
public static void isAssignable(Class<?> superType, Class<?> subType) {
@Contract("_, null -> fail")
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType) {
isAssignable(superType, subType, "");
}

Expand Down

0 comments on commit 52f0e8f

Please sign in to comment.