Use assertions to make your code more stable
Learn how Java's assertions (assertion) performance provides developers with a better way to create stable, high-quality, and easily debugged code.
by Josh Street
As you may not have noticed, Sun Microsystems has added a new keyword to the Java language. This assert keyword is presented by JSR41, a Java specification request that eventually adds real assertions performance to Java. Because this new assert keyword is very different from the usage familiar to most of the C + + developers, it will certainly benefit you to understand it in detail.
The Java assertions attribute is used for a particular purpose: it throws an error when the user-defined Boolean expression is false. Rather, a assertion is used as a signal to find a value false, indicating that the current function needs to be interrupted. This performance is useful when creating a proprietary debug mode that should not be present in the real product deployment process.
Open assertion
You need to enable them before using assertions, because the Java Runtime Environment (JRE) automatically closes all assertions. You can use the –enableassertions tag (flag) (abbreviated as –EA) to open it, and then close it with the –disableassertions tag (abbreviated as –DA). You can use some options with both tags:
|
no arguments means that the mark applies to all assertions. |
|
PackageName ... means that the tag applies to all classes in the PackageName package and its child packages. |
|
... Refers to an unnamed (default) package that applies to the current directory. |
|
ClassName means that the tag applies only to ClassName. |
Look at the following command line:
Java-ea:foo.bar ...-da:foo.bar.old MyClass.
|
It is used to open the assertions in Foo.bar packages and subdirectories other than Foo.bar.old.
How and when to use assertion
There are two forms of a Java assertion. The first form proved to be desirable by practice:
Assert some_boolean_expression; |
This means that if the value of Some_boolean_expression is false, a assertionerror is thrown. Note that this is an error, not an exception; Similarly, it is treated as a unchecked exception (exception). (For more information on the difference between errors and exceptions, check out my other article, "Errors and exceptions.") )
Another more complex form of assert statements provides some more functionality:
Assert some_boolean_expression:
some_non-void_expression
|
This form is more useful than the previous one. The second expression Some_non-void_expression is passed into the constructor for the Assertionerror. It can show more information that occurs when assertion fails. Note that the second parameter may be any object or base type (including null).
Listing 1 provides a simple example that uses the form of a second Assert statement. Since Assertionerror is an unchecked exception, it is not necessary to capture it ( It was captured in Listing 1 for illustrative reasons). In fact, ignoring (not capturing) assertionerrors may look good, but you shouldn't do this because it might put your application in an unstable state, especially for a swing-based program.
There are some situations where you can't use assertions, like trying to find a problem with the following code:
Assert IsValid (MyObject); |
Although this may seem like a good idea, think about what to do when assertion closes-remember that this is its default state: This IsValid method will not execute. In particular, some behaviors (methods, operations, and so on) cannot be used for the first argument of this assert statement, because (unless otherwise specifically specified) it will never be executed.
You might want to know how to write code that opens assertions, which is not a good idea, but it can be very helpful for known dependency (dependency) code. For this, Sun offers this little trick:
Boolean assertsenabled = false;
//Here ' s the trick
assert assertsenabled = true;
if (!assertsenabled)
throw new RuntimeException (
"Asserts must be enabled!");
|
You can use this code in any static initialization class that needs to be open assertion. If the assertion is turned off, the Assert statement will not execute and an exception will be thrown, otherwise it will continue and the condition fails.
Resolving compatibility issues
The Java Assertion feature is a language feature, not an extension of API performance. Assertions represents the first significant change in the Java language between multiple versions, but it also brings back the problem of binary compatibility. This means that a program that compiles to support assertions will not run in the old version of the JRE. To make it easier for developers to create programs with assertions, Sun takes a cautious approach. The default state of the program in JDK version 1.4 is not supported assertion, and code compiled with assertions produces the following response:
Warning:as of Release 1.4, assert are a
keyword, and may are used as an identifier
|
In addition to this warning, using the Assert keyword can also cause many compilation errors. In order to compile the program with assertions, you need to use a –source 1.4 tag. For example, the compiled command code shown in Listing 1:
Javac-source 1.4 Assert.java |
The
Java New Assertion feature provides a new way for developers to create stable, high-quality code using advanced technologies such as design by Contract. You need to think about the issues I'm talking about, but I think you'll also agree that this new feature is quite popular in the Java language
--Listing 1---.
detect with assert List 1. This simple example is used to demonstrate how to use assertions to detect predictable but unsatisfactory results. The
public class Assert {public static void
Main (string[] args) {try {dosomething ();
catch (Assertionerror error) {error.printstacktrace ();
The public static int dosomething () {int i = 2;
Assert I!= 2: "I am 2 for some reason";
return i; } |
| ,
About Author:
Josh Street is the program architect of Bank of America. You can contact him through rjstreet@computer.org.