A SpecistCation Language Design for the Java Modeling Language
Total Page:16
File Type:pdf, Size:1020Kb
View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Digital Repository @ Iowa State University Computer Science Technical Reports Computer Science 2008 A Specification Language Design for the Java Modeling Language (JML) Using Java 5 Annotations Kristina B. Taylor Iowa State University Follow this and additional works at: http://lib.dr.iastate.edu/cs_techreports Part of the Software Engineering Commons Recommended Citation Taylor, Kristina B., "A Specification Language Design for the Java Modeling Language (JML) Using Java 5 Annotations" (2008). Computer Science Technical Reports. 307. http://lib.dr.iastate.edu/cs_techreports/307 This Article is brought to you for free and open access by the Computer Science at Iowa State University Digital Repository. It has been accepted for inclusion in Computer Science Technical Reports by an authorized administrator of Iowa State University Digital Repository. For more information, please contact [email protected]. A Specification Language Design for the Java Modeling Language (JML) Using Java 5 Annotations Abstract Design by contract specification languages help programmers write their intentions for a piece of code in a formal mathematical language. Most programming languages do not have built-in syntax for such specifications, so many design by contract languages place specifications in comments. The aJ va Modeling Language (JML) is one such specification language for Java that uses comments to specify contracts. However, starting with version 5, Java has introduced annotations, a syntactical structure to place metadata in various places in the code. This thesis proposes an initial design to writing JML contracts in the Java 5 annotation syntax and evaluates several criteria in the areas of specification languages and Java language design: whether these annotations are expressive enough to take advantage of annotation simplicity and tool support, and whether the annotation syntax is expressive enough to support handling a large specification language such as JML. Keywords JML annotation specification Disciplines Software Engineering This article is available at Iowa State University Digital Repository: http://lib.dr.iastate.edu/cs_techreports/307 A specification language design for the Java Modeling Language (JML) using Java 5 annotations by Kristina Boysen Taylor A thesis submitted to the graduate faculty in partial fulfillment of the requirements for the degree of MASTER OF SCIENCE Major: Computer Science Program of Study Committee: Gary T. Leavens, Krishna Rajan, Major Professor Hridesh Rajan Iowa State University Ames, Iowa 2008 Copyright c Kristina Boysen Taylor, 2008. All rights reserved. ii DEDICATION To my parents, who raised me in a house filled with love and computers. To all of my friends over the years, who taught me that having fun is as important as working hard. And finally, to my husband, Travis, who kept me going with love and patience, even when it seemed as though I was facing the impossible. iii TABLE OF CONTENTS LIST OF TABLES . v LIST OF FIGURES . vi ABSTRACT . vii CHAPTER 1. OVERVIEW . 1 1.1 Introduction . 1 CHAPTER 2. REVIEW OF LITERATURE . 3 2.1 Background . 3 2.2 Related Work . 3 CHAPTER 3. LANGUAGE DESIGN . 5 3.1 Background . 5 3.2 Three Approaches . 5 3.2.1 The Single Annotation . 6 3.2.2 The Parameter Annotation . 6 3.2.3 The Clausal Annotation . 7 3.3 Discussion . 7 3.3.1 Consistency . 7 3.3.2 Readability . 8 3.3.3 Usability . 9 3.3.4 Extensibility . 9 3.3.5 Summary . 9 3.4 Use Cases . 10 3.4.1 Specification Definition Modifiers versus Specification Clauses . 10 3.4.2 Model Methods . 11 3.4.3 Specification Cases . 12 CHAPTER 4. IMPLEMENTATION . 14 4.1 Prework . 14 4.1.1 Resolving Syntax Tree Issues . 14 4.1.2 Writing Bytecode . 14 4.2 Adding an Additional Compiler Pass . 15 4.2.1 Resolving Annotation Types . 15 4.2.2 Walking the Abstract Syntax Tree . 15 4.3 Translating the Annotations . 16 4.3.1 Writing the Parser . 16 4.3.2 Integrating the Parsed Elements . 17 4.4 Conclusion . 19 iv CHAPTER 5. CASE STUDIES . 21 5.1 Two Small Test Cases . 21 5.2 One Large Test Case . 22 CHAPTER 6. USING JML ANNOTATIONS FOR VERIFICATION OF WEB-BASED DATAENTRY ......................................... 25 6.1 Abstract . 25 6.2 Introduction . 25 6.3 Significance . 25 6.4 Related Work . 26 6.5 Approach . 27 6.6 Results . 28 6.6.1 Checking Individual Properties . 28 6.6.2 Checking Properties on Form Submit . 29 6.7 Evaluation . 30 6.8 Conclusion . 31 CHAPTER 7. DISCUSSION AND SUMMARY . 33 7.1 Discussion . 33 7.1.1 Annotations as JML . 33 7.1.2 Annotations as Java Syntax . 34 7.2 Summary . 34 APPENDIX. ANNOTATION SYNTAX . 36 ACKNOWLEDGMENTS . 50 v LIST OF TABLES Table 7.1 JML2 vs. JML5 . 34 vi LIST OF FIGURES 1.1 JML2 Annotation Example . 2 1.2 JML5 Annotation Example . 2 3.1 Base Example . 5 3.2 Single Annotation Example . 6 3.3 Parameter Annotation Example . 6 3.4 Clausal Annotation Example . 7 3.5 Model and Invariant Base Example . 10 3.6 Model and Invariant Clausal Example . 10 3.7 Model Method Base Example . 11 3.8 Model Method Clausal Example . 12 3.9 Specification Case Base Example . 12 3.10 Specification Case Clausal Example . 13 3.11 Specification Case Clausal Example (with strings) . 13 4.1 Partial Package Resolution . 15 4.2 Annotation Accessor in JMethodDeclaration.java . 15 4.3 Grammar for jmlRequiresClause[] .................... 16 4.4 Partial JML Grammar . 16 4.5 Partial JML5 Grammar . 16 4.6 Grammar for jmlRepresentsClause[] ................... 17 4.7 Parser Method . 17 4.8 JMethodDeclaration.java . 18 4.9 JmlMethodDeclaration.java . 19 4.10 Visiting a JmlMethodDeclaration ..................... 20 5.1 MarkerAnnotations.java . 21 5.2 SingleElementAnnotations.java . 22 5.3 AAList.java . 23 5.4 AAList Incorrect Constructor . 24 6.1 Tapestry Template Example . 27 6.2 Tapestry Java Example . 28 6.3 Tapestry Web Form Example . 29 6.4 Transformed setMass(..) Method . 29 6.5 Tapestry Error Form . 30 6.6 Multiple Property Web Form . 31 6.7 Transformed onSubmitFromNewDose(..) Method . 32 6.8 Tapestry Error Form.