Spring Assert (method into parameter detection Tool class-assertion)

Source: Internet
Author: User
Tags object object

The

Web app needs to check for legitimacy after it accepts the data submitted by the form, and if the form data is illegal, the request will be dismissed. Similarly, when we write a method of a class, we often need to check the validity of the method's entry, and if the incoming parameter does not meet the requirements, the method will reject the subsequent processing by throwing an exception. As an example: there is a way to get the input stream based on the file name: InputStream getData (String file), in order for the method to execute successfully, you must ensure that the file entry parameter cannot be null or white space character, otherwise no subsequent processing is necessary. At this point the writer of the method usually writes a piece of code that detects the incoming parameter at the very front of the method body, as follows:  

Public inputstream getData (String file) { 
     if (file = = NULL | | file.length () = = 0| | File.replaceall ("\\s", ""). Length () = = 0) { 
  &nb sp;     throw new IllegalArgumentException ("File entry is not a valid document address");  
   } &NBSP,
...  


Code that is similar to the detection method's entry is very common, but it is not a good idea to use manual detection logic in each method. Reading the spring source, you will find that spring uses a Org.springframework.util.Assert generic class to accomplish this task.  

assert translates to Chinese as "assertion", and readers who have used JUnit are familiar with this concept, which determines that an actual running value is the same as expected, or throws an exception. Spring's detection of method parameters borrows this concept, and the Assert class provided has many methods to assert the method's parameters according to the rules, which can satisfy the requirements of most methods ' incoming parameter detection. These assertion methods throw illegalargumentexception when the incoming parameter does not meet the requirements. Let's take a look at the common assertion methods in the Assert class:  
All methods of Spring assert:

Assertion Method Description
1. Notnull (Object object)
When an object is not NULL, an exception is thrown, and 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 entry 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 does not contain an element.
Notempty (map map)/notempty (map map, string message) and Notempty (object[] array, string message)/Notempty (object[] Array , String message) to determine the entry of the Map and object[] types, 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 (method into parameter detection Tool class-assertion)

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.