public abstract class Assert extends Object
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; for a more comprehensive suite
of assertion utilities consider org.apache.commons.lang3.Validate from
Apache Commons Lang,
Google Guava's
Preconditions,
or similar third-party libraries.
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
isNull(Object object)
已过时。
as of 4.3.7, in favor of
isNull(Object, String) |
static void |
isNull(Object object,
String message)
Assert that an object is
null. |
static void |
isNull(Object object,
Supplier<String> messageSupplier)
Assert that an object is
null. |
static void |
isTrue(boolean expression)
已过时。
as of 4.3.7, in favor of
isTrue(boolean, String) |
static void |
isTrue(boolean expression,
String message)
Assert a boolean expression, throwing an
IllegalArgumentException
if the expression evaluates to false. |
static void |
isTrue(boolean expression,
Supplier<String> messageSupplier)
Assert a boolean expression, throwing an
IllegalArgumentException
if the expression evaluates to false. |
static void |
notEmpty(Object[] array,
String message)
Assert that an array contains elements; that is, it must not be
null and must contain at least one element. |
static void |
notNull(Object object)
已过时。
as of 4.3.7, in favor of
notNull(Object, String) |
static void |
notNull(Object object,
String message)
Assert that an object is not
null. |
static void |
notNull(Object object,
Supplier<String> messageSupplier)
Assert that an object is not
null. |
public static void isTrue(boolean expression,
String message)
IllegalArgumentException
if the expression evaluates to false.
Assert.isTrue(i > 0, "The value must be greater than zero");
IllegalArgumentException - if expression is falsepublic static void isTrue(boolean expression,
Supplier<String> messageSupplier)
IllegalArgumentException
if the expression evaluates to false.
Assert.isTrue(i > 0, () -> "The value '" + i + "' must be greater than zero");
IllegalArgumentException - if expression is false@Deprecated public static void isTrue(boolean expression)
isTrue(boolean, String)IllegalArgumentException
if the expression evaluates to false.public static void isNull(Object object, String message)
null.
Assert.isNull(value, "The value must be null");
IllegalArgumentException - if the object is not nullpublic static void isNull(Object object, Supplier<String> messageSupplier)
null.
Assert.isNull(value, () -> "The value '" + value + "' must be null");
IllegalArgumentException - if the object is not null@Deprecated public static void isNull(Object object)
isNull(Object, String)null.public static void notNull(Object object, String message)
null.
Assert.notNull(clazz, "The class must not be null");
IllegalArgumentException - if the object is nullpublic static void notNull(Object object, Supplier<String> messageSupplier)
null.
Assert.notNull(entity.getId(),
() -> "ID for entity " + entity.getName() + " must not be null");
IllegalArgumentException - if the object is null@Deprecated public static void notNull(Object object)
notNull(Object, String)null.public static void notEmpty(Object[] array, String message)
null and must contain at least one element.
Assert.notEmpty(array, "The array must contain elements");
IllegalArgumentException - if the object array is null or contains no elementsCopyright © 2024 tan. All rights reserved.