JML start --- use JML to improve your Java program (1)

zhaozj2021-02-16  45

JML start

Improve your Java programs using JML

By Joe Verzulli (Joe55055@yahoo.com)

http://www-106.ibm.com/developerWorks/java/library/j-jml.html

Java Modeling Language, JML is a symbolic language for detailed designs, which encourages you to look at Java classes and methods in a new way. In this tutorial, Java program design Senior consultants Joe Verzulli will introduce this new tool and how to use this tool.

Object-oriented analysis and design (OOAD) an important principle is that process thinking should be as delayed as much as possible, but most people who follow this principle are not only to apply this principle to the method. Once the design is designed and interface, the following things are naturally implemented in which the definitions are implemented. Yeah, what can we do? Is there any other way to use? After all, use Java to design and use other languages, we must implement each method step by step.

The tag itself just means how to do a thing (how to do something, at all, no matter what we want to do. If we can know what we can reach before doing something, it is very good, but the Java language does not provide us with a way to insert this information into our program code.

Java Modeling Language, JML adds some symbols in the Java code, which is used to identify what is dry, but it doesn't care about it. If you use JML, we can describe the expected features of a method regardless of how he is implemented. In this way, JML delays the process's thinking delays in the method design, thereby expanding this principle of object-oriented design.

JML introduces a large number of structures for describing behavior, such as model domains, quantifiers, asserted visible range, pretreatment, post-processing, conditional inheritance, and normal behavior (relatively relative to abnormal behavior). These structures make JML very powerful, but you don't have to understand or use all aspects described above, and you don't need to use all of these aspects. You can learn a little bit, from very simple start.

This article uses a step-by-step way to introduce JML. We must first understand the various benefits of using JML, especially the impact on the development and compilation process. Then, we will discuss some of the structures of JML, such as front conditions, back conditions, model domains, quantifiers, side effects, and abnormal behavior, and more. At the same time, while discussing these structures, we will give some routines to give you an intuitive feel. Through this study, you will be able to have a conceptual understanding of JML, so you can apply JML in your own project.

JML overview

Using JML to declare a method or class's expected behavior can significantly improve overall development processes. Add a model tag to your Java program code:

What is more accurately describing these code?

Can efficiently discover and correct the bug in the process

You can reduce the opportunity to introduce bugs during application upgrade

You can discover the customer code to use the wrong use of class.

Documents that can provide a JML format that is fully consistent with the application code

The JML mark is always inside the Java annotation, so there is no impact on the normal compiled code. If you want to compare a normal class and what is the difference in class that uses JML, you can use an open source JML compiler (please refer to the following address). Code compiled with JML compiler If there is no implementation required by the JML specification, a JML exception will be thrown when running. This feature can not only help us capture bugs in the code, but also make sure that the document in JML can be consistent with the program code.

The following part of the article, I will use the PriorityQueue interface in the Jakarta Commons Collection Component (JCCC) project, and the BinaryHeap class to demonstrate the various properties of JML. Here you can find this two files that use JML tags.

Requirements and responsibilities

The code used herein (refer to the following address) includes the priorityQueue interface in the open source project JCCC. The interface is naturally a signature of some methods, including the parameter type of the method, the type of return value, does not involve the implementation of the method. Under normal circumstances, or just according to the Java grammar, implement the interface of the interface, as long as the various methods defined in the interface, whether the implementation is how to leave the niche. We don't want this, we want to be able to determine a behavior specification, all classes that implement this interface use our way to implement the methods defined in this interface. We can do this by using JML.

Consider the POP () method of the PriorityQueue interface, what kind of functional requirements should the POP () method should have a POP () method for priority queues? At the very least, there should be three: first, if you want to call the POP () method, there must be at least one element in the queue; second, the method should return the highest priority element in the queue; third, the method should be from the queue Delete the elements returned.

The following code segment shows the JML tag that satisfies the first requirements:

Code section 1 POP () method function specification JML tag

/ * @

@ Public normal_behavior

@ Requires! ISempty ();

@ * /

Object pop () throws nosuchelementexception;

As mentioned earlier, the JML tag is written in the java code. Multi-line annotations containing JML tags with / * @, JML ignores any blank line starting with @. If it is a single line, you can also use @ @.

Here, the PUBLIC keyword in JML comments is the same as the public meaning in Java. It indicates that all other classes in the program will follow this JML requirements. Public requirements can only be applied to public methods and public member variables. JML also has private-, protected-, and package-level scopes. Similarly, the rules of these scope are very similar to the rules of the role domain in the Java language.

Here, the Normal_Behavior keyword means that this JML requires that this is a normal situation and does not throw an exception when running. Behind, we will describe how the abnormal behavior is defined.

The front condition and the rear condition JML keyword Requires are used to represent the pre-condition, and the front condition indicates some requirements that must be met before calling a method. The above code segment contains a prerequisites that require calling the POP () method is that the iSempty () method returns false, which means that this queue contains at least one element.

The back condition specification for a method represents a method of responsibility, that is, when this method returns, it must meet the requirements of this back condition. In our example, the POP () method should return the highest priority element in the queue. We want to specify a rear condition requires JML to check if this is satisfied when it is runtime. To do this, we must track all the elements added to this priority queue so that we can judge which element should be returned to the POP () method. How to do it? You may consider adding a member variable to the priorityQueue interface to store the value of the elements in the queue, but there are two questions:

PriorityQueue is an interface that may have a variety of different specific implementations, such as Binary Heap, Fibonacci Heap or Calendar Queue, which is consistent with its various implementations, and JML tags should not involve any specific implementation. detail. As an interface, PriorityQueue can only have a static member variable.

In order to deal with this situation, JML introduced a concept called model domain.

The model domain model domain is similar to the member variable, which can only be applied to the behavior specification. This is an example of declaring model domain in priorityQueue:

@ Public Model Instance Jmlobjectbag ElementsinQueue;

This statement means that there is a model domain called ElementSinqueue, and its type is JMLObjectBag (this data type is defined in JML). Instance Keyword Representation Although this domain is defined in an interface, any class that implements this interface has a separate, non-static Elementsinqueue domain. Like other JML tags, this statement is also in the comment, so the regular Java code cannot use this ElementSinQueue variable. When the program is running, there is no object with a member variable called ElementSinQueue.

Code of conduct and implementation

Use a bag to store the elements in the queue, then check that each element finds that the highest priority will make people feel that the efficiency is not high. However, this is only part of the behavioral specification without involvement. The role of behavioral norms is to describe the behavior interface of PriorityQueue, that is, external behavior that specifies customer code that use PriorityQueue. The specific implementation of the PriorityQueue interface can use any more efficient ways as long as the requirements of this behavioral specification can be met. For example, JCCC has a BinaryHeap class that implements this interface, and its implementation is to use a binary heap stored in an array.

However, although the operational efficiency is not required when using JML definition, the JML assertion check is very important. So the operation of the program may have performance pressure when an assertion check is turned on.

ElementSINQUEUE stores the value of the element added to the priority queue, the following code segment displays the POP () method how to use ElementSinQueue:

Code segment 2 uses the model domain in the back condition of POP ()

/ * @

@ public normal_behavior

@ Requires! ISempty ();

@ Ensures

@ Elementsinqueue.equals ((JMLObjectbag)

@ / OLD (ElementSINQUEUE))

@ .Remove (/ result) &&

@ /Result.equals (/ OLD (PEEK ()));

@ * /

Object pop () throws nosuchelementexception;

The EnsuRES keyword indicates that the back condition must be met when the POP () method returns. / Result is a JML keyword, which is equal to the return value of the POP () method. / OLD () is a JML function that returns the value of the previous parameter to the POP () method call.

This EnsuRES statement contains two back conditions. First, the element returned by the POP () method must be deleted from ElementSINQUEUE. Second, this return value is consistent with the value returned by the peek () method.

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

New Post(0)