This is the second day of my participation in the Novembermore Challenge

Assert is rarely used in Java. Using Assert can improve the readability of code. However, using Assert too much can be detrimental to the maintenance of later code.

1 Introduction to Spring Assert

In Java, assert is a keyword that validates certain conditions. Needs to be enabled in some editors, assert function, can be used, such as IDEA, Eclipse, and so on, is not very easy to use, such as production environment is not applicable, therefore, often using the Spring framework encapsulated org. Springframework. Util. Assert.

Partial source code:

public abstract class Assert {

	/**
	 * Assert a boolean expression, throwing an {@code IllegalStateException}
	 * if the expression evaluates to {@code false}.
	 * <p>Call {@link #isTrue} if you wish to throw an {@codeIllegalArgumentException} * on an assertion failure. * <pre class="code">Assert.state(id == null, "The id property must not already be initialized"); </pre> *@param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalStateException if {@code expression} is {@code false}
	 */
	public static void state(boolean expression, String message) {
		if(! expression) {throw newIllegalStateException(message); }}/**
	 * Assert a boolean expression, throwing an {@code IllegalStateException}
	 * if the expression evaluates to {@code false}.
	 * <p>Call {@link #isTrue} if you wish to throw an {@code IllegalArgumentException}
	 * on an assertion failure.
	 * <pre class="code">
	 * Assert.state(entity.getId() == null,
	 *     () -&gt; "ID for entity " + entity.getName() + " must not already be initialized");
	 * </pre>
	 * @param expression a boolean expression
	 * @param messageSupplier a supplier for the exception message to use if the
	 * assertion fails
	 * @throws IllegalStateException if {@code expression} is {@code false}
	 * @since5.0 * /
	public static void state(boolean expression, Supplier<String> messageSupplier) {
		if(! expression) {throw newIllegalStateException(nullSafeGet(messageSupplier)); }}/**
	 * Assert a boolean expression, throwing an {@code IllegalStateException}
	 * if the expression evaluates to {@code false}.
	 * @deprecatedAs of 4.3.7, in favor of {@link #state(boolean, String)}
	 */
	@Deprecated
	public static void state(boolean expression) {
		state(expression, "[Assertion failed] - this state invariant must be true");
	}

	/**
	 * Assert a boolean expression, throwing an {@code IllegalArgumentException}
	 * if the expression evaluates to {@codefalse}. * <pre class="code">Assert.isTrue(i &gt; 0, "The value must be greater than zero"); </pre> *@param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if {@code expression} is {@code false}
	 */
	public static void isTrue(boolean expression, String message) {
		if(! expression) {throw newIllegalArgumentException(message); }}/**
	 * Assert a boolean expression, throwing an {@code IllegalArgumentException}
	 * if the expression evaluates to {@code false}.
	 * <pre class="code">
	 * Assert.isTrue(i &gt; 0, () -&gt; "The value '" + i + "' must be greater than zero");
	 * </pre>
	 * @param expression a boolean expression
	 * @param messageSupplier a supplier for the exception message to use if the
	 * assertion fails
	 * @throws IllegalArgumentException if {@code expression} is {@code false}
	 * @since5.0 * /
	public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
		if(! expression) {throw newIllegalArgumentException(nullSafeGet(messageSupplier)); }}/**
	 * Assert a boolean expression, throwing an {@code IllegalArgumentException}
	 * if the expression evaluates to {@code false}.
	 * @deprecatedAs of 4.3.7, in favor of {@link #isTrue(boolean, String)}
	 */
	@Deprecated
	public static void isTrue(boolean expression) {
		isTrue(expression, "[Assertion failed] - this expression must be true"); }}Copy the code

As you can see from the code, expressions for Assert assertions fall into two basic categories

  • Assert. Method name (Boolean expression) : These methods are all labeled obsolete and call the second type of method
  • Assert. Method name (Boolean expression, error message) : This type of method, if the Boolean expression is not satisfied, will throw an exception and encapsulate the exception message.

When using method does not meet the Boolean expression, corresponding exception will be thrown, such as an IllegalStateException and IllegalArgumentException, such exceptions are a runtime exception.

2 use of Spring Assert

        if(Condition true) {// Business processing
        }
Copy the code

The above can be replaced by

Assert.isTrue(Condition true,"Condition not met");
// Business processing
Copy the code
    public static void main(String[] args) {

        Assert.isTrue(1>0);
        System.out.println("Assertion 1>0 is successful");
        Assert.isTrue(1 < 0."Failed to assert 1 < 0");
    }


/ * run results: Exception in the thread "main" assertion 1 > 0 success Java. Lang. IllegalArgumentException: Claim 1 < 0 failure at org. Springframework. Util. Assert. IsTrue (121) Assert. Java: the at com.cf.demo.config.MailTest2.main(MailTest2.java:61) */
Copy the code

When used, it is found that Spring mainly throws illegal parameter exceptions and illegal status exceptions, which are mainly used for parameter verification and can improve the readability of code. If -else statements are used to branch and enter if the condition is satisfied, while Assert assertions are used to run down if the condition is satisfied, and exceptions are raised if the condition is not satisfied.

References:

www.runoob.com/w3cnote/c-a…

zhuanlan.zhihu.com/p/265444322