Skip to content

Commit 52f0e8f

Browse files
committed
Refine null-safety for additional Assert methods
Closes gh-33530
1 parent 5985800 commit 52f0e8f

File tree

1 file changed

+17
-2
lines changed
  • spring-core/src/main/java/org/springframework/util

1 file changed

+17
-2
lines changed

spring-core/src/main/java/org/springframework/util/Assert.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@
5858
* @author Sam Brannen
5959
* @author Colin Sampaleanu
6060
* @author Rob Harrop
61+
* @author Sebastien Deleuze
6162
* @since 1.1.2
6263
*/
6364
public abstract class Assert {
@@ -209,6 +210,7 @@ public static void notNull(@Nullable Object object, Supplier<String> messageSupp
209210
* @throws IllegalArgumentException if the text is empty
210211
* @see StringUtils#hasLength
211212
*/
213+
@Contract("null, _ -> fail")
212214
public static void hasLength(@Nullable String text, String message) {
213215
if (!StringUtils.hasLength(text)) {
214216
throw new IllegalArgumentException(message);
@@ -229,6 +231,7 @@ public static void hasLength(@Nullable String text, String message) {
229231
* @since 5.0
230232
* @see StringUtils#hasLength
231233
*/
234+
@Contract("null, _ -> fail")
232235
public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
233236
if (!StringUtils.hasLength(text)) {
234237
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
@@ -244,6 +247,7 @@ public static void hasLength(@Nullable String text, Supplier<String> messageSupp
244247
* @throws IllegalArgumentException if the text does not contain valid text content
245248
* @see StringUtils#hasText
246249
*/
250+
@Contract("null, _ -> fail")
247251
public static void hasText(@Nullable String text, String message) {
248252
if (!StringUtils.hasText(text)) {
249253
throw new IllegalArgumentException(message);
@@ -264,6 +268,7 @@ public static void hasText(@Nullable String text, String message) {
264268
* @since 5.0
265269
* @see StringUtils#hasText
266270
*/
271+
@Contract("null, _ -> fail")
267272
public static void hasText(@Nullable String text, Supplier<String> messageSupplier) {
268273
if (!StringUtils.hasText(text)) {
269274
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
@@ -387,6 +392,7 @@ public static void noNullElements(@Nullable Object[] array, Supplier<String> mes
387392
* @throws IllegalArgumentException if the collection is {@code null} or
388393
* contains no elements
389394
*/
395+
@Contract("null, _ -> fail")
390396
public static void notEmpty(@Nullable Collection<?> collection, String message) {
391397
if (CollectionUtils.isEmpty(collection)) {
392398
throw new IllegalArgumentException(message);
@@ -406,6 +412,7 @@ public static void notEmpty(@Nullable Collection<?> collection, String message)
406412
* contains no elements
407413
* @since 5.0
408414
*/
415+
@Contract("null, _ -> fail")
409416
public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
410417
if (CollectionUtils.isEmpty(collection)) {
411418
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
@@ -461,6 +468,7 @@ public static void noNullElements(@Nullable Collection<?> collection, Supplier<S
461468
* @param message the exception message to use if the assertion fails
462469
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
463470
*/
471+
@Contract("null, _ -> fail")
464472
public static void notEmpty(@Nullable Map<?, ?> map, String message) {
465473
if (CollectionUtils.isEmpty(map)) {
466474
throw new IllegalArgumentException(message);
@@ -479,6 +487,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, String message) {
479487
* @throws IllegalArgumentException if the map is {@code null} or contains no entries
480488
* @since 5.0
481489
*/
490+
@Contract("null, _ -> fail")
482491
public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
483492
if (CollectionUtils.isEmpty(map)) {
484493
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
@@ -497,6 +506,7 @@ public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSup
497506
* of the offending object's type will be appended.
498507
* @throws IllegalArgumentException if the object is not an instance of type
499508
*/
509+
@Contract("_, null, _ -> fail")
500510
public static void isInstanceOf(Class<?> type, @Nullable Object obj, String message) {
501511
notNull(type, "Type to check against must not be null");
502512
if (!type.isInstance(obj)) {
@@ -516,6 +526,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, String mess
516526
* @throws IllegalArgumentException if the object is not an instance of type
517527
* @since 5.0
518528
*/
529+
@Contract("_, null, _ -> fail")
519530
public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<String> messageSupplier) {
520531
notNull(type, "Type to check against must not be null");
521532
if (!type.isInstance(obj)) {
@@ -530,6 +541,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj, Supplier<St
530541
* @param obj the object to check
531542
* @throws IllegalArgumentException if the object is not an instance of type
532543
*/
544+
@Contract("_, null -> fail")
533545
public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
534546
isInstanceOf(type, obj, "");
535547
}
@@ -546,6 +558,7 @@ public static void isInstanceOf(Class<?> type, @Nullable Object obj) {
546558
* offending subtype will be appended.
547559
* @throws IllegalArgumentException if the classes are not assignable
548560
*/
561+
@Contract("_, null, _ -> fail")
549562
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, String message) {
550563
notNull(superType, "Supertype to check against must not be null");
551564
if (subType == null || !superType.isAssignableFrom(subType)) {
@@ -565,6 +578,7 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
565578
* @throws IllegalArgumentException if the classes are not assignable
566579
* @since 5.0
567580
*/
581+
@Contract("_, null, _ -> fail")
568582
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType, Supplier<String> messageSupplier) {
569583
notNull(superType, "Supertype to check against must not be null");
570584
if (subType == null || !superType.isAssignableFrom(subType)) {
@@ -579,7 +593,8 @@ public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
579593
* @param subType the subtype to check
580594
* @throws IllegalArgumentException if the classes are not assignable
581595
*/
582-
public static void isAssignable(Class<?> superType, Class<?> subType) {
596+
@Contract("_, null -> fail")
597+
public static void isAssignable(Class<?> superType, @Nullable Class<?> subType) {
583598
isAssignable(superType, subType, "");
584599
}
585600

0 commit comments

Comments
 (0)