As a result, make the code more stable

zhaozj2021-02-16  44

As a result, make the code more stable

Understanding the assertions in Java how to help developers create more stable, quality and easy-to-error

Maybe you haven't noticed it yet, Sun Microsystems has added a new keyword to the Java language. This keyword Assert is the results of JSR41, and a Java Specification Request finally joined a real assertion to the Java language. Because the new keyword Assert is very different from C / C usage habits. You will benefit from in-depth understanding of this Java new feature.

Java assertions provide a special feature: throw an error when the user-defined Boolean expression is false. More specifically, if you need to interrupt the current operation when a value is false, you can use the assertion. For example, you have to develop a Debug mode that can be turned off at the product deployment, which is very useful for you.

Open your assertion

Before using the assertion, you need to turn on the disclaimer because Java Runtime Environment (JRE) off defaults to assert the assertion. You can use the flag (FLAG) -enableassertions to turn on the disconnection function. Similarly, you can also use tag -disableassertions to turn off assertion. You can use the following options on both tags:

l None parameter tag accepts all assertions

l PackageName tag only accepts classes within the package name and subcautions (SUBPACKAGE)

l ... Tags that accept the default package of the current directory (Default package)

The classname tag only accepts the specified class.

Look at this command line:

Java -ea: foo.bar ... -da: foo.bar.old myclass

It means to open an assertion for the package foo.bar and its subpapers, except for foo.bar.OLD.

How to use when using an assertion

The Java assertion has two forms, the first is:

askSSERT SOME_BOOLEAN_EXPRESSION

It explains how Some_Boolean_Expression is false, and an assertionerror will be thrown. Note that this is an error, not Exception. That is, this is an unchecked exception (learn more about Error and Exception, please read the author's article "Understand the difference between Error and Exception").

Another more complex form provides more features:

askSSERT SOME_BOOLEAN_EXPISSION:

Some_non-void_expression

This version is more useful than the above. The second expression, Some_non-void_expression will be transferred to the constructor when AssertionError occurs. This allows more detail information when asserting fails. Note that the second parameter can be any object or original type (including NULL).

Listing 1 provides a simple example that uses the form of the second assertion. Since AssertionError is unchecked Exception, it is not necessary to capture (in the purpose of the presentation, capture in Listing 1). In fact, you may tend to not capture AssertionError, but do not recommend this, because it may enable your system to enter instability. Especially those Swing (Swing-based)-based applications. Listing 1. This simple example demonstrates how to use asserts to detect those that will definitely appear but may not be what we need.

Public Class Assert

{

Public static void main (string [] args)

{

Try

{

DOSMETHING ();

}

Catch (Assertionerror Error)

{

Error.PrintStackTrace ();

}

}

Public static int dosomething ()

{

INT i = 2;

Assert I! = 2: "I Was 2 for Some Reason";

Return I;

}

}

As should not be used in some cases. For example, look at the following code:

Assert isvalid (MyObject);

Although this seems not bad, bad, assertions may be closed, this is the default state. Then method isvalid will never be executed. In general, an action (method, operation, etc.) should not be used as the first parameter of Assert, unless there is special needs because they may not be executed.

You may be thinking, how can I write a program that can detect if the assertion is turned on. Although this is not a good idea, it is still very useful. Now SUN provides this tip:

STATIC {

Boolean assertsenabled = false;

// here's the trick

Assert assertsenabled = true;

IF (! assertsenable)

Throw new runtimeException

"Asserts Must Be Enabled!");

}

You can join this code block in any code that uses the assertion. If the assertion function is turned off, the assert will not be executed, and the exception will be thrown.

Solve compatibility

Java assertions are a new language feature, not an API enhancement. In many versions, assertions are the first to pay attention to changes, and it also brings compatibility with Binary versions. Opened the assertion support to compile the files that cannot be executed in the old jRE. In order to make the developer more assured, SUN uses some small ways. The default compilation mode of JDK 1.4 does not support asserting and generates the following when compiling with asserted code:

Warning: as of release 1.4, askERT IS A

Keyword, And May Not Be Used as an identifier

In addition, all code using keyword assert generate compilation errors when compiling. If you need to compile the code using the assertion, you must use the tag -source 1.4. For example, compile the code in Listing 1 using the following code:

Javac -Source 1.4 Assert.java

Java's assertion skills enable developers to create more stable, better code, just like the DESIGN BY CONSTRACT. You need to carefully consider the questions I mention, but you will definitely agree that it is indeed a popular new feature. About author

Josh Street is a designer of Bank of America. You can contact him through rjstreet@computer.org.

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

New Post(0)