Learn how to enable and disable assertions in Java code

xiaoxiao2021-03-06  95

Merlin's magic: use assertion

English original

content:

Ask the keyword Enable assertion Method for using the assertion By programming, the summary reference information is the evaluation of the author to this article

related information:

Incremental Development with antid and junitmerlin's magic column Documenting Java Member Functionsalphaworks Jate

Also in the Java area:

Teaching tools and product code and components all articles practical skills

Learn how to enable and disable assertion in Java code John Zukowski (Jaz@zukowski.net) President, JZ Ventures, Inc. 2002 February

Merlin adds an optional pre-condition and post-conditionation verification capabilities to improved methods. Although the assertion function is disabled when the default is runtime, it can be enabled to check the error condition. In this article

In Merlin's magic, John Zukowski takes you to understand some of the basic knowledge of the assertion and how to enable and disable the assertions through command lines and programming methods. Please click on the top or bottom of the article

Discussion

Share your ideas in this article on the forum.

As an assertion function is a new feature that the Java 1.4 release is a new feature that is hot. You can view this feature as an advanced form of exception handling. As a result, it is some Boolean expression, and the programmer believes that the value of a particular point in the program is true. For example, a programmer may include assertions in the program so that you will never reach the default condition in the Switch statement. Since the assertion verification can be enabled or disabled at any time, the assertion verification can be disabled when the assertion verification is enabled when the assertion is enabled. Similarly, after the program is put into operation, the end user can re-enable the assertion when it comes to the problem. In this column articles, the assertions are enabled and disabled by demonstrating two different ways to optimize code testing and performance, and I will tell you some basic knowledge about adding assertions to the code. As a keyword assertion check, it is abnormal, but when the assertion fails, it will not throw an exception, but will throw an AssertionError. Like all errors, it is difficult to recover (if not impossible), because such a fault indicates that the program status has deviated from the normal operation parameters. The two basic configurations of the assertion are as follows:

Assert BooleaneXpression;

askERT BOOLEANEXPIPRESSION: MESSAGE;

Note that an optional message is displayed as the result of the error thrown. The assertion statement can be added as a pre-condition or rear condition of the method. Boolean expressions can also be established to call methods. Similarly, you may want to add assertions to the ELSE condition in the IF block, where this condition has been set, or add assertions to the default in the Switch statement, making the default. The only restriction of the Assert keyword is that it must be in the executable block. You cannot use the asserted class variable to declare a piece of use, but it can be placed in any method. Enable assertions If you want to use assertions, you must use some special command line options to compile and run the program. Since the compiler runs on the 1.3 compatibility mode, you must explicitly ask for running in 1.4 mode. To compile the procedures with assertions, simply pass-Source 1.4 settings to the Java compiler. By default, an assertion check is disabled, so you have to explicitly enable this feature at runtime. Please use the -EnableAssrtions option or shorter-EA option to enable assertion check. The following simple example introduces some of the necessary steps to add assertions to the program. It checks the number of command line parameters. If the value is not 0, the report problem is reported:

Public class asserttest {public static void main (string args [] {

askERT Args.length == 0: args.length "! = 0";

System.out.println (Args.Length);

}

}

Be sure to compile the program as follows:

Javac -Source 1.4 Asserttest.java

To test the program, just run the program and pass the command line parameters:

Java Asserttest 1 2 3 4

Because the program is used to display the number of parameters, the test program will display four. Since the assertion check is disabled in the default, no assertion error is not detected. Now, please have an assertion check to run the program:

Java-Ea Asserttest 1 2 3 4

With enabling assertion check running procedures producing the following error report:

Exception in thread "main" java.lang.assertionerror: 1! = 0

At asserttest.main (askTTEST.JAVA: 3)

This is the essence of the entire assertion function. Here, the most difficult part is to determine what is set to the Assert key and the conditions. Other methods of using assertions For the entire program, although the -ea option is an easy way to enable assertion, other options can also be used. In addition, -ea cannot be used for system classes; because of that reason, -ENABLESYSTEMASSERTIONS, or simpler, use -esa. To enable assertions for the entire package (ie package and sub-package), just add a colon, the name of the package and a omitted number (...) after the -ea option. For example, the following syntax will enable assertion check on Net.zukowski.ibm packets and all its subcles:

Java -ea: Net.zukowski.ibm ... Eclipsetest

For specific classes, you need to specify a complete class name instead of adding omitting after the package name. If you want to enable assertion checking for the entire collection of the package, not a class or package (tree) enable assertion check, the -da tag can meet this requirement ("-da" is the abbreviation of -disableassertions). Just list enabled and disable options, you only need to do this. For example, the following syntax enables assertion check on the NET.ZUKOWSKI.IBM package, but does not enable assertion check for the Net.zukowski.ibm.BeanUtils class:

Java -ea: Net.zukowski.ibm ... -da: net.zukowski.ibm.beanutils Eclipsetest

By programming enable assertions In addition to enable assertion checks from the command line, you can also check (or disable) check. Since this only affects the class being loaded in the future, it cannot be closable or opened. ClassLoader uses the following four methods to control the enabled and disabled:

Public Void SetDefaultsSctionStatus (Boolean Enabled): This method sets the default state of the packets and classes loaded by the class loader. This setting can be overwritten by setting a specific package and class option. Public Void SetPackageAnStatus (String PackageName, Boolean Enabled): To overwrite the status of the package and all its subclats, incoming the package name, and enabled. Public Void SetClassAsStatus (String ClassName, Boolean Enabled): To override the status of a particular class, pass to fully qualified class names and enabled status. Public void clearassertionStatus (): This method resets all packets and classes to false, and then reset the class loader to false. To get a class loader, call the getClassLoader () method of the Class object. Use the following command to non-static methods: classloader loader = getClass (). GetClassLoader ();

For static methods, getClass () cannot be called because there is no instance, so simply add .class to the current class name, as shown below:

ClassLoader Loader = Eclipsetest.class.getClassLoader ();

Keep in mind that this only affects the class being loaded. In most cases, you will find that you are enabled from the command line.

转载请注明原文地址:https://www.9cbs.com/read-105672.html

New Post(0)