Refine null-safety for additional Assert methods
Closes gh-33530
This commit is contained in:
@@ -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.
|
||||
@@ -58,6 +58,7 @@ import org.springframework.lang.Nullable;
|
||||
* @author Sam Brannen
|
||||
* @author Colin Sampaleanu
|
||||
* @author Rob Harrop
|
||||
* @author Sebastien Deleuze
|
||||
* @since 1.1.2
|
||||
*/
|
||||
public abstract class Assert {
|
||||
@@ -209,6 +210,7 @@ public abstract class Assert {
|
||||
* @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);
|
||||
@@ -229,6 +231,7 @@ public abstract class Assert {
|
||||
* @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));
|
||||
@@ -244,6 +247,7 @@ public abstract class Assert {
|
||||
* @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);
|
||||
@@ -264,6 +268,7 @@ public abstract class Assert {
|
||||
* @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));
|
||||
@@ -387,6 +392,7 @@ public abstract class Assert {
|
||||
* @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);
|
||||
@@ -406,6 +412,7 @@ public abstract class Assert {
|
||||
* 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));
|
||||
@@ -461,6 +468,7 @@ public abstract class Assert {
|
||||
* @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);
|
||||
@@ -479,6 +487,7 @@ public abstract class Assert {
|
||||
* @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));
|
||||
@@ -497,6 +506,7 @@ public abstract class Assert {
|
||||
* 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)) {
|
||||
@@ -516,6 +526,7 @@ public abstract class Assert {
|
||||
* @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)) {
|
||||
@@ -530,6 +541,7 @@ public abstract class Assert {
|
||||
* @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, "");
|
||||
}
|
||||
@@ -546,6 +558,7 @@ public abstract class Assert {
|
||||
* 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)) {
|
||||
@@ -565,6 +578,7 @@ public abstract class Assert {
|
||||
* @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)) {
|
||||
@@ -579,7 +593,8 @@ public abstract class Assert {
|
||||
* @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, "");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user