Spring Assert Assertion Tool class

Source: Internet
Author: User
Tags object object

The Org.springframework.util.Assert class implements legitimacy detection in Spring, with the following method of Assert:

Assertion Method Description:

1. notnull (Object object)    
When Object is not null When an exception is thrown, the Notnull (Object object, String message) method allows you to customize the exception information with a message. The opposite of the Notnull () method assertion rule is isNull (Object object)/isnull (Object object, String message), which requires that the incoming parameter must be null; 

2.  istrue (Boolean expression)/IsTrue (Boolean expression, String message)    
When expression does not throw an exception for true;  

3.  notempty (Collection Collection)/Notempty (Collection Collection, String message)   
throws an exception when the collection contains no elements.  
Notempty (map map)/notempty (map map, string message) and Notempty (object[] array, String message)/Notempty (Obje Ct[] Array, String message) to determine the entry of the Map and object[] type respectively;  

4.  haslength (string text)/Haslength (string text, string message)  

Throws an exception when text is null or has a length of 0;

5. HasText (string text)/HasText (string text, string message)

Text cannot be null and must contain at least one character that is not a space, otherwise throws an exception;

6. isinstanceof (class Clazz, Object obj)/isinstanceof (class type, object obj, String message)

If obj cannot be properly styled as clazz the specified class will throw an exception;

7. isassignable (class Supertype, class subtype)/Isassignable (class Supertype, class subtype, String message)

Subtype must be able to match by type to supertype, otherwise an exception will be thrown;

Use the Assert assertion class to simplify code for method-in-parameter detection, such as InputStream getData (String file) after applying the Assert assertion class, its code can be simplified to the following form:

Public InputStream getData (String file) {
Assert.hastext (file, "filename entry is not a valid document address");
① using Spring assertion classes for method-in-parameter detection
...
}

Spring Assert Assertion Tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.