Help you debug -java1.4-assertions, assert that make your code more stable

xiaoxiao2021-03-06  107

Use assertions to make your code more stable

Learn how Java's Assertions is based on how to provide developers with better ways to create stable, high quality, easy-to-debug code.

By Josh Street

Maybe you haven't noticed yet, Sun Microsystems has added a new keyword to the Java language. This Assert key is made by JSR41 - a Java Specification Request that ultimately adds real Assertions performance to Java. Because this new Assert keyword and most of the usage familiarity familiar with the C / C developers have a great difference, they will understand that it will benefit you.

Java's assertions features are used for a special purpose: it will throw an error when the user-defined Boolean expression is false. More specifically, an assertion is used as a signal that is discovered into false, which is used to indicate that the current function needs to be interrupted. This performance is used very useful in creating a dedicated debug mode in the real product deployment process.

Open Assertion You need to open (Enable) before using Assertions, because Java Runtime Environment (JRE) will automatically turn all Assertions to close. You can use the -enableassertions tag (FLAG) (abbreviation is -ea) to turn it; You can use some options with these two tags:

NO arguments refers to this tag applies to all Assertions.

PackageName ... refers to this tag applies to all classes in the packagename package and their subclats.

... refers to this tag applies to unnamed (default) packets in the current directory.

ClassName means that the tag is only available for classname.

Look at the following command line:

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

It is used to turn on the foo.bar package other than foo.bar.old and Assertions in its subdirectories.

How to use the Assertion a Java Assertion has two forms. The first form is proven to be desirable:

askERT SOME_BOOLEAN_EXPRESSION;

This means that if the value of some_boolean_expression is False, an AssertionError will be thrown. Note that this is an error, not an exception; in the same manner, it is treated as a unchecked exception. (To learn more about the differences between errors and abnormalities, please review my article "Error and Abnormal".)

Another more complex form of ASSERT statement provides some more features:

askSSERT SOME_BOOLEAN_EXPISSION:

Some_non-void_expression

This form is more useful than the previous one. The second expression SOME_NON-VOID_EXPRESSION is incorporated into the constructor for AssertionError. It can display more information when it happens in Assertion. Note that the second parameter may be any object or basic type (including NULL).

Listing 1 provides a simple example of the form of the second ASSERT statement. Since AssertionError is a non-check exception, there is no need to capture it (capture it due to the reasons for the demonstration description). In fact, Ignore (don't capture) AssertionerRors may look very well, but you shouldn't do this, because this may make the application in an unstable state, especially for a swings-based program This is even more. There are some cases that cannot use Assertions, for example, try to find the following code:

Assert isvalid (MyObject);

Although this looks like a good way, think about what to do when assertion is closed - remember this is its default status: this isvalid method will not be executed! In particular, some behavior (methods, operations, etc.) cannot be used for the first parameter of this Assert statement because (unless other specified specified) it will never be executed.

You may want to know how to write the code to open Assertions, although this is not a good idea, but it will help the known dependence (Dependency) code is very helpful. To this end, this tip is provided:

Boolean assertsenabled = false;

// here's the trick

Assert assertsenabled = true;

IF (! assertsenable)

Throw new runtimeException

"Asserts Must Be Enabled!");

}

You can use this code to be used in any static initialization class that needs to open Assertions. If the assertion is closed, the Assert statement will not be executed and will throw an exception; otherwise it will continue, the condition is invalid.

Solving compatibility issues Java's assertion feature is a language characteristic, not an expansion of API performance. Assertions represents the first significant change in the Java language in multiple versions but also brought back binary compatibility issues. This means that compiled procedures that support Assertions will not be able to run in the old version JRE. In order to make developers more easily create procedures with Assertions, Sun has taken a cautious method. The default status of the program in the JDK 1.4 version is not supported, and the code compiled with Assertions produces the following response:

Warning: as of release 1.4, askERT IS A

Keyword, And May Not Be Used as an identifier

In addition to this Warning, use Assert keywords can also cause many compilation errors. To implement the compiler with Assertions, you need to use a-Source 1.4 tag. For example, the compile command code displayed in the list 1:

Javac -Source 1.4 Assert.java

Java's new Assertion feature provides new methods for developers using advanced technology (such as Design By Contract) to create a stable, high quality code. You need to consider the questions I mentioned, but I think you will agree that this new feature is quite popular in Java language.

- List 1 ---.

Use Assert to test a list 1. This simple example is used to demonstrate how to use Assertions to detect those results that are predictable but unsatisfactory.

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;

}

}

About the Author:

Josh Street is a planning architect of Bank of America. You can pass

RJSTREET@computer.org contact him.

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

New Post(0)