The application of assert in Web development __web

Source: Internet
Author: User
Tags assert object object
J2SE 1.4 offers a new feature in language, the assertion feature, which is the biggest innovation in the Java language of this version. 

Theoretically, the correctness of the program can be proved by assertion method, but it is a rather complicated work, and there is not much practical significance at present. In an implementation, assertion is a statement in a program that checks a Boolean expression, and a correct program must guarantee that the Boolean expression evaluates to true; if the value is False, the program is already in an incorrect state.
 
The system will give a warning or exit. Generally speaking, assertion is used to guarantee the most basic and critical correctness of a program. Assertion checks are typically opened during development and testing.
 
To improve performance, the assertion check is usually turned off after the software is released. 1, syntax representation in syntax, in order to support Assertion,java added a keyword assert.
        It consists of two expressions, as follows: Assert expression1;

Assert Expression1:expression2; In both expressions, expression1 represents a Boolean expression, Expression2 represents a basic type or an object, and the base type includes boolean,char,double,float,int and long. 

Because all classes are subclasses of object, this parameter can be used for all objects. 2, meaning at runtime, if the assertion function is turned off, these statements will not play any role.
 
If the assertion feature is turned on, the expression1 value is computed, and if its value is false, the statement strongly throws a Assertionerror object. If the assertion statement includes the expression2 parameter, the program calculates the expression2 result and then takes the result as an argument for the Assertionerror constructor to create the Assertionerror object and throw the object 

If the expression1 value is true,expression2, it will not be evaluated. 

A special case is that if the expression itself throws a exception when the expression is evaluated, the assert stops running and throws the exception. 3, compiling because assert is a new keyword, using the old version of the JDK is unable to compile with assert source program. Therefore, we must makeWith the JDK1.4 (or newer) Java compiler, we must add-source 1.4 as an argument when using the Javac command. 

-source 1.4 means that the source code is compiled using JDK version 1.4, otherwise the compilation cannot pass because the default Javac compiler uses the JDK1.3 syntax rules. 
 When you use the IDE tools such as Eclipse,jbuilder, you should pay attention to the compiler version, the JRE used, and the Javac version

method into the parameter detection Tool class

Web applications need to be checked for legitimacy after accepting form submissions, and requests will be dismissed if the form data is not valid. Similarly, when we write the method of a class, we often need to check the validation of the method entry, and if the argument does not meet the requirement, the method rejects the post-processing by throwing an exception. For example: There is a way to get an input stream based on the filename: InputStream getData (String file), in order for the method to execute successfully, you must ensure that the file entry parameter cannot be null or whitespace characters, otherwise no subsequent processing is necessary. In this case, the writer of the method usually writes a section of code that detects the incoming parameter in front of the method body, as follows:

Public InputStream getData (String file) {
if (file = = NULL | | file.length () = = 0| | File.replaceall ("\\s", ""). Length () = 0) {
throw new IllegalArgumentException ("file into parameter is not a valid address");
}
...
}

Code similar to the above instrumentation method is very common, but it is not a good idea to use the method of manually writing detection logic in each of these methods. Read spring source, and you'll find that spring uses a Org.springframework.util.Assert generic class to accomplish this task.

Assert translation is Chinese as "assertion", and the reader who has used JUnit is familiar with this concept, it determines that one actual running value is the same as expected, otherwise throws an exception. Spring's detection of method parameters borrows this concept by providing an Assert class that has many methods of asserting the method's parameters by rule, and satisfies most of the requirements of the method's incoming parameter detection. These assertion methods throw illegalargumentexception when the parameter does not satisfy the requirement. Next, let's take a look at the common assertion methods in the Assert class:

Assertion method
Description

Notnull (Object object)
When an object is not NULL to throw an exception, the Notnull (object, String message) method allows you to customize the exception information through the message. The opposite method of the Notnull () method assertion rule is the IsNull (object)/isnull (Object object, String message), which requires that the incoming parameter must be null;

IsTrue (Boolean expression)/IsTrue (Boolean expression, String message)
An exception is thrown when expression is not true;

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 judge the incoming parameters of the MAP and object[] types, respectively;

Haslength (string text)/Haslength (string text, String message)
Throws an exception when text is null or length 0;

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 an exception is thrown;

Isinstanceof (class Clazz, Object obj)/isinstanceof (class type, object obj, String message)
If obj cannot be properly styled, the class specified for Clazz throws an exception;

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;

You can use the Assert assertion class to simplify the code that the method parameters detect, such as InputStream getData (String file) after the Assert assertion class is applied, its code can be simplified to the following form:

Public InputStream getData (String file) {assert.hastext (file, "filename is not a valid address"), using the Spring assertion class for method in-parameter detection ...}

It is obvious that the simplicity of the method has been improved by using Spring's Assert instead of the entry-parameter detection logic implemented by the encoding. Assert does not depend on the Spring container, you can boldly use this tool class in your own application.



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.