After a web application accepts the data submitted by a form, it must check its validity. If the form data is invalid, the request is rejected. Similarly, when writing class methods, we often need to perform a method check on the method input parameters. If the input parameters do not meet the requirements, the method rejects subsequent processing by throwing an exception. For example, there is a method to get the input stream based on the file name: inputstream getdata (string file). To make the method run successfully, you must ensure that the file input parameter cannot be null or a blank character, otherwise, no subsequent processing is required. At this time, method writers usually write a section at the beginning of the method body to check the input parameters. Code , As shown below:
Public inputstream getdata (string file ){
If (file = NULL | file. Length () = 0 | file. replaceall ("\ s", ""). Length () = 0 ){
Throw new illegalargumentexception ("the file input parameter is not a valid file address ");
}
...
}
Code similar to the preceding detection method input parameters is very common, but it is not a good idea to manually write the detection logic in each method. Read the spring source code and you will find that spring uses an org. springframework. util. Assert generic class to complete this task.
Assert is translated as an "assertion" in Chinese. readers who have used JUnit are familiar with this concept. It determines that a certain actual running value is the same as expected; otherwise, an exception is thrown. Spring uses this concept to detect method input parameters. The assert class provided by spring has a number of methods that assert method input parameters according to rules, which can meet the requirements of most method input parameters detection. These assertion Methods throw illegalargumentexception when the input parameters do not meet the requirements. Next, let's take a look at the common assert methods in the assert class:
All spring assert methods:
Description of assertion method
1.Notnull (Object object)
If the object is not null, an exception is thrown. The notnull (Object object, string message) method allows you to customize the exception information through message. The opposite method to the notnull () method assertion rule is isnull (Object object)/isnull (Object object, string message), which requires that the input parameter be null;
2.Istrue (Boolean expression)/istrue (Boolean expression, string message)
An exception is thrown when expression is not true;
3.Notempty (Collection collection)/notempty (Collection collection, string message)
An exception is thrown when the set does not contain any element.
Notempty (MAP map)/notempty (MAP map, string message) and notempty (object [] array, string message)/notempty (object [] array, string message) determine the input parameters of the map and object [] types respectively;
4.Haslength (string text)/haslength (string text, string message)
An exception is thrown when text is null or the length is 0;
5.Hastext (string text)/hastext (string text, string message)
Text cannot be null and must contain at least one non-space character; otherwise, an exception is thrown;
6.Isinstanceof (class clazz, object OBJ)/isinstanceof (class type, object OBJ, string message)
If OBJ cannot be correctly modeled as a class specified by clazz, an exception is thrown;
7.Isassignable (class supertype, class subtype)/isassignable (class supertype, class subtype, string message)
Subtype must be able to match with supertype by type; otherwise, an exception is thrown;
The assert asserted class can simplify the code for checking input parameters. For example, after applying the assert asserted class, the code of inputstream getdata (string file) can be simplified as follows:
Public inputstream getdata (string file ){
Assert. hastext (file, "file input parameter is not a valid file address ");
① Use spring assertion class for method input parameter detection
...
}