JML Start---Use JML to improve your Java program (4)

Source: Internet
Author: User
Tags ibm developerworks
-->
-->

Abnormal behavior


The behavior specification that is given earlier requires that the queue cannot be empty when the peek () and Pop () methods are invoked , but it is possible to call both methods when the queue is empty. If this happens, the two methods throw a nosuchelementexception. Abnormal. We must revise the code of conduct that we have set out earlier to allow this possibility to happen. In this case, we want to use the JML exceptional_behavior statement.

So far, our code of conduct has been preceded by public Normal_behavior. Here the normal_behavior keyword indicates that this is a normal behavior and the method does not throw any exceptions. the public exceptional_behavior tag can be used to describe the behavior of throwing an exception. The following code snippet shows the Exception section in the behavior specification of the Peek () method in class Priorityqueue:

Code Snippets 9 exceptional_behavior Mark

/*@

@ Public Normal_behavior

@ requires! IsEmpty ();

@ ensures Elementsinqueue.has (Esult);

@ Also

@ Public Exceptional_behavior

@ requires IsEmpty ();

@ Signals (Exception e) e instanceof nosuchelementexception;

@*/

/*@ Pure @*/Object Peek () throws nosuchelementexception;

As with all of the examples we saw earlier, the first part of the specification also begins with the public Normal_behavior , representing the normal behavior; the difference is that this specification has a second part to public Exceptional_ The behavior begins with a description of the exception behavior. As with the Normal_behavior statement, the exceptional_behavior statement also has a requires statement. This requires statement represents the conditions that must be met when the exception listed in the signals statement is thrown. In the example above, Peek () throws a nosuchelementexception exception if the IsEmpty () method returns True.

Signals Statement


signals statements are shaped like signals (e) R , where e is Exception the class itself or one of its subclasses, r is an expression. JML explains a Statement: If an exception is thrown that has a type of e , check to see if it is r true. If so, execute the established specification; otherwise, throw a unchecked exception Translator: unchecked exception Also called runtimeexception , for these two concepts, refer to the description of the exception in java language), Used to indicate that our program code violates the requirements of the exceptional_behavior specification. " "" /span>

AbovePeek ()In the methodSignalsThe statement means that if the queue is empty, it throws aNosuchelementexceptionAbnormal. IfPeek ()Method is not thrown in the runtime.Nosuchelementexceptionof other anomalies, thenJMLIt's going to be a mistake, becauseE instanceof NosuchelementexceptionThe is not true . If you want to deal with the nosuchelementexception exception and want to handle other run-time exceptions, we can modify the signals above. Statement, change to signals (nosuchelementexception e) true; . This means that if the peek () method throws a nosuchelementexception exception, the condition true must be true, and true is a constant that can always meet the criteria, so for The handling of exceptions can work correctly. However, we do not mention information about other anomalies here, and the peek () method can throw its signature ( Translator Note: The signature of a method means the various parts of a method declaration, specifically, the method name, the type of the parameter, Any exceptions allowed by the return type and the general name of the thrown exception. Its signature says it can throw nosuchelementexception exception, which means that it can both throw Nosuchelementexception exception, and can throw runtimeexception .

If there are some elements in the queue and when we call the peek () method or throw a nosuchelementexception exception (or some other exception),JML The Run-time assertion check throws a unchecked exception, which means that the normal post condition fails.

Conclusion


This paper briefly introduces the concept of JML , explains its contribution to the analysis and design of object-oriented system, and demonstrates how to use JML tag in Java program by example . You can download the complete code used in this article from the resources listed below, and you can find more information about JML .

You can use the open source JML compiler to compile the code that contains the JML tag, and the generated class file will automatically check the JML specification at runtime. If your program does not implement what is specified in the specification,JML throws a unchecked exception to explain which specification your program violates. This helps us to capture bugs in our programs and ensures that our code is highly consistent with documents (documents inJML format).

JMLRuntime assertion Check that the compiler is the firstJML tools, other related tools, and JML doc and JML unit , and so on. JML doc and javadoc The tool is similar, but it contains html format document JML specification; JML unit can be a java class file testing framework that makes it easy for you to use the junit tool to test contains jml the java Code of the tag. You can also find additional information about the various aspects of from the resources listed below.

in this connection, allow me to Gary leavens and Yoonsik Cheon expressed deep gratitude that they helped me to solve a part about JML questions and reviewed the article you saw.

Resources

  • Download the JML. zip > source code used in this article .
  • sourceforge is JML specs/ JML specification , open source JML tools such as Span lang= "en-US" >JML compiler, JML unit and the home page for related information.
  • priorityqueue interface and class is open source project Jakarta Universal Collection Component ( JCCC ) Part of the . " /li>
  • gary T. Leavens , albert L. Baker and clyde Ruby "JML /prelimdesign /prelimdesign_toc.html JML design start ( Iowa State University Computer Science Department, 2003 year 1 month is a more detailed description of the JML . " "/ span>
  • Bertrand Meyer in object-oriented software constructs, the second edition of the book on the adoption of the contract (JML The most basic concept) of the design of the discussion (Prentice Hall, 1997).
  • Granville Miller in the introduction of object-oriented system modeling in java/library/j-jmodcol.html ">Java Modeling section (developerWorks, 2002).
  • Eric Allen in the "java/library/j-diag0723.html" >diagnosing Java code:assertions and temporal logic in Java programming"(developerWorks, July 2002) discusses some of the assertion checking limitations.
  • Kyle Brown in "java/library/j-sdao/" >A stepped approach to Java testing with Sdao"( DeveloperWorks, March 2003) discusses how to combine analog data objects with layered tests.
  • For information on all aspects of Java programming, refer to java/>IBM developerWorks java zone .

< end >

Other parts please refer to:

http://www.111cn.net/develop/read_article.asp?id=19198 JML Start---Use JML to improve your Java program (1)
http://www.111cn.net/develop/read_article.asp?id=19199 JML Start---Use JML to improve your Java program (2)
http://www.111cn.net/develop/read_article.asp?id=19200 JML Start---Use JML to improve your Java program (3)

-->
-->

<
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.