spring-framework / org.springframework.util / Assert

Assert

abstract class Assert

Assertion utility class that assists in validating arguments.

Useful for identifying programmer errors early and clearly at runtime.

For example, if the contract of a public method states it does not allow null arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class's invariants.

Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to configuration initialization code, there is usually no point in falling back to defaults in such methods.

This class is similar to JUnit's assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically). For example:

 Assert.notNull(clazz, "The class must not be null"); Assert.isTrue(i > 0, "The value must be greater than zero");

Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of String utilities.

Author
Keith Donald

Author
Juergen Hoeller

Author
Sam Brannen

Author
Colin Sampaleanu

Author
Rob Harrop

Since
1.1.2

Constructors

<init>

Assert()

Assertion utility class that assists in validating arguments.

Useful for identifying programmer errors early and clearly at runtime.

For example, if the contract of a public method states it does not allow null arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class's invariants.

Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to configuration initialization code, there is usually no point in falling back to defaults in such methods.

This class is similar to JUnit's assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically). For example:

 Assert.notNull(clazz, "The class must not be null"); Assert.isTrue(i > 0, "The value must be greater than zero");

Mainly for internal use within the framework; consider Apache's Commons Lang for a more comprehensive suite of String utilities.

Functions

doesNotContain

open static fun doesNotContain(textToSearch: String, substring: String, message: String): Unit

Assert that the given text does not contain the given substring.

Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");

open static fun doesNotContain(textToSearch: String, substring: String, messageSupplier: Supplier<String>): Unit

Assert that the given text does not contain the given substring.

 Assert.doesNotContain(name, forbidden, () -> "Name must not contain '" + forbidden + "'"); 

open static fun doesNotContain(textToSearch: String, substring: String): Unit

hasLength

open static fun hasLength(text: String, message: String): Unit

Assert that the given String is not empty; that is, it must not be null and not the empty String.

Assert.hasLength(name, "Name must not be empty");

open static fun hasLength(text: String, messageSupplier: Supplier<String>): Unit

Assert that the given String is not empty; that is, it must not be null and not the empty String.

 Assert.hasLength(name, () -> "Name for account '" + account.getId() + "' must not be empty"); 

open static fun hasLength(text: String): Unit

hasText

open static fun hasText(text: String, message: String): Unit

Assert that the given String contains valid text content; that is, it must not be null and must contain at least one non-whitespace character.

Assert.hasText(name, "'name' must not be empty");

open static fun hasText(text: String, messageSupplier: Supplier<String>): Unit

Assert that the given String contains valid text content; that is, it must not be null and must contain at least one non-whitespace character.

 Assert.hasText(name, () -> "Name for account '" + account.getId() + "' must not be empty"); 

open static fun hasText(text: String): Unit

isAssignable

open static fun isAssignable(superType: Class<*>, subType: Class<*>, message: String): Unit

Assert that superType.isAssignableFrom(subType) is true.

Assert.isAssignable(Number.class, myClass, "Number expected");

open static fun isAssignable(superType: Class<*>, subType: Class<*>, messageSupplier: Supplier<String>): Unit

Assert that superType.isAssignableFrom(subType) is true.

 Assert.isAssignable(Number.class, myClass, () -> "Processing " + myAttributeName + ":"); 

open static fun isAssignable(superType: Class<*>, subType: Class<*>): Unit

Assert that superType.isAssignableFrom(subType) is true.

Assert.isAssignable(Number.class, myClass);

isInstanceOf

open static fun isInstanceOf(type: Class<*>, obj: Any, message: String): Unit

Assert that the provided object is an instance of the provided class.

Assert.instanceOf(Foo.class, foo, "Foo expected");

open static fun isInstanceOf(type: Class<*>, obj: Any, messageSupplier: Supplier<String>): Unit

Assert that the provided object is an instance of the provided class.

 Assert.instanceOf(Foo.class, foo, () -> "Processing " + Foo.class.getSimpleName() + ":"); 

open static fun isInstanceOf(type: Class<*>, obj: Any): Unit

Assert that the provided object is an instance of the provided class.

Assert.instanceOf(Foo.class, foo);

isNull

open static fun isNull(object: Any, message: String): Unit

Assert that an object is null.

Assert.isNull(value, "The value must be null");

open static fun isNull(object: Any, messageSupplier: Supplier<String>): Unit

Assert that an object is null.

 Assert.isNull(value, () -> "The value '" + value + "' must be null"); 

open static fun isNull(object: Any): Unit

isTrue

open static fun isTrue(expression: Boolean, message: String): Unit

Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.

Assert.isTrue(i > 0, "The value must be greater than zero");

open static fun isTrue(expression: Boolean, messageSupplier: Supplier<String>): Unit

Assert a boolean expression, throwing an IllegalArgumentException if the expression evaluates to false.

 Assert.isTrue(i > 0, () -> "The value '" + i + "' must be greater than zero"); 

open static fun isTrue(expression: Boolean): Unit

noNullElements

open static fun noNullElements(array: Array<Any>, message: String): Unit

Assert that an array contains no null elements.

Note: Does not complain if the array is empty!

Assert.noNullElements(array, "The array must contain non-null elements");

open static fun noNullElements(array: Array<Any>, messageSupplier: Supplier<String>): Unit

Assert that an array contains no null elements.

Note: Does not complain if the array is empty!

 Assert.noNullElements(array, () -> "The " + arrayType + " array must contain non-null elements"); 

open static fun noNullElements(array: Array<Any>): Unit

notEmpty

open static fun notEmpty(array: Array<Any>, message: String): Unit

Assert that an array contains elements; that is, it must not be null and must contain at least one element.

Assert.notEmpty(array, "The array must contain elements");

open static fun notEmpty(array: Array<Any>, messageSupplier: Supplier<String>): Unit

Assert that an array contains elements; that is, it must not be null and must contain at least one element.

 Assert.notEmpty(array, () -> "The " + arrayType + " array must contain elements"); 

open static fun notEmpty(array: Array<Any>): Unit
open static fun notEmpty(collection: MutableCollection<*>): Unit
open static fun notEmpty(map: MutableMap<*, *>): Unitopen static fun notEmpty(collection: MutableCollection<*>, message: String): Unit

Assert that a collection contains elements; that is, it must not be null and must contain at least one element.

Assert.notEmpty(collection, "Collection must contain elements");

open static fun notEmpty(collection: MutableCollection<*>, messageSupplier: Supplier<String>): Unit

Assert that a collection contains elements; that is, it must not be null and must contain at least one element.

 Assert.notEmpty(collection, () -> "The " + collectionType + " collection must contain elements"); 

open static fun notEmpty(map: MutableMap<*, *>, message: String): Unit

Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.

Assert.notEmpty(map, "Map must contain entries");

open static fun notEmpty(map: MutableMap<*, *>, messageSupplier: Supplier<String>): Unit

Assert that a Map contains entries; that is, it must not be null and must contain at least one entry.

 Assert.notEmpty(map, () -> "The " + mapType + " map must contain entries"); 

notNull

open static fun notNull(object: Any, message: String): Unit

Assert that an object is not null.

Assert.notNull(clazz, "The class must not be null");

open static fun notNull(object: Any, messageSupplier: Supplier<String>): Unit

Assert that an object is not null.

 Assert.notNull(clazz, () -> "The class '" + clazz.getName() + "' must not be null"); 

open static fun notNull(object: Any): Unit

state

open static fun state(expression: Boolean, message: String): Unit

Assert a boolean expression, throwing an IllegalStateException if the expression evaluates to false.

Call #isTrue if you wish to throw an IllegalArgumentException on an assertion failure.

Assert.state(id == null, "The id property must not already be initialized");

open static fun state(expression: Boolean, messageSupplier: Supplier<String>): Unit

Assert a boolean expression, throwing an IllegalStateException if the expression evaluates to false.

Call #isTrue if you wish to throw an IllegalArgumentException on an assertion failure.

 Assert.state(id == null, () -> "ID for " + entity.getName() + " must not already be initialized"); 

open static fun state(expression: Boolean): Unit