Home

Contract Wizard II - ETH E-Collection

image

Contents

1. 95 96
2. do Result Reference Function Actual argument list Function Effected function Deferred function Table 48 Definition of a Contract Wizard function The two tables Table 49 and Table 50 below show valid productions of Contract Wizard proxy class functions The first table depicts a function representing an effected NET function The second table shows a function representing an abstract NET function frozen add a new withdrawal SYSTEM OBJECT INTEGER is do Result withdrawal list add a new withdrawal end Table 49 Example of an effected Contract Wizard function add a new withdrawal SYSTEM OBJECT INTEGER is deferred end Table 50 Example of a deferred Contract Wizard function 8 9 Feature specification Before we introduce Contract Wizard types we have to specify features Features see Table 51 below A feature Feature is a creation procedure an attribute a procedure or a function For better readability we don t specify feature clauses Table 51 on next page shows the specifications of features 82 Features Creation_procedure Attribute Procedure Function Features Feature Features Table 51 Definition of a Contract Wizard feature 8 10 Class specification Now we are ready to specify Contract Wizard proxy classes A Contract Wizard proxy class can act as a surrogate for the following NET con
3. 270 5 112s Parsing XML NET Reflection Figure 22 Performance test I also tested how long it takes to add contracts using the Contract Handler Contract Wizard requires in average less than 1 minute to add 5000 contracts preconditions postconditions and invariants to mscorlib on the same system We have tested adding contracts on randomly selected classes and features of mscorlib In the worst case there are O n m computations to add a precondition and O n computations to add an invariant using the Contract Handler to the symbol DS ast where n stands for the number of classes and m for the number of features 87 88 Part D Theoretical part 11 Storing the contracts The current implementation of Contract Wizard stores contracts in an XML file This has some advantages but also some drawbacks Let us review them now 11 1 Storing contracts in an XML file Advantages e XML files can easily be manipulated manually This makes it possible to change contracts directly in the XML files e XML files can be read by humans which can help to debug the Contract Wizard proxy classes e XML files are more flexible against future changes of the NET framework e XML files also allow having more than one version of contracts for an assembly This may also be dangerous though Disadvantages e Consistency We have to keep track of two files the assembly and the XML representation 1
4. A Contract Wizard proxy class creation procedure consists of the keyword frozen followed by the creation procedure name make an optional formal argument list and a procedure body where the referenced NET class is instantiated The restriction frozen is required to prohibit programmers from redefining the creation procedure Table 41 shows the production of a Contract Wizard proxy class creation procedure Reference represents the references to the original NET class Reference ld Creation procedure frozen make Formal argument list is do create Reference make Actual argument list end Table 41 Definition of a Contract Wizard creation procedure Table 42 on next page shows a production of a valid creation procedure 79 frozen make INTEGER is do create deposit make a_sum end Table 42 Example of a Contract Wizard creation procedure 8 6 Attribute specification A Contract Wizard proxy class attribute represents a NET field The Contract Wizard attribute has a frozen keyword in front of the attribute name prohibiting users from redefining it Table 43 below specifies a Contract Wizard proxy class attribute Reference references the original NET class Attribute_name Id Attribute frozen Attribute name Type is do Result Reference Attribute_name end Table 43 Defi
5. end tag handles events fired when an XML end tag has been processed 5 3 5 Parse XML A closer look at a Contract Wizard XML start tag shows that there is enough information stored in it to create a Contract Wizard ast node see Table 27 The parser creates a Contract Wizard ast node for each XML start tag using the feature on start tag finish see Table 28 below 57 The specific type CW_PROCEDURE CW_FUNCTION and CW_ATTRIBUTE of the Contract Wizard ast node depends on the name of the XML start tag In Table 28 below you see the implementation of the feature on_start_tag_finish The XML node represented by cw_current is queried in the if statements Depending on the name of the XML node the following features are called generate_type generate_procedure generate_precondition on_start_tag_finishis Generate Contract Wizard ast nodes do if is_type cw_current then generate_type elseif is attribute cw current then generate attribute elseif js creation procedure cw current then generate creation procedure elseif is procedure cw current then generate procedure elseif is function cw current then generate function elseif js feature type cw current then generate return type elseif is argument cw current then generate argument elseif is precondition cw current then generate precondition elseif is postcondition cw current then generate posicondition elseif invariant cw current
6. Implementation frozen hello ref HELLO 1 Reference to the NET class end Table 33 Deferred proxy class representing the CLS compliant NET class Hello The original NET class is an interface If the original NET class is an interface the corresponding Contract Wizard proxy class is deferred Eiffel does not support the notion of interface Although the Contract Wizard proxy class inherits from SYSTEM OBJECT the features to string equals and get hash code have not to be redefined in SYSTEM OBJECT since NET interface does not have any effected features by definition 68 If interface specifies one of these features the Contract Wizard proxy class undefines them in SYSTEM OBJECT as well as all interfaces the Contract Wizard proxy class inherits from see example in Table 34 below Example The interface CW_IMEMBERHSIP_CONDITION Table 34 represents the interface IMEMBERSHIP_CONDITION from the NET core library mscorlib 14 The interface specifies the VET members ToString and Equals The corresponding Contract Wizard proxy class undefines these features in all interfaces 1 1t inherits from These features are also undefined in the class SYSTEM OBJECT 2 indexing note Automatically generated by the Contract Wizard dotnet name System Security Policy IMembershipCondition deferred class CW CONDITION inherit ISECURITY ENCODABLE 3 undefine 7 equ
7. equals to string end SYSTEM OBJECT redefine 5 get hash code equals to string end 66 feature Implementation frozen all membership condition ref ALL MEMBERSHIP CONDITION Reference to the NET class end Table 31 Fragment of an effected proxy class implementing NET interfaces The original NET class is an abstract class From the implementation point of view there is no difference between effected and deferred Contract Wizard proxy classes concerning redefinition e Both the effected and the abstract Contract Wizard proxy class inherit from SYSTEM OBJECT e It is not allowed in NET to specify one of the three features string equals or get hash code as abstract in NET there is no construct undefine like in Eiffel e This means we can handle the three effected features to string equals and get hash code in the same way we handled them in the effected class Overriding As in the effected NET class a programmer can override one of the three features to string get hash code and equals also in the abstract NET class Overriding has no effect on the current implementation of the Contract Wizard proxy class If a client calls an overridden feature the call is delegated by the Contract Wizard proxy class to the overridden feature of the NET class In Table 32 below you see an example of CLS compliant NET class written in C and in Table 33 there is the correspondin
8. then generate invariant end Precursor XM CALLBACKS FILTER end Table 28 Feature on start tag finish Composing Contract Wizard ast nodes In the previous section I discussed how to create Contract Wizard ast nodes The next step 15 to compose them All Contract Wizard ast nodes defined between an XML start tag and an XML end tag have to be composed For example if the XML tag represents a Contract Wizard type the parser assigns all Contract Wizard features defined between the XML start tag cw type and the XML end tag cw type to this Contract Wizard type 58 The feature tag see Table 29 below is responsible for composing the Contract Wizard ast nodes 1 2 3 It is called by the parser whenever an XML end tag has been processed on end lag a namespace STRING a prefix STRING a local part STRING is Compose Contract Wizard AST nodes local upper part STRING do upper part a local part as upper if js type upper part then compose type 7 extend ast elseif js creation procedure upper part then compose routine cw creation procedure 2 elseif is procedure upper part then compose routine cw procedure 3 elseif is function upper part then compose routine cw function 4 cw function set return type return type elseif is attribute upper part then compose attribute 5 elseif is argument upper part then compose argument 6 end Precursor XM CALL
9. valid file implies parser Void eiffel parser is correct valid file implies parser is correct end Table 26 Feature parse The feature parse see Table 26 and Figure 15 above creates a file 1 creates the XML parser EIFFEL PARSER 2 and instantiates the events filter XML FILTER 3 The feature set callbacks registers the event filter CW XML FILTER to the XML parser XM EIFFEL PARSER 4 55 5 3 1 The parsing process The feature parse_from_stream 5 initiates the parsing process The parser reads the XML stream sequentially and throws events on XML tags They are handled by the registered event filter CW_XML_FILTER described in the next chapter 5 3 2 The event filter The event filter consists of the following classes CW XML FILTER XML CONSTANTS and CALLBACKS FILTER see Figure 16 below The class CW XML FILTER The class CW XML FILTER redefines the following features from class XM CALLBACKS FILTER on start tag on attribute start tag finish and on end tag in order to add new functionality The class XM CALL BACK FILTER The class XM CALL BACK FILTER makes the class XML FILTER acting as an event handler The class CW XML CONSTANTS The class CW XML CONSTANTS provides the CW XML FILTER class with constants GD Effective concrete class Inherits from Figure 16 Class hierarchy of the event filter 56 5 3 3 Cooperati
10. 15 refined in the later milestones Implementation of the Parser Name Resolver and the M2 Parser symbol DS Design of the DTD and implementation of the Contract M3 Contract Reader Reader based on this DTD Contract Handler Implementation of the Contract Handler M5 Code Generator Implementation of the Code Generator consisting of three parts e XML Writer e Ace File producer e Proxy Class generator Implementation of a class calling the Eiffel Compiler M6 Eiffel Compiler M7 Basic GUI Basic GUI only for demonstration purposes Implementation of a demo that illustrates the capabilities M8 Demo of the tool Writing of the intermediary report Presentation of the M9 Intermediary Report intermediary results in a group meeting M10 Developer Report Writing of the developer report M11 Thesis Report Writing of the thesis report including the user documentation the developer documentation test case documentation a theoretical part discussing the possibility to store the contracts in the assembly metadata and a project review Presentation of the project results in a group meeting Table 3 Milestones 13 1 5 Deadlines Table 4 below shows the dates of the scheduled deadlines at the beginning of the project and the dates when the milestones have been handled As you can see spent much more time in milestone M2 Parser and M3 Contract Reader it turned ou
11. Contract Wizard type TYPE from the field type and sets it to the Contract Wizard attribute 3 It also sets the features is static and 15 constant depending on the parsed field 4 The postcondition ensures that the features ATTRIBUTE are set 5 4 4 8 The feature creation procedure cw creation procedure a constructor CONSTRUCTOR INFO CREATION PROCEDURE is 7 Contract Wizard creation procedure from NET constructor constructor require a constructor not void a constructor Void local a procedure CW PROCEDURE do a procedure cw procedure from method base a constructor create Hesult make from procedure a procedure ensure result not void Result Void result is not static ResulL is static False result is not deferred Result is deferred False end Table 18 Feature cw creation procedure Generating a Contract Wizard creation procedure The feature cw creation procedure creates a Contract Wizard creation procedure CREATION PROCEDURE from a NET constructor 1 The creation procedure must be neither static nor deferred It is guaranteed by the postconditions result is not static and result is deferred 2 42 4 5 The parser CW_PARSER ast ast factory parse parse_type inspect_constructors inspect_methods inspect_fields ast_factory assembly parse parse type inspect contructors inspe
12. Elements of Reusable Object Oriented Software Addison Wesley 1995 Nancy Kotary Colleen Gorman Ellie Volckhausen David Futato Framework Essentials O reilly 2002 Christof Marti Automatic Contract Extraction Developing a CIL parser http se inf ethz ch projects christof_marti consulted in September 2003 Bertrand Meyer NET Training Course Prentice Hall 2001 Bertrand Meyer Object Oriented Software Construction 2nd edition Prentice Hall 1997 Bertrand Meyer The start of an Eiffel standard In Journal of Object Technology Vol 1 No 2 July August 2002 95 99 http www jot fm issues issue 2002 07 column8 pdf Acronymfinder com http www acronymfinder com consulted in June 2003 ECMA Standard http www ecma international org consulted in June 2003 Eiffel Software http www eiffel com consulted in June 2003 Gobo Eiffel http www gobosoft com eiffel gobo consulted in June 2003 The Expat XML Parser http expat sourceforge net consulted in June 2003 The Microsoft NET library API http msdn microsoft com library default asp url library en us netstart html cpframeworkref start asp frame true consulted in June 2003 XML Spy http www xmlspy com consulted in June 2003 O Reilly Network C in a Nutshell_ Introducing C and the VET Framework Part 2 Apr 22 2002 Online at http www ondotnet com pub a dotnet excerpt csharpnut_1 index2 html consulted 21 06 2003
13. Implementation scope of the implementation Developer report This manual documents the software architecture and its limitations describes the difficulties encountered during the implementation explains how the software could be extended and contains a section discussing the test cases Intermediary report The intermediary report consists of the intermediary developer report User manual The user manual describes the usage of the tool Contract Wizard II Thesis report The thesis report consists of the final user manual the final developer report and a theoretical part discussing the possibility to store the contracts in the assembly metadata Validation Steps The validation for each milestone comprises Report Sending detailed report and the relevant parts of the work to the supervisor for review Meeting Organizing a meeting with the supervisor or presentation and discussion of the conducted work Revision Revision of parts or all of the work for this milestone depending on the conclusion of the supervisor 12 1 4 Plan with milestones 1 4 1 Project steps Table 3 below shows the milestones of this project Milestones Objectives M1 Software architecture Design of the entire Software Architecture This includes writing the core interfaces of the Parser the Name Resolver the Contract Reader the Contract Handler the Code Generator the symbol DS and the symbol DS with contracts The Software Architecture
14. These are the same interfaces as the original NET class ALL MEMBERSHIP CONDITIONS implements Handling interfaces Interface inheritance has some consequences for the Contract Wizard proxy class since interfaces are represented by deferred abstract classes in Eiffel for NET abstract classes also inherit from SYSTEM OBJECT the features to string get hash code and equals have 65 to be undefined to avoid ambiguous feature names These features are already inherited from SYSTEM_OBJECT 2 Interfaces force a programmer to implement defined features For example the NET interface IMEMBERSHIP_CONDITION 3 forces the programmer to implement among other features the features to_string and equals This does not affect the Contract Wizard proxy class except the implementation detail that the proxy class needs only undefine the feature get_hash_code 4 since the other two features are already defined by the interface The class SYSTEM_OBJECT has to redefine the three features to_string equals and get_hash_code to enable delegation of these three features indexing note Automatically generated by the Contract Wizard dotnet_name System Security Policy AllMembershipCondition class CW ALL MEMBERSHIP CONDITION inherit IMEMBERSHIP CONDITION 1 3 undefine 4 get hash code end ISECURITY ENCODABLE 1 undefine 2 get hash code equals to string end ISECURITY POLICY ENCODABLE 1 undefine 2 get hash code
15. all classes used to determine whether an assembly already has some contracts 3 2 10 The sub cluster parse parser This cluster contains the effected classes CW_XML_PARSER and CW_DOTNET_PARSER Both inherit from CW_PARSER The role of the class CW_XML_PARSER is to parse an XML document and to produce the symbol DS The role of the class CW_DOTNET_PARSER is to produce the symbol DS from a NET assembly using the reflection mechanism of NET In both cases we end up with a symbol DS The only difference is that in the first case CW_XML_PARSER the symbol DS contains contracts 3 2 11 The sub cluster parse support This cluster contains classes that provide facilities needed by other classes of the same parse cluster It contains in particular constant classes 3 2 12 The sub cluster visitor accessing the symbol DS There are two possibilities to access data stored in the symbol DS Either we access the data directly using the desired features e g a_type features name or we use a visitor The table below shows how to use a visitor In this example a type and its features are visited Then there are two assignments The first assignment retrieves a list of attributes the second assignment retrieves a list of procedures Both lists have been extended during the loop type visit visitor features type features from features start until features after loop features item visit visitor features forth end attributes visit
16. chapter starts by describing some parts of the NET Framework that are directly relevant to Contract Wizard II Then it explains the implementation of the parser that parses assemblies using the NET reflection capabilities 4 1 The NET Framework 4 1 1 NET Framework overview The NET Framework Figure 10 shows the architecture of the NET Framework The two main components in the NET Framework are the Common Language Runtime CLR and the NET Framework class library The CLR is a modern runtime environment that manages the execution of user code 16 The NET Framework class library is a library of classes providing access to system functionality and is designed to be the foundation on which NET Framework applications components and controls are built 14 The Common Type System CTS The Common Language Specification CLS and the Common Type System CTS are based on the NET Framework class library The Common Type System CTS is a shared type system The CTS defines the rules by which all types are declared defined and managed regardless of source language 16 The Common Language Specification CLS The Common Language Specification CLS is a subset of the CTS see Figure 11 that enables cross language integration The VET languages among them Eiffel for NET Visual Basic NET and are based on the CLR CTS CLS CTS Eiffel for Microsoft Framework N
17. classes representing the symbol DS factory classes to produce the symbol DS formatter classes responsible for translating names into Eiffel names incrementality classes indicating whether there are contracts for the specified NET assembly or not see also Sub cluster incrementality dealing with already contracted NET assemblies parser classes responsible for parsing VET assemblies and their corresponding XML files support helper classes providing functionalities used by other classes in other clusters visitor classes implementing the visitor pattern 3 The cluster generation The cluster generation has two sub clusters internal and dotnet The sub cluster internal contains the following classes 25 CW_LACE_GENERATOR This class generates an ACE file from the symbol DS CW_EIFFEL_GENERATOR This class generates the proxy classes from the symbol DS CW_XML_GENERATOR This class generates an XML file representing the symbol DS CW_GENERATOR The above classes have in common that they all need a symbol DS and a specific visitor which they inherit from the class CW_GENERATOR Code is produced by traversing the symbol DS simplify code generation I use the visitor pattern 3 The class CW_EIFFEL_VISITOR helps to produce Eiffel code from the symbol DS The class CW_XML_VISITOR helps to generate the XML representation of the symbol DS Both classes will be discussed thoroughly in the section Accessing the symbol
18. developed a completely new architecture for the tool Contract Wizard now called Contract Wizard IT including the following points Design of the software architecture Implementation of a parser and the corresponding data structure ast An XML parser and the corresponding XML data structure to store the Contract Wizard proxy classes containing contracts as well The XML parser also acts as the Contract Reader Contract handler for managing the contracts within the data structure ast Code generator which generates the Contract Wizard proxy classes their XML representation and the LACE file from the data structure ast Contract Wizard II works well I tested it on the VET core library mscorlib 14 Because of time limit I didn t implement a GUI but a command line application which provides simple access to the functionality of Contract Wizard see also Demo Application Future work A lot of interesting work can be done Implement support for inner classes and overloaded features Join automatic contract extraction 5 with Contract Wizard II Contract Wizard II offers a simple interface for insertion of contracts as comma separated values see chapter 2 Extend Contract Wizard II as a web service Implement a GUI 91 13 Acronyms Acronym Description NET Framework is a new computing platform that simplifies application development in the highly
19. implemented by the class CW_XML_PARSER and exposes the attribute ast of type LINKED_LIST like cw_type where cw_type represents the Contract Wizard type being parsed see Figure 15 above 5 2 3 The classes CW_XML_FILTER and XM_EIFFEL_PARSER The class CW_XML_FILTER acts as an event handler and the class XM EIFFEL PARSER acts as an event source XM_EIFFEL_PARSER 15 part of the Gobo library 54 5 2 4 The class CW_XML_PARSER The role of the class CW_XML_PARSER is to parse the XML representation of a Contract Wizard proxy class using the Eiffel parser XM_EIFFEL_PARSER and to generate the data structure ast from it The class CW_XML_PARSER implements the following features the initialization feature make and the feature parse see Figure 15 above The initialization procedure make initializes the file_name and creates an empty ast of type LINKED_LIST like cw_type The filename file_name represents the name of the XML file 5 3 Setting up the GOBO XML parser parse is Parse an XML file with name file_name containing the serialized proxy classes Create ast do create file make file_name 1 file open_read if not valid file then Error else create eiffel_parser make 2 create cw xml filter make ast 3 eiffel parser set callbacks cw xml filter 4 eiffel parser parse from stream a file 5 file close end ensure xml filter not void valid file implies cw xml filter Void eiffel parser not void
20. make a type name 2 Result set deferred a type is abstract or a type is interface 3 Result set expanded a type is value type Result set array a type is array Result set enum a type is enum Result set namespace a namespace 4 Add interfaces this type implements 5 ensure 6 result not void Result Void deferred set Result is deferred type is abstract or a type is interface array set Result is array a type is array enum set Result is enum a type is enum expanded set Hesult is expanded a type is value type end Table 12 Feature cw type Generating a Contract Wizard type The feature cw type creates a Contract Wizard type CW TYPE from a NET type TYPE 1 The creation of a Contract Wizard type requires Contract Wizard type name CW NAME therefore it creates a Contract Wizard type name and generates a Contract Wizard type with it 2 To create proxy classes we have to distinguish between the following types A type can be an array an enumeration type an expanded type or a deferred type The feature cw type sets the following features for the created Contract Wizard type depending on the parsed NET type is deferred is expanded is array and is enum 3 The feature deferred is set to true if the parsed type is abstract is expanded is set to true if the parsed type is a value type is array is set to true if the parsed type is an array and is enum is set to true if
21. type by using features inspect constructors inspect fields inspect methods and creates a Contract Wizard type TYPE It adds the created Contract Wizard type to the ast e Feature inspect constructors This feature iterates through all constructors and calls the feature parse public constructor for every inspected constructor Feature inspect fields This feature iterates through all fields and calls the feature parse public field for every inspected field Feature inspect methods This feature iterates through all methods and calls the feature parse public function or parse public procedure for every method depending on the return type Featureparse public constructor It parses a constructor and generates a Contract Wizard creation procedure CREATION PROCEDURE It adds the created object to the Contract Wizard type TYPE Featureparse public field It parses a field and generates a Contract Wizard attribute CW ATTRIBUTE It adds the created object to the Contract Wizard type e Featureparse public function It parses a public function and produces Contract Wizard function FUNCTION It adds the created object to the Contract Wizard type CW TYPE 4 6 The NET parser CW DOTNET PARSER The class DOTNET PARSER gathers information needed to generate proxy classes It provides the following features make parse parse type inspect methods inspect fields inspect constructor
22. 1 2 Storing contracts in the assembly metadata Another solution would be to store contracts in the metadata associated with any NET assembly Let us discuss the pros and cons of such an approach Advantages e We do not need to generate XML file everything is in the same file which probably implies a gain in space The XML file representing the Contract Wizard proxy classes of the NET core library mscorlib dll requires 4 5 megabyte of space compared with mscorlib dll that requires about 2 megabyte of space e Because information is stored in just one file it makes the assembly self describing No problem with extra files somewhere on the disk e It also makes the process of adding contracts simpler for the programmer 89 Disadvantages e Maybe lower performance tests with the current version of Contract Wizard II have shown that it is faster to retrieve the symbol data structure ast from the XML representation versus getting it from the assembly using the reflection mechanism of see also chapter 10 However it would remain to be tested how fast it is to extract contracts from the metadata 11 3 Conclusion chose to store the contracts in an XML file representing the Contract Wizard proxy classes 1 think using XML makes Contract Wizard more flexible and extendible For example if you want to rewrite Contract Wizard for other programming languages you can reuse the same XML files 90 12 Conclusion I
23. ATTER is responsible for this It implements the following features eiffel_formatted_name eiffel_formatted_keyword_name eiffel formatted array name and eiffel formatted primitive type name see Figure 14 above I now describe them in more detail Feature eiffel formatted name By convention feature names in Eiffel use all lower case characters and like class names words are separated by underscore 11 The feature eiffel formatted name translates CamelCase NET names into Eiffel names separated by underscore For example getName becomes get name e Feature eiffel formatted keyword name If the name corresponds to an Eiffel keyword an underscore is append to it For example the following VET member names feature is do become feature 7 is do All keywords are defined in the class CW_EIFFEL_KEYWORD_CONSTANTS e Feature eiffel formatted array name Arrays have the following format in type name Example string arrayName The commas stand for the dimension of the array If the array has dimension one there are no commas If array has dimension 1 and i is bigger than 1 there are 1 1 commas between the brackets This feature translates a NET array into an Eiffel array The class NATIVE ARRAY represents a NET array in Eiffel for NET Arrays are written as NATIVE ARRAY ARRAY TYPE independently of the dimension The above example is translated into NAT
24. BACKS a namespace a prefix a local part ensure then return type set js function a local part as upper implies cw function return type return type end Table 29 Feature on end tag 5 3 6 The document type definition The document type definition DTD describes the structure of an XML file and validates it The document type definition in Table 30 below validates every Contract Wizard XML file You can use the DTD with various tools to edit XML files such as xmlspy 15 lt xml versionz 1 0 encoding utf 8 gt lt ELEMENT cw ast cw_type gt lt ELEMENT cw type interfaces cw_creation_procedures cw_attributes cw_procedures cw_functions cw_invariants gt lt ELEMENT cw creation procedures cw_creation_procedure gt lt ELEMENT cw attributes cw attribute 39 lt ELEMENT cw procedures cw_procedure gt lt ELEMENT cw_functions cw_function gt lt ELEMENT cw_preconditions cw_precondition gt lt ELEMENT cw postconditions cw_postcondition gt lt ELEMENT cw_invariants cw_invariant gt lt ELEMENT interfaces interface gt lt ELEMENT cw_arguments cw_argument gt lt ATTLIST cw_type dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED namespace CDATA REQUIRED is deferred yes no REQUIRED is expanded yes no REQUIRED is enum yes no REQUIRED is interface yes no REQUIRED gt lt ELEMENT cw_creation_proced
25. CEDURE inherit from ROUTINE A creation procedure is a special procedure That is why CREATION PROCEDURE inherits from CW PROCEDURE 28 All preconditions CW_PRECONDITION postconditions CW_POSTCONDITION and invariants CW_INVARIANT have an assertion tag and an assertion expression in common They inherit the assertion tag and the assertion expression from CW_ASSERTION Deferred abstract class GD Effective concrete class Inheritance link Figure 9 AST used by the Contract Wizard 3 2 7 The sub cluster parse factory producing the symbol DS I introduce a class CW_FACTORY with its two variants CW_AST_FACTORY and CW_DOTNET_FACTORY to make the creation of AST nodes simpler This corresponds to the Abstract Factory pattern described in 3 Ast nodes can only be created by the CW_AST_FACTORY 3 2 8 The sub cluster parse formatter translating names into Eiffel names This cluster contains the class CW_NAME_FORMATTER which translates NET names into Eiffel names 29 3 2 9 Sub cluster incrementality dealing with already contracted assemblies If the user has added contracts to NET assembly there is an XML file containing all information about the generated proxy classes and their contracts In this case the Contract Wizard gets the symbol DS from the XML file Otherwise it retrieves the symbol DS from the NET assembly The cluster incrementality contains
26. Contract Wizard II Diploma Thesis Dominik Wotruba Supervised by Prof Dr Bertrand Meyer and Karine Arnout ETH Z rich October 6 2003 Acknowledgements Let me express my thanks to the people who made this diploma thesis possible First of all I want to thank Karine Arnout for her supervision her scientific support and her very helpful comments on this diploma thesis I thank Prof Dr Bertrand Meyer for his consultations and supervision would also like to express my thanks to Hans Dubach for his great help and support in dealing with administrational questions Last but not least I am very grateful to my parents for their support in every respect Abstract This diploma thesis is part of a general effort to improve existing components by adding contracts a posteriori In this diploma thesis I focus on components NET does not support Design by Contract For this reason a tool called Contract Wizard which allows adding contracts to every NET assembly whatever NET language it was originally written in was developed by Karine Arnout 2 The Contract Wizard was written for a beta version of NET The goal of this project was to rewrite this tool for the latest version of Contract Wizard II reads a contract less NET assembly and explicit contracts provided separately The tool merges this information automatically generates Eiffel proxy classes containing the contracts and compiles them using t
27. DS The sub cluster dotnet includes the following classes CW_COMPILER_LAUNCHER This class launches the compiler and reports the user interface with possible compilation error messages CW_EDITOR_LAUNCHER This class launches an external editor 3 2 The sub cluster parse ast 3 2 1 Contract Wizard type CW_TYPE The symbol DS contains the data to produce the Eiffel proxy classes and their XML representation The symbol DS is a list of elements of type CW_TYPE An instance of CW TYPE has features CW_FEATURE interfaces CW_INTERFACE and invariants CW_INVARIANT where a feature can either be a routine CW_ROUTINE or an attribute CW_ATTRIBUTE see Figure 7 below 26 features LINKED LIST return_type invariants interfaces LINKED_LIST e Deferred abstract class Effective concrete class Inheritance link gt Supplier link Figure 7 CW_TYPE and its features interfaces and invariants 3 2 2 Contract Wizard Interface CW_INTERFACE As shown on Figure 7 CW_INTERFACE inherits from CW_TYPE CW_INTERFACE has an additional access feature special names LINKED LIST This feature returns the interface feature names which has to be undefined in the proxy class later see also Chapter 7 The reason I use CW INTERFACE instead of CW TYPE is the feature special names that facilitates the creation and the parsing of the XML representation of the proxy classe
28. ET base class library CLR Figure 10 Architecture of the NET Framework 4 1 2 The Common Language Specification CLS 33 CLR CTS lt gt Figure 11 Relations between the CLR CTS and the CLS Ideal would be cross language integration based on the CTS but the CTS is too large for most non OO languages That s why the CLS a subset of the CTS see Figure 11 has been introduced The CLS is part of the ECMA standard 10 Every language can be a CLS consumer a CLS extender or a CLS producer e A language is a CLS consumer if it can consume any CLS compliant type e A language is a CLS extender if it can extend any CLS compliant type e A language is a CLS producer if it produces CLS compliant code Eiffel for NET is a CLS consumer a CLS extender and a CLS producer 4 1 3 Contract Wizard II and the NET Framework Contract Wizard 15 based on cross language interoperability The generated proxy classes are in Eiffel for The resulting VET assembly obtained by compiling the generated Eiffel for proxy classes can be used from any NET language Since only CLS compliant code guarantees cross language interoperability among all languages it is required that the original VET assembly and the proxy classes are CLS compliant 4 2 NET parser class overview To parse NET assembly I implemented the following classes e CW PARSER A class providing common fe
29. Framework see Figure 6 below 23 Visual Studio Command Prompt MyPro jects m example Accout ing_Example C _sources_for_Account dll gt gacutil i ficcount dll icrosoft NET Global Assembly Cache Utility Version 1 0 32705 0 opyright Microsoft Corporation 1998 2801 All rights reserved ssembly successfully added to the cache NMyProjectsNmn7Nexample Mficcouting Example CH sources for ficcount d11 Figure 6 Adding an assembly into the Global Assembly Cache 24 Part C Developer manual 3 Software architecture In this chapter give a rough overview of the software architecture of the Contract Wizard First I describe briefly the cluster hierarchy of the system Second I focus on the AST sub cluster and explain it in more details 3 1 Cluster overview 3 1 1 The root_cluster Contract Wizard II consists of the following clusters 3 1 2 controller classes for handling the symbol DS and its contracts doc Contract Wizard documentation including this report example some example applications including the Accounting example presented before generation classes for code generation see also section The cluster generation parse classes for parser generation NET parser and XML parser see also section The sub cluster parse ast Ast stands for Abstract syntax tree Sub clusters of the cluster parse The cluster parse contains the following sub clusters 3 1 3 ast
30. IVE ARRAY STRING 49 Feature eiffel_formatted_primitive_type_name The NET Framework has the following CLS compliant primitive types byte int16 int32 int64 single double boolean char decimal intPtr and string The feature eiffel_formatted_primitive_type_name translates primitive CLS compliant NET types into Eiffel according to the following translation rule Byte gt INTEGER_8 Int16 gt INTEGER_16 Int32 2 INTEGER 64 INTEGEH 64 Single gt REAL Double DOUBLE Boolean 2BOOLEAN Char gt CHARACTER Decimal gt DECIMAL Object gt SYSTEM_OBJECT IntPtr gt POINTER String gt SYSTEM_STRING Table 24 Translation rule for primitive types Feature eiffel_formatted_defined_type_name Eiffel class names must be unique within a system There are VET class names that conflict with the class names in the EiffelBase library They are renamed according to the following translation rule Object gt SYSTEM_OBJECT String gt SYSTEM STRING Array 2 SYSTEM ARHAY Console gt SYSTEM CONSOLE DateTime gt SYSTEM DATE TIME Directory 5 SYSTEM DIRECTORY File gt SYSTEM FILE Queue gt SYSTEM QUEUE Random SYSTEM RANDOM SortedList 2 SYSTEM SORTED LIST Stack gt SYSTEM STACK Stream gt SYSTEM STREAM Table 25 Translation rule for types already defined in the EiffelBase library e Feature eiffel formatted creation procedure name The name of the constructor in is ctor This feature r
31. SYSTEM STREAM Array type NATIVE ARRAY Object type Primitive type Defined type 1 y Object_type ld Primitive_type CHARACTER 8 INTEGER 16 INTEGER INTEGER 64 POINTER REAL Type Object_type Primitive_type Array_type Defined_type Table 38 Definition of types Valid productions for Type are INTEGER 64 NATIVE ARRAY CLASS 8 3 Formal argument specification To express Eiffel routines we have to introduce formal arguments Table 39 specifies a formal argument list Formal argument ld Type Formal arguments Formal argument Formal argument 7 Formal arguments Formal argument list 4 Formal arguments 7 Table 39 Definition of a formal argument 78 A valid production for a formal argument list Formal_argument_list 15 STRING an_object SYSTEM_OBJECT 8 4 Actual argument specification Actual arguments are required for expressing Eiffel routine calls Table 40 below specifies an actual argument list Actual_argument Id dm Actual arguments Actual argument Actual argument Actual arguments Actual argument list 4 Actual arguments Table 40 Definition of actual arguments A valid production for an actual argument list Actual argument list is a name an object 8 5 Creation procedure specification
32. TION 77 8 2 iii doce 78 8 3 FORMAL ARGUMENT SPECIFICATION 1 hne 78 8 4 ACTUAL ARGUMENT SPECIFICATION 0 79 8 5 CREATION PROCEDURE SPECIFICATION 0 0 79 8 6 ATTRIBUTE 000 80 8 7 PROCEDURE SPECIFICATION 0 etes sene iier 80 8 8 FUNCTION SPECIFICATION 81 8 9 FEATURE SPECIFICATION 82 8 10 55 SPECTEICATION a EE e 83 9 CONTRACT 85 TO TEST CASES as 87 11 STORING THE 5 08 nn nn ann nn tet rei 89 11 1 STORING CONTRACTS IN AN 89 11 2 STORING CONTRACTS IN THE ASSEMBLY 89 11 3 CONCLUSION E nenn ees dake dee debet 90 CONCLUSION ee e 91 135 SACRONY MS ecd Mast odere a Andres Be oe eu pr 93 REEERENEES MR D 95 Introduction does not support Design by Contract For this reason a tool called Contract Wizard which allows adding contracts to every NET assembly whatever VET language it was o
33. VERVIEW 0 34 4 3 THE CLASS CW_AST_FACTORY 35 4 4 THE CLASS CW_DOTNET_AST_FACTORY 37 4 5 THE PARSER CW_PARSER uo das 43 4 6 THE NET PARSER CW_DOTNET_PARSER 44 4 7 TRANSLATING NAMES INTO EIFFEL NAMES 0000 49 4 8 SUMMARY li 51 5 THE XML PARSER CONTRACT 53 5 1 DESIGN OF THE XML PARSER 0 53 5 2 STRUCTURE OF THE XML 0020001 2 54 5 3 SETTING UP THE GOBO XML PARSER 0 0 6 55 6 CONTRACT WIZARD PROXY 58 6 63 6 1 THE CONTRACT WIZARD PROXY 58 4 0000000 0 0 000 63 7 GENERATING CONTRACT WIZARD PROXY 585 5 71 7 1 THE EIFFEL CODE 0 0 0 71 Ta THE XML CODE GENERATOR 73 7 3 THE LACE CODE 000 0 2 2 2 0 stesse e 75 Ber PROXY CLASS SYNTAX a OS 77 8 1 IDENTIFIER SPECIFICA
34. _obj end frozen to_string SYSTEM_STRING is dotnet_name Account ToString String do Result account ref to string end frozen may withdraw a sum INTEGER BOOLEAN is dotnet name Account MayWithdraw Sum Int32 Boolean do Result account ref may withdraw a sum end frozen get type from original class TYPE is dotnet name Account GetType Type do Result account ref get type end feature Commands frozen deposit a sum INTEGER is dotnet name Account Deposit Sum Int32 do account ref deposit a sum end frozen withdraw a sum INTEGER is dotnet name Account Withdraw Sum Int32 require not too big a sum balance minimum balance 1 do account ref withdraw a sum end 21 feature NONE Implementation frozen account_ref ACCOUNT Reference to the NET class end Table 6 Proxy class CW_ACCOUNT with a precondition on feature withdraw 2 2 2 Incrementality To demonstrate incrementality we insert a second contract see Figure 5 below The invariant not_under_minimum balance gt minimum_balance states that the balance is always bigger than the minimum balance ca Visual Studio Command Prompt contract wizard create_from_assembly Account c cw_tmp append_contracts c2 csv MyProjects m EI FGEN W_code type c2 csu inv ACCOUNT not under minimum balance gt minimum balance MyPro jects m EI FGENWW_code gt
35. a_name_not_void a_name Void do create Result make eiffel name dotnet name ensure result not void Result Void end cw type name a name SYSTEM STRING CW NAME is 2 Type name with Eiffel name and NET name created from name require a name not void a Void do create Result make eiffel name dotnet name ensure result not void Result Void end Table 11 Features cw feature name and cw type name Generating a feature name and a type name Features cw type name and feature name create a Contract Wizard name NAME consisting of an Eiffel name and a NET name The reason why there are two features 1 2 for creating Contract Wizard names is performance NET types and NET members are translated differently into Eiffel 4 4 The class DOTNET AST FACTORY 4 41 Generating AST nodes for features and types The class DOTNET AST FACTORY implements the following core features cw type cw interface cw argument cw procedure cw function cw attribute and cw creation procedure see Figure 12 above They are described below 37 4 4 2 The feature cw type cw type a type TYPE CW TYPE is 1 Contract Wizard type AST node created from NET type a type require a type not void a type Void type name not void a type name Void local a type name CW NAME do a type name cw type name a type name to string create Result
36. abstraction e Core principles of OOSC2 7 e Command query separation o Simple interfaces o Uniform access o Information hiding o etc Style guidelines Correct and robust code Readability of source code Ease to use Quality of documentation e Completeness e Understandable documentation e Usefulness e Structure 1 3 3 Method of work The technologies involved are e The System Reflection library from the VET Framework 14 The System XML library from the VET Framework 14 Programming language Eiffel for NET 11 ISE Eiffel compiler beta version 5 4 for test purposes 11 The development tool is Eiffel Studio 5 3 11 11 I developed the software using the Eiffel style of design in close cooperation with the supervisor 1 3 4 Quality management Quality was ensured by 1 3 5 1 3 6 Weekly progress reports Short weekly progress reports sent to the supervisor Milestone progress reports Detailed reports for each milestone see plan with milestones below Review and Validation Review of each milestone by the supervisor concluded by a meeting Validation Validation of each milestone after review see validation steps below Testing Testing of the software by application of different test cases Documentation Progress reports o Short weekly progress reports consisting of the main tasks completed o Detailed reports for each milestone consisting of o The main tasks Eventual encountered difficulties o
37. act Wizard creation procedure CREATION PROCEDURE and appends it to the creation procedures list The code includes preconditions and postconditions e visit attribute This feature generates Eiffel code for a Contract Wizard attribute ATTRIBUTE and appends it to the attributes list visit procedure This feature generates Eiffel code for a Contract Wizard procedure CW PROCEDURE and appends it to the procedures list The code includes preconditions and postconditions visit function This feature generates Eiffel code for a Contract Wizard function CW FUNCTION and appends it to the functions list The code includes preconditions and postconditions e visit invariant This feature generates Eiffel code for a Contract Wizard invariant CW INVARIANT and appends it to the invariants list e visit type This feature generates Eiffel code for a Contract Wizard type TYPE using the features creation procedures attributes procedures functions and invariants These features are called indirectly by registering the Eiffel visitor to a Contract Wizard ast node A visitor is registered to a Contract Wizard ast node by the feature visit A visitor can be registered more then once There is an internal counter You can ask every Contract Wizard ast node in which state it is There are two states is visited and not is visited It can be very useful to distinguish between these two states Some pre processing can be done in the st
38. agment in Table 23 4 6 9 Parse procedures The feature parse public procedure parses a public procedure and generates a Contract Wizard procedure PROCEDURE The NET ast factory facilitates the creation of the Contract Wizard procedure It sets all required features is static is deferred and is property The generated function is added to the feature list of the Contract Wizard type The code resembles the code fragment in Table 23 4 6 10 Parse fields The feature parse field parses a public field and generates a Contract Wizard attribute CW ATTRIBUTE from it The NET ast factory facilitates the creation of a Contract Wizard attribute It sets all required features is static is constant and value The feature value is set only if the field is a constant The generated attribute is inserted to the feature list of the Contract Wizard type The code resembles the code fragment in Table 23 48 4 7 Translating NET names into Eiffel names eiffel formatted name formatted array name eiffel formatted primitive type name formatted defined type name eiffel formatted creation procedure name D Deferred abstract class f Effective implemented feature Figure 14 Name formatter Transforming NET names into Eiffel names Since Eiffel names and names differ slightly we need a class that translates NET names into Eiffel names The class CW_NAME_FORM
39. alance private void Add int Sum return Balance gt MinimumBalance Sum Balance Balance Sum 18 Account balance public int Balance Minimum balance static public int MinimumBalance 1000 List of deposits public DepositList Deposits List of withdrawals public WithdrawalList Withdrawals Table 5 C class Account cs In this example we add the precondition in Eiffel syntax not_too_big a_sum lt balance minimum_balance to feature withdraw of class Account The file c csv contains the contract not_too_big see Figure 4 below 2 2 1 Insertion of a contract We add the contract not_too_big using the command line application contract_wizard see Figure 4 below The first command create_from_assembly Account c cw_tmp specifies from which assembly we want to create the Contract Wizard proxy classes and where they have to be stored The second command append contracts cl csv specifies that we would like to append new contracts stored in the file c csv Visual Studio Command Prompt contract wizard create from assembly Account c cw_tmp append contracts c1 csv a x MyProjects m 7 EI FGEN W_code gt type ci csy E re ACCOUNT withdraw not too big a sum lt balance minimum balance MyProjects m EI FGEN W_code gt contract_wizard create from assembly Account c cw_tmp contracts iu 4 Figure 4 Insertion of a cont
40. als to string end ISECURITY POLICY ENCODABLE 3 undefine 7 equals to string end SYSTEM OBJECT undefine 2 equals to string end feature Query equals a obj SYSTEM OBJECT BOOLEAN is 1 dotnet name IMembershipCondition Equals obj Object Boolean deferred end 69 to_string SYSTEM_STRING is 7 dotnet name IMembershipCondition ToString String deferred end end Table 34 Proxy class corresponding to the NET interface IMEMBERSHIP_CONDITION 70 7 Generating Contract Wizard proxy classes 7 1 The Eiffel code generator In this section we have a closer look at the implementation of the Eiffel code generator see Figure 19 below The role of the Eiffel code generator is to generate the Contract Wizard proxy classes and to store them into the specified directory source creation_procedures attributes procedures functions invariants visit_type visit_creation_procedure visit_attribute generate_source visit_procedure generate generate_source store_source visit_type generate_and_store_class visit_creation_procedure visit_attribute visit_procedure visit_function CD Deferred abstract class visit_argument visit_precondition Effective concrete class visit_postconditon visit_invariant Inherits from gt Client relation f Effective implemented feat
41. ame resolver tentative M2 Name resolver real M3 Contract Reader tentative M3 Contract Reader real 4 Contract Handler tentative 4 Contract Handler real M5 Code generator tentative M5 Code generator real M6 Eiffel Compiler tentative M6 Eiffel Compiler real M7 Basic GUI tentative M7 UI real 8 Demo tentative M8 Demo real Documentation M9 Intermediary report tentative 9 Intermediary report real 10 Developer report tentative M10 Developer report real M11 Thesis report tentative M11 Thesis report real Testing Test cases tentative Test cases real Optimization optional parts tentative Optimization optional parts real co Cn Cn Figure 2 Tentative and real project schedule 15 2003 07 14 Mo 8 2003 09 01 t 2003 09 08 Mo 2003 09 15 Mo t 2003 09 22 t 2003 09 29 Mo 3 2003 10 06 Mo 5 2003 10 13 Mo 5 2003 10 20 Mo 16 Part User manual 2 Demo Application 2 1 Contract Wizard commands Figure 3 shows the commands provided by Contract Wizard ca Visual Studio Command Prompt F x C MyPro jects m 7 EI FGEN WW_code gt contract_wizard help Usage contract_wizard help i version i remove_contracts Partial assembly name i edit contracts Partial assembly name External editor i create from assembly Partial assembly name Target directo
42. ate not visited Example of how to use visitors Table 35 on the next page shows an example how Contract Wizard proxy classes are generated using a visitor Note that the visitor must be of type CW EIFFEL VISITOR to generate Eiffel code The visitor is registered twice to the Contract Wizard type TYPE 1 2 and once to every feature FEATURE 3 After registering the visitor to all Contract Wizard ast nodes the Contract Wizard Eiffel code can be retrieved using the query visitor type text 72 visit_class a_type CW_TYPE is Visit a_type require type_not_void a_type Void visitor_not_void visitor Void local a feature features LINKED_LIST CW_FEATURE do a type visit visitor 7 features a_type features from features start until features after loop a_feature features item visit_feature a_feature 3 features forth end features a_type features visit_assertions a_type invariants a_type visit 2 end Table 35 Registering a visitor to Contract Wizard ast nodes Note 1f you want to generate an XML representation of a Contract Wizard proxy class instead of the Eiffel representation you must use the XML visitor CW_XML_VISITOR instead of the Eiffel visitor CW EIFFEL VISITOR 7 2 The XML code generator The XML code generator resembles the Eiffel code generator in many ways 73 source creation_procedures attributes proced
43. atures for the NET parser DOTNET PARSER and the XML parser XML PARSER e DOTNET PARSER A class implementing the NET parser which populates the symbol DS by retrieving information from the NET assembly given as input using the reflection capabilities of NET XML PARSER class implementing the XML parser which reads an XML representation of the symbol DS that may contain contracts and builds the corresponding AST to be used by the Contract Handler 34 e CW_AST_FACTORY class providing common features for the class CW_DOTNET_AST_FACTORY and the class CW_XML_AST_FACTORY e CW_DOTNET_AST_FACTORY class responsible for creating AST nodes CW_TYPE CW_PROCEDURE CW_ATTRIBUTE NET members METHOD_INFO FIELD_INFO and NET types TYPE CW_NAME_FORMATTER A class responsible for translating member names and NET type names into Eiffel feature names and Eiffel type names CW DOTNET KEYWORD CONSTANTS A class defining keywords CW DOTNET TYPE CONSTANTS A class defining NET type constants CW EIFFEL KEYWORD CONSTANTS A class defining Eiffel keywords PUNCTUATION CONSTANTS A class defining punctuation constants 4 3 The class CW AST FACTORY 4 3 1 Generating AST nodes for contracts feature and type names cw feature name cw type name cw precondition cw postcondition 4 cw invariant cw lype cw interface cw argumen
44. contract_wizard from assembly Account c cw_tmp append contracts c2 csu Figure 5 Adding a new contract Table 6 shows a fragment of the modified proxy class CW ACCOUNT The class contains the inserted contracts too big 1 and not under minimum balance 2 Indexing note Automatically generated by the Contract Wizard dotnet name Accounting Account class CW ACCOUNT inherit SYSTEM OBJECT redefine get hash code equals to string end create make 22 feature NONE Initialization frozen make initial amount INTEGER is dotnet name Account ctor InitialAmount Int32 do create account ref make a initial amount end feature Commands frozen deposit a sum INTEGER is dotnet name Account Deposit Sum Int32 do account ref deposit a sum end frozen withdraw a sum INTEGER is dotnet name Account Withdraw Sum Int32 require not too big a sum balance minimum balance 1 do account ref withdraw a sum end feature NONE Implementation frozen account ref ACCOUNT Reference to the NET class invariant not under minimum balance minimum balance 2 end Table 7 Proxy class CW ACCOUNT with an invariant 2 2 3 Adding assemblies to the Global Assembly Cache Contract Wizard reads assemblies from the Global Assembly Cache To add an assembly into the GAC use the gacutil tool delivered with the NET
45. ct methods inspect fields Deferred abstract class f Effective implemented feature C D Effective concrete class f Redefined feature Inherits from f Deferred abstract feature Figure 13 Parser hierarchy The parser consists of the following classes CW_PARSER CW_DOTNET_PARSER and CW_XML_PARSER see Figure 13 The class CW_PARSER defines and partly implements the features common for CW_XML_PARSER and CW_DOTNET_PARSER The class CW_DOTNET_PARSER parses a CLS compliant NET assembly and generates the data structure ast of type LINKED LIST AST_NODE The data structure ast contains all required information to generate the proxy classes The class CW_XML_PARSER parses the XML representation of the data structure ast and generates the object representation of the data structure ast The class CW_PARSER implements the following attributes ast and ast_factory see Figure 13 The data structures ast and ast_factory are described above The class CW_PARSER defines the following deferred features parse parse_type inspect_constructors inspect_fields inspect_methods parse_public_constructor parse_public_field and parse_public_function see Figure 13 will now describe them in more details 43 e Feature parse It parses all types by calling the feature parse_type for every type and generates the data structure ast of type LINKED LIST TYPE e Feature parse type It parses a
46. distributed environment of the Internet 11 INET The NET Framework has two main components the common language runtime and the NET Framework class library The common language runtime is the foundation of the NET Framework 11 ACE Assembly of Classes in Eiffel AST Abstract syntax tree CLR Common Language Runtime 14 CLS Common Language Specification 14 COM Component Object Model CTS Common Type System 14 DS Data structure DTD Document Type Definition 9 GAC Global Assembly Cache 14 GUI Graphical User Interface LACE Language for the Assembly of Classes in Eiffel Object Oriented Software Construction Second Edition the book OOSC2 by Bertrand Meyer is used as the primary programming and language reference for implementing the software of this project in Eiffel Symbol DS Symbol Data structure XML eXtendible Markup Language 9 Table 53 Acronyms 93 94 References 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Karine Arnout D veloppement de la technologie Eiffel dans l environnement NET de Microsoft Rapport de stage Jeune Ing nieur ENST Bretagne September 2001 Karine Arnout and Rapha l Simon The Contract Wizard Adding Design by Contract to languages other than Eiffel IEEE Computer Society TOOLS 39 Santa Barbara USA July 2001 p 14 23 Erich Gamma Richard Helm Ralph Johnson John Vlissides Design patterns
47. e following the client calls the feature do it of the Contract Wizard proxy class instead of calling the feature it of the original class directly The Contract Wizard proxy class delegates the call to the original NET class In this way each call is delegated to the original class Since the Contract Wizard proxy class specifies all public features of the NET class including inherited features from super classes of the NET class delegation to inherited features of the NET class is provided 6 1 1 Implementation of a Contract Wizard proxy class Figure 18 on the next page shows the implementation of a Contract Wizard proxy class CW PROXY Every Contract Wizard proxy class has a reference ref to the original NET class The reference is of type ORIGINAL DOTNET CLASS and delegates calls to the original NET class 63 do do_it_sup do_it do_it_sup do_it Deferred abstract class INTERFACE represents all interfaces Effective concrete class f Effective implemented feature Inherits from Client relation f Deferred feature Figure 18 Implementing a Contract Wizard proxy class CW_PROXY Both the Contract Wizard proxy class CW_PROXY and the original NET class ORIGINAL_DOTNET_CLASS implement inherit from the same interfaces The deferred class INTERFACE represents all interfaces That is why it is grey Thi
48. e parse public constructor for every inspected public constructor The Inspect constructors code resembles the code fragment in Table 22 47 4 6 7 Parse constructors parse_public_constructor is Parse a constructor_info Generate corresponding cw_creation_procedure Add cw_creation_procedure to cw_type do cw_creation_procedure ast_factory cw_creation_procedure constructor_info 7 cw_type add_feature cw_creation_procedure 2 end Table 23 Feature parse_public_constructor The feature parse public constructor parses a public NET constructor class CONSTRUCTOR INFO corresponding to the NET System Reflection ConstructorInfo and generates a Contract Wizard creation procedure CREATION PROCEDURE from it The NET ast factory DOTNET AST FACTORY facilitates the creation of the Contract Wizard creation procedure CREATION PROCEDURE 1 It sets all required features is static and is deferred The generated creation procedure is added to the feature list of the Contract Wizard type CW TYPE 2 4 6 8 Parse functions The feature parse public function parses a public function and generates a Contract Wizard function FUNCTION The NET ast factory facilitates the creation of the Contract Wizard function It sets all required features is static and is deferred The generated function is inserted to the feature list of the Contract Wizard type The code resembles the code fr
49. eature is property 3 The feature is property is set to true if the parsed function is an accessor method for example get property The postcondition of cw function ensures that features of FUNCTION are set 4 4 4 7 The feature cw attribute cw attribute a field FIELD INFO CW ATTRIBUTE is 7 Contract Wizard attribute from NET field a field require a field not void a field Void a field name not void a field name Void local an attribute name CW NAME a type CW TYPE a value STRING do an attribute name cw feature name a field name to string create Hesult make an attribute name 2 a type cw type a field field type Result set type a type 3 Result set static a field is static 4 Result set constant a field is literal 41 ifa field is literal then create value make from field get value a field reflected type to string Result set value a value end ensure 5 result not void Result Void static set ResultLis static a field is static constant set Result is constant a field is literal constant value set Result is constant implies Result value Void end Table 17 Feature cw attribute Generating a Contract Wizard attribute The feature cw attribute creates a Contract Wizard attribute ATTRIBUTE from NET field 1 First it creates the Contract Wizard attribute with the attribute name CW NAME 2 Then it creates a
50. ementation full Eiffel for NET makes it possible to turn Contract Wizard II into a Web service later I implemented the tool considering future Eiffel language extension like contracts for attributes Furthermore all inserted contracts are stored in XML This has the advantage that for example tools for automatic contract extraction 5 can easily be incorporated The work consists of the following parts see Figure 1 on next page o NET 4 Name symbol contracts assembly Faser Resolver DS in XML O symbol el DS with file compiler assembly contracts Proxy classes with user contracts contracts Contract DS Error in contracts If specific metadata retrieve XML files B 2 Contract s Reader contracts in XML Figure 1 Software architecture 1 If there is no specific metadata the parser parses the NET assembly and produces a symbol DS Data Structure The Name Resolver keeps track of the renaming from NET names to Eiffel names 2 If there 15 specific metadata e g a path to the XML contract files in the VET assembly expressing that there are already contracts for it the Contract Reader reads the contracts from XML con
51. enames it to make 50 4 8 Summary 4 8 1 Characteristics of the NET parser The assembly being parsed has to be CLS compliant The parser parses the following information required to generate Eiffel proxy classes o public types class TYPE corresponding to System Type o public NET constructors class CONSTRUCTOR_INFO corresponding to NET System Reflection ConstructorInfo o public NET methods class METHOD_INFO corresponding to NET System Reflection MethodInfo o public NET fields class FIELD_INFO corresponding to NET System Reflection FieldInfo The parser generates the following Contract Wizard nodes o The parser generates a Contract Wizard type CW_TYPE and sets the following features for it is_deferred is_array is_expanded and is_enum name space Moreover it attaches all interfaces of type CW INTERFACE that the type implements o The parser generates a Contract Wizard attribute CW ATTRIBUTE and sets the following features for it is static is constant and if the parsed field is a constant it also sets the constant value o The parser generates a Contract Wizard procedure PROCEDURE and sets the following features for it is static is deferred o parser generates a Contract Wizard function FUNCTION and sets the following features for it is static is deferred and is property is set to true if the function is an accessor o The parser generates a Contract Wizard creation p
52. epicts a procedure representing an abstract NET procedure The second table shows a procedure representing an effected NET procedure deposit a sum INTEGER is deferred end Table 46 Example of a deferred Contract Wizard procedure frozen deposit a sum INTEGER is do account deposit a sum end Table 47 Example of an effected Contract Wizard procedure 8 8 Function specification A Contract Wizard proxy class function represents either a deferred abstract NET function or an effected implemented function If the Contract Wizard proxy class function represents an abstract NET function it consists of a function name followed optionally by a formal argument list followed by a colon a return type and a deferred function body The function name represents the name of the NET function The function type represents the type of the NET function 81 If the Contract Wizard proxy class function represents an effected NET function there is a frozen keyword in front of the function name prohibiting users from redefining it The delegated function call is assigned to the Result in the effected feature body Table 48 below specifies a Contract Wizard proxy class function Function_name ld Deferred_function Function_name Formal_argument_list Type is deferred Effected function frozen Function name Formal argument list Type is
53. erface EMPTY gt lt ATTLIST interface dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED to_string yes no REQUIRED equals yes no REQUIRED get_hash_code yes no REQUIRED gt lt ELEMENT type EMPTY gt lt ATTLIST type dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED is array yes no REQUIRED Table 30 The document type definition for the Contract Wizard proxy classes 61 6 Contract Wizard proxy classes This chapter explains how Contract Wizard creates the Eiffel representation and the XML representation of Contract Wizard proxy classes and examines the structure of a Contract Wizard proxy class 6 1 The Contract Wizard proxy class Contract Wizard generates a Contract Wizard proxy class for each public class or public interface in the specified NET assembly The Contract Wizard proxy class acts as a surrogate for the original NET class or NET interface To do this the proxy class has to specify the same interfaces and features as the original class Interactions between the Contract Wizard proxy class and the original NET class are as follow do it do it Contract Wizard proxy class client NET class Figure 17 Delegating calls to the original NET class In Figure 17 you see the interaction between a client a Contract Wizard proxy class and an original NET class The interaction is th
54. es part of the Eiffel NET Framework contain a variety of validating and non validating parsers decided to use the Eiffel parser The main reason 15 that the Eiffel parser unlike 165 competitors is written in pure Eiffel Moreover it is fast and simple to use Even if the Eiffel parser is a non validating parser I wrote a document type definition It can be very useful to test the generated XML files The XML files can be validated using external tools such as xmlspy 15 The Eiffel parser is an event parser It reads an XML stream sequentially and acts as an event source It throws events on XML start tags XML end tags and XML attributes The Eiffel parser is initiated by the class CW_XML_PARSER described below 53 5 2 Structure of the XML parser 5 2 1 Class hierarchy ast parse cw xml filter eiffel parser make parse Deferred abstract class f Effective implemented feature CD Effective concrete class f Deferred feature Inherits from gt Client relation Figure 15 Class hierarchy of the XML parser The parser consists mainly of the following classes CW_PARSER CW_XML_PARSER CW_XML_FILTER and EIFFEL see Figure 15 above XM_EIFFEL_PARSER 15 part of the Gobo library wrote the other classes The classes with prefix CW_ are part of the Contract Wizard 5 2 2 The class CW_PARSER The class CW_PARSER defines the feature parse
55. f class design 26 A sense of style 28 The software construction process e NET Training Course 5 e Rapport de stage Jeune Ing nieur 1 1 2 2 Software e Source code of Contract Wizard 1 3 Project management 1 3 1 Objectives and priorities Table 2 below shows the objectives and priorities of the different parts of the project One represents the highest priority and three the lowest priority The most important software parts are design of the software architecture the parser the contract reader the code generator and the test cases The most significant documentation part is the thesis report including the developer documentation Objective Priority Software architecture 1 Parser Contract Reader Contract Handler Code Generator Demo application Basic GUI Test cases Optimization User documentation Developer documentation Intermediary report Thesis report Table 2 Objectives and priorities NO 00 GO PN ea Ss ct 10 1 3 2 Criteria for success The focus on this project is quality The result may be a partial implementation of the intended results and objectives without implying any penalty on the success of the project Quality of software e Use of Design by Contract o Routine pre and postconditions o Class invariants o Loop variants and invariants e Careful design o Design Patterns o Extendibility o Reusability o Careful
56. fied by the Partial assembly name 2 2 Contract Wizard accounting example The Contract Wizard provides users with the possibility to add contracts to a CLS compliant NET assembly a posteriori The tool can be used incrementally meaning it is possible to add contracts to a NET assembly that has already been contracted using the Contract Wizard We 17 will explain how to add contracts to assembly on the accounting example delivered with this project In the directory example Accouting_Example CA_sources_for_Account dll you find the files Account dll cl csv the extension csv stands for comma separated values and c2 csv the latter two contain the contracts Table 5 below shows the class Account from assembly Account dll to which we want to add contracts using System assembly CLSCompliant true namespace Accounting public class Account Set up account with InitialAmount public Account int InitialAmount Balance InitialAmount Deposits new DepositList Withdrawals new WithdrawalList Deposit into the account public void Deposit int Sum Add Sum Deposits Add new Deposit Sum Withdraw from the account public void Withdraw int Sum Ada Sum Withdrawals Add new Withdrawal Sum Is it possible to withdraw from the account public bool MayWithdraw int Sum Add Sum to B
57. g Contract Wizard proxy class The feature ToString is overridden 1 the feature Equals and GetHashCode are inherited from System Object using System assembly AssemblyVersion 1 0 3300 1 assembly AssemblyKeyFile mykey snk assembly CLSCompliant true public abstract class Hello Overrides the method ToString from System Object override public string ToString 1 return Hello World Table 32 CLS compliant class Hello overriding the method ToString from System Object 67 Examining the corresponding Contract Wizard proxy class in Table 34 you see that overriding has no effect on the current implementation of a Contract Wizard proxy class The overridden method is called 2 since the reference is of type HELLO 1 Note it 15 not possible to call the method to_string from SYSTEM_OBJECT through the Contract Wizard proxy class in this case indexing note Automatically generated by the Contract Wizard dotnet_name Hello deferred class CW_HELLO feature Query frozen get hash code INTEGER is dotnet name Hello GetHashCode Int32 do Result hello ref get hash code end frozen equals a obj SYSTEM OBJECT BOOLEAN is dotnet name Hello Equals obj Object Boolean do Result hello ref equals a obj end frozen to string SYSTEM STRING is 2 dotnet name Hello ToString String do Result hello ref to string end feature NONE
58. he Eiffel compiler into a new NET assembly The tool provides incrementality if the assembly already contains explicit contracts the tool does not process the whole NET assembly again but takes advantage of an XML representation of the Eiffel proxy classes In this diploma thesis I explain how to use the tool Contract Wizard I discuss its design and give a precise description of its implementation Content SPROJECT PEAN aaa 7 1 1 PROJECT DESCRIPTION cccccssesesceccccceccesssssesccccsessusessssecccccsssusuussesceccssssuuuueasscesceseeaaes 7 1 2 BACKGROUND MATERIAL ccssssssccccccccsessssssccccceccesesssssccsccesseuesssescsccseeseueusessessceseseaes 10 1 3 PROJECT MANAGEMENT ci alada 10 1 4 PLAN WITH 5 8 13 1 5 DEADLINES art 14 1 6 TENTATIVE SCHEDULE 14 2 DEMO APPLICATION 0 ener 17 2 1 CONTRACT WIZARD COMMANDS 0 1 200 000 0 0600600 17 2 2 CONTRACT WIZARD ACCOUNTING 04040400000 00 00 17 3 SOFTWARE 25 3 1 CEUSTER OVERVIEW is 25 3 2 THE SUB CLUSTER 5 26 4 THE 6 33 4 1 THE NET FRAMEWORK 33 4 2 NET PARSER CLASS O
59. ion procedure Creates the XML representation of a Contract Wizard procedure CW_PROCEDURE including contracts The generated code is appended to the creation procedures list 74 visit attribute Creates the XML representation of a Contract Wizard attribute CW_ATTRIBUTE including contracts The generated code is appended to the attributes list visit function Creates the XML representation of a Contract Wizard function CW_FUNCTION including contracts The generated code is appended to the functions list visit procedure Creates the XML representation of a Contract Wizard procedure CW_PROCEDURE including contracts The generated code is appended to the procedures list visit invariant Creates the XML representation of a Contract Wizard invariant CW_INVARIANT including contracts The generated code is appended to the invariants list yisit type Creates the XML representation of a Contract Wizard type CW_TYPE making use of the features creation_procedures attributes procedures functions and invariants You can access the generated XML code using the query visitor source 7 3 The LACE code generator To compile the Contract Wizard proxy classes we need an ACE file The LACE generator CW_LACE_GENERATOR see below creates the appropriate ACE file written in LACE Language for Assembly of Classes in Eiffel and stores it into the specified directory generate_source generate ge
60. ly in the assembly s metadata instead of storing them in separate XML files as implemented Part A Project plan 1 Project plan 1 1 Project description 1 1 1 Overview Building reliable software is the aim of software engineering Assertions which are part of the Eiffel programming language provide the programmer with essential tools for expressing and validating correctness arguments The key concept is Design by Contract Viewing the relationship between a class its clients as a formal agreement expressing party s right and obligations is the only way one can hope to attain a significant degree of trust in large software systems 2 The current implementation of VET does not support Design by Contract yet The goal of this project is to develop a tool which provides the ability to add contracts like preconditions postconditions and invariants to an arbitrary VET assembly which will be a big enrichment for many programming languages based on NET 1 1 2 Scope of the work The main work of the diploma thesis is to implement a tool called Contract Wizard II There already exists a tool called Contract Wizard developed by Karine Arnout The main difference between Contract Wizard II and Contract Wizard is the implementation I developed the entire tool Contract Wizard IT in full Eiffel for NET and take advantage of the XML technology comparing to Contract Wizard I that is a mix of Eiffel C and COM The current impl
61. make an argument name 1 a type an argument parameter type Result set type cw type a type 2 39 ensure result_not_void Result Void end Table 14 Feature cw_argument Generating a Contract Wizard argument The feature cw_argument creates a Contract Wizard argument with a Contract Wizard argument name CW_NAME 1 To do this it first creates a Contract Wizard type CW_TYPE representing the type of the VET argument and sets it to the Contract Wizard type 2 4 4 5 The feature cw_procedure cw_procedure a_procedure METHOD_INFO CW_PROCEDURE is 1 Contract Wizard procedure AST node from NET procedure procedure require procedure not void procedure Void do Result cw procedure from method base a procedure 2 ensure 3 result not void Result Void deferred set Result is deferred a procedure is abstract static set Result is static a procedure is static end Table 15 Feature cw procedure Generating a Contract Wizard procedure The feature procedure creates a Contract Wizard procedure from NET procedure 1 It sets the procedure name CW NAME the feature is deferred and the feature is static using the feature cw procedure from method base 2 18 static is set to true if the NET procedure of type METHOD INFO corresponding to the NET System Reflection MethodInfo is static Is deferred is set to true if the NET procedure is abstrac
62. n expression representing the invariant e type by name Return the object representation of a type CW TYPE given a type name e feature by name Return the object representation of a feature FEATURE given a type name and a feature name Note For future GUI developments it would be better to access the data structure ast directly instead of using the Contract Handler the data structure ast itself acts as a Contract Handler Using the Contract Handler would yield a performance overhead 85 10 Test cases I tested Contract Wizard on the NET core library mscorlib dll 14 I checked that the generated Contract Wizard proxy classes are the same whether you retrieve them from XML files or from the NET assembly Figure 22 below shows the results of performance tests with the XML parser and with the parser The left bar shows how long it takes to retrieve the ast from the XML representation of the Contract Wizard proxy classes representing mscorlib The right bar shows how long it takes to retrieve the ast from the NET assembly using the NET reflection mechanism As you can see it is 2 4 times faster to retrieve the ast from the XML representation Furthermore the memory consumption to retrieve the ast from the XML representation is 30 percent lower 51 Megabyte XML compared to 78 Megabyte NET I tested the parsing process on a system with a Pentium 3 processor with 1100 MHz and 256 megabyte RAM time
63. nerate_source 6 Deferred abstract class f Effective implemented feature Effective concrete class f Redefined feature Inherits from Figure 21 Class diagram of the LACE Generator 73 CW_LACE_GENERATOR The class CW_LACE_GENERATOR see Figure 21 above inherits from the three classes CW_GENERATOR CW_LACE_CONSTANTS CW_EIFFEL_KEYWORD_ CONSTANTS The last two classes provide the LACE generator with the required constants to generate an ACE file CW_GENERATOR The feature generate see Figure 21 above provides the LACE generator with an assembly ASSEMBLY corresponding to the NET System Reflection Assembly and a prefix The prefix is used to prevent ambiguous names CW_LACE_GENERATOR The LACE generator extracts all required information from the assembly to generate the ACE file such as the assembly version the assembly public key token and the assembly name 76 8 Proxy class syntax In this section discuss the syntax of the generated Contract Wizard proxy classes present the syntax on a small language based on regular expressions specified by the grammar defined in Table 36 below Concatenation A A corresponds to B followed by Option C A corresponds to B or C Zero or one A B A corresponds to zero or one B Zero or many A B A corresponds to zero or many Table 36 A small language based on regular expressions I s
64. nition of a Contract Wizard attribute Table 44 shows a valid production of a Contract Wizard proxy class attribute frozen balance INTEGER is do Result account balance end Table 44 Example of a Contract Wizard attribute 8 7 Procedure specification A Contract Wizard proxy class procedure represents either a deferred abstract NET procedure an effected implemented procedure If the Contract Wizard proxy class procedure represents a deferred NET procedure it consists of a procedure name followed optionally by a formal argument list and a deferred procedure body 80 If the Contract Wizard proxy class procedure represents an effected NET procedure there is a frozen keyword in front of the procedure name prohibiting users from redefining it It has an effected feature body where the procedure call is delegated to the VET procedure Table 45 below specifies a Contract Wizard proxy class procedure Procedure_name ld Deferred procedure Procedure name Formal argument list is deferred Effected procedure frozen Procedure name Formal argument list is do Reference Procedure name Actual argument list end Procedure Effected procedure Deferred procedure Table 45 Definition of a Contract Wizard procedure The two tables Table 46 and Table 47 below show examples of Contract Wizard proxy class procedures The first table d
65. o equals get hash code and to string are delegated to the original NET class The original NET class may provide for example a special hash function All other features of the class SYSTEM OBJECT are not delegated The reason is that they are all frozen in the class SYSTEM OBJECT thus cannot be redefined Other reasons are e the feature get type We want to hide implementation details of the original class e For the features equals object object reference equals memberwise clone These features implement the required service and don t affect the current type In order to delegate the services of the three features get hash code equals and string we have to redefine them in the class SYSTEM OBJECT which every Contract Wizard proxy class inherits from I will explain the delegation of these features on the following three cases e the original class is an effected class e the original NET class is an abstract class e the original NET class is an interface The original NET class is an effected class The Contract Wizard proxy class has to implement inherit from the same interfaces as the original class to act as a surrogate For example the Contract Wizard proxy class in Table 31 on the next page generated from the NET core library mscorlib 14 implements inherits from the following interfaces e IMEMBERSHIP CONDITION 1 ISECURITY ENCODABLE 1 ISECURITY POLICY ENCODABLE 1
66. o store contracts in XML Contract Beads Implementation of a Contract Reader based on the mentioned DTD e Implementation of basic GUI for demonstration Basic GUI purposes to enable adding contracts preconditions postconditions and invariants e Implementation of the Contract Handler which is used by the basic GUI e Implementation of a code generator consisting of the following parts o XML Writer Implementation of an XML writer which enables storing contracts in an XML file based on the specified DTD One XML contract file per class o ACE File Producer Implementation of an ACE File Producer which keeps track of the NET assembly and of the Eiffel proxy classes o Proxy Class Generator Implementation of a Proxy Class Generator which produces Eiffel classes with contracts Scope Generation of a proxy class for every class subclass in the NET assembly o Assembly Updater This part updates the proxy classes with metadata e g path to the XML contract files e Implementation of a demo demonstrating the functionality and scope of Contract Wizard Il Test cases Testcases that will be used for the demo Parser Name Resolver Contract Handler Code Generator Demo application Table 1 Intended results 1 2 Background material 1 2 1 Reading list e Chapters in OOSC2 7 in particular o 1 Software quality 11 Design by Contract building reliable software 23 Principles o
67. on between CW XML PARSER XML FILTER I will explain the cooperation between the CW XML PARSER event source and the XML FILTER event handler on the example in Table 27 below The example demonstrates the serialized content of a Contract Wizard proxy class in XML It shows a list of Contract Wizard types The first type has the name CLASS It contains a creation procedure make with one argument and a procedure foo cw ast cw type dotnet name class CLASS gt lt cw_creation_procedures gt cw creation procedure eiffel name make gt cw arguments cw argument dotnet gt type is array no gt cw gt cw arguments cw creation procedure cw creation procedures cw procedures cw procedure name foo gt cw type cw type cW ast Table 27 Example of a Contract Wizard XML file 5 3 4 Event handling As mentioned before the XML parser fires events on XML tags handled by the following features of class CW XML FILTER on start tag on start tag finish on attribute and on end tag The feature on start tag handles events fired when an XML start tag has been processed The feature start tag finish handles events when the end of an XML start tag has been processed The feature on attribute handles events fired when an XML attribute has been processed and finally the feature
68. or NET methods The feature inspect fields takes care of fields It adds the resulting Contract Wizard type to the ast 5 46 4 6 4 Inspect methods inspect_methods is Inspect all public method infos from type Call parse_public_procedure parse_public_function for every inspected method info depending on the method info type local i INTEGER do method infos type get methods Inspect all public method infos from type from i method infos lower until method infos count loop method info method infos item 1 if procedure exists then parse public procedure 1 elseif function exists then parse public function 2 end 1 ensure then all methods parsed method info method infos item method 1 end The feature inspect methods iterates through all public methods of the NET type being parsed It calls the feature parse public method 1 or parse public function 2 for every Table 22 Feature inspect methods inspected method depending on the return type of the NET method 4 6 5 The feature inspect fields iterates through all public fields of the parsed NET type It calls the feature parse public field for every inspected public field The code resembles the code Inspect fields fragment in Table 22 4 6 6 The feature inspect contructors iterates through all public constructors of the parsed NET type It calls the featur
69. or attributes procedures visitor procedures Table 8 Example of how to use a visitor 30 Table 9 shows an example of how the lists could be extended For every call a_symbol visit visitor a specific visitor procedure is called on the visitor object If the visited symbol in the symbol DS 15 type CW_PROCEDURE then the visitor procedure visit_procedure 15 called 1f the visited symbol is of type CW_ATTRIBUTE then the visitor procedure visit_attribute 15 called and so on Back to the example this means that we can create specific Eiffel code or its XML representation for every visited symbol through execution of the specific visitor procedure From the argument symbol of the visitor procedure we can retrieve all needed information to produce code for example if the symbol is of type CW_PROCEDURE we can get the procedure name the attributes class EJIFFEL VISITOR EXAMPLE inherit VISITOR create make feature Basic operations visit procedure a procedure CW PROCEDURE is Visit procedure a procedure local a procedure name STRING some attributes STRING do a procedure name a procedure eiffel name a procedure name append some attributes procedures extend a procedure name end visit creation procedure a creation procedure CW PROCEDURE is Visit creation procedure a creation procedure end Table 9 Example using the EIFFEL VISITOR class 3l 32 4 The NET parser This
70. ract with Contract Wizard II Table 6 shows the generated Contract Wizard proxy class CW_ACCOUNT for the class Account A closer look at Table 6 on the next page shows the inserted precondition 1 indexing note Automatically generated by the Contract Wizard dotnet_name Accounting Account class CW_ACCOUNT 19 inherit SYSTEM_OBJECT redefine get_hash_code equals to_string end create make feature NONE Initialization frozen make a initial amount INTEGER is name Account ctor InitialAmount Int32 do create account ref make a initial amount end feature Access frozen balance INTEGER is dotnet name Account Balance Int32 do Result account ref balance end frozen deposits DEPOSIT LIST is dotnet name Account Deposits DepositL ist do Result account ref deposits end frozen withdrawals WITHDRAWAL LIST is dotnet name Account Withdrawals WithdrawalList do Result account ref withdrawals end frozen minimum balance INTEGER is dotnet name Account MinimumBalance Int32 do Result account ref minimum balance end 20 feature Query frozen get_hash_code INTEGER is dotnet_name Account GetHashCode Int32 do Result account_ref get_hash_code end frozen equals obj SYSTEM OBJECT BOOLEAN is dotnet_name Account Equals obj Object Boolean do Result account_ref equals a
71. riginally written in was developed by Karine Arnout 2 The Contract Wizard was written for a beta version The aim of this diploma thesis is to implement a tool called Contract Wizard II which works with the latest version of Contract Wizard reads a contract less NET assembly and separately provided contracts It merges this information and automatically generates Eiffel proxy classes containing contracts Then the contracted Eiffel proxy classes are compiled into a new NET assembly Moreover an XML representation of the contracted Eiffel proxy classes is generated This enables the client to append contracts to an already contracted assembly without going through the whole process given above For more details on the scope and the objectives of this diploma thesis see Chapter 1 Contract Wizard II has been designed with extendibility in mind it should be easy to adapt it to support future changes of the Eiffel language and to automatic contract extraction from NET assemblies 5 This report consists of four major parts e The first part describes the project plan including the set up of the project and the intended results e The second part is a user manual explaining how to use the tool Contract Wizard e The third part is a developer report it addresses the developers who want to extend the tool e The last part is theoretical and discusses the advantages and drawbacks of storing contracts direct
72. rocedure CREATION PROCEDURE The CW NAME FORMATTER 4 8 2 translates CamelCase NET names into Eiffel names separated by underscore keeps track of the following CLS compliant primitive types byte int16 int32 int64 single double boolean char decimal intPtr and string keeps track of NET class names that conflict with the EiffelBase library keeps track of NET names that conflict with keywords of the Eiffel language For example feature do end handles arrays of dimension n Limitations The parser does not parse events class EVENT INFO System ReflectionEventInfo in NET since the event mechanism of NET differs from the agent mechanism of Eiffel The parser does not parse nested classes 51 52 5 The XML parser Contract Reader This chapter discusses the implementation of the XML parser First evaluate two parsers and explain why selected the Gobo XML parser Then describe the XML parser developed Finally I discuss the event handling mechanism 5 1 Design of the XML parser 5 1 1 Evaluating the right parser There are quite a few libraries to parse XML files in Eiffel for NET have evaluated the two following libraries e Gobo a free and portable library for Eiffel 10 e XML libraries part of the VET Framework 14 The Gobo library contains two non validating XML parsers A pure Eiffel parser and a parser based on the expat C library 13 The XML librari
73. ry create from assembly Partial assembly name Target directory append contracts File name i create from assembly Partial assembly name Target directory put contracts File name 1 C MyPro jects m 7 EI FGEN W_code gt of 4 4 Figure 3 Contract wizard commands e The command help shows all valid commands see Figure 3 e The command version shows the Contract Wizard version e The command remove contracts removes the XML file storing all contracts for the assembly specified by Partial_assembly_name e The command edit contracts opens the file containing the XML representation of the Contract Wizard proxy classes in an external editor If you use for example the editor vi you can edit the contracts using the command edit contracts assembly vi The command create from assembly means that we want to create Contract Wizard proxy classes for the assembly specified by Partial assembly name e The command append contracts inserts contracts defined in file File name into the assembly corresponding to Partial assembly name e The command put contracts removes all existing contracts and adds new contracts specified in the file File name Partial assembly name describes only a part of the assembly name The assembly fully qualified name also contains the assembly version and the assembly public key token In the current version Contract Wizard loads the newest version of the assembly from the global assembly cache GAC speci
74. s Special names is a linked list storing the interface feature names which are foreseen to be undefined in the proxy class 3 2 3 Contract Wizard feature CW FEATURE The next version of Eiffel 11 will allow assertions on attributes ATTRIBUTE That is why a feature FEATURE has preconditions PRECONDITION and postconditions POSTCONDITION A feature is either a routine ROUTINE or an attribute ATTRIBUTE see Figure 8 below 27 preconditions LINKED LIST postconditions LINKED LIST arguments LINKED LIST QD Deferred abstract class Effective concrete class Inheritance link Supplier link Figure 8 Clients of CW FEATURE 3 2 4 Contract Wizard routine CW ROUTINE A routine has a list of arguments ARGUMENT see Figure 8 A routine is either a procedure PROCEDURE a function FUNCTION see Figure 9 on next page the latter has a return type TYPE 3 2 5 Contract Wizard attribute CW ATTRIBUTE Every attribute has a name NAME and a type TYPE 3 2 6 Implementation hierarchy CW TYPE CW FEATURE and CW ASSERTION inherit the feature visit from AST NODE We need this feature to implement the visitor pattern 3 The class CW FEATURE introduces preconditions and postconditions which are inherited by ROUTINE ATTRIBUTE Both FUNCTION PRO
75. s parse public constructor parse public function parse public procedure and parse public field see Figure 13 above I will now describe them in more detail 44 4 6 1 The creation procedure make an_assembly like assembly is 1 Initialize current with CLS compliant an_assembly Generate ast require assembly_not_void an_assembly Void assembly is cls compliant js cls compliant an assembly 2 do Create an empty ast create ast make 3 Create ast factory to enable generation of cw ast nodes create ast factory make 3 assembly an assembly ensure assembly assigned assembly an assembly end Table 19 Creation procedure of the NET parser This feature initializes the parser with a NET assembly that has to be parsed 1 The NET assembly must CLS compliant hence precondition assembly is cls compliant 2 Moreover this feature initializes the data structure ast and the ast factory 3 This is ensured by the class invariant clauses ast void and ast factory not void 4 6 2 Feature parse parse is Parse all public types from assembly local E INTEGER do types assembly get types from types lower until types count loop type types item i if type_exists 2 then type 1 end Jc mei end 45 ensure then types_not_void types Void types parsed type types item types count 1 consistent a
76. s structure makes the proxy a real surrogate for the original NET class 6 1 2 Inheritance from System Object Every type in NET is an object meaning that it must derive directly or indirectly from the Object class If you do not specify a base class when you define a class the compiler will inject this requirement into the IL code 3 Since Contract Wizard proxy classes are also NET classes they inherit from the class System Object Before discussing the consequences I will examine the public methods of the class System Object in Eiffel SYSTEM_OBJECT From now on I will use the Eiffel for NET naming conventions The class SYSTEM OBJECT has the following public methods equals obj SYSTEM OBJECT BOOLEAN Determines whether the specified obj is equal to the current object 64 e frozen equals object object obj SYSTEM OBJECT obj b SYSTEM OBJECT BOOLEAN Compares two objects and determines whether they are equal field by field equality e frozen reference equals obj a SYSTEM OBJECT obj b SYSTEM OBJECT BOOLEAN Compares two object references and determines whether they are referring to the same object reference equality e get hash code INTEGER Gets the object hash code e frozen get type TYPE Obtains the object s type at runtime e 10 string SYSTEM STRING Gets a string representation of the object e frozen memberwise clone SYSTEM OBJECT Creates a shallow copy of the current object Calls t
77. ssion CW_PRECONDITION is Precondition with tag a tag and boolean expression an expression require tag not void a tag Void tag not empty not fag is empty expression not void an expression Void expression not empty not an expression is empty do create Result make a tag an expression 1 ensure precondition not void Result Void tag set Result tag a tag expression set Result expression an expression end Table 10 Feature cw precondition creating a precondition from given tag and expression The feature cw precondition creates a Contract Wizard precondition from a contract tag and a contract expression 1 4 3 3 The feature cw postcondition The feature cw postcondition creates a Contract Wizard postcondition from a contract tag and a contract expression The implementation resembles the implementation of precondition given above simply replace precondition with postcondition 36 4 3 4 The feature cw_invariant The feature cw_invariant creates a Contract Wizard invariant from a contract tag and a contract expression The implementation resembles the implementation of cw_precondition shown before just replace precondition with invariant 4 3 5 The features cw_type_name and cw_feature_name cw_feature_name SYSTEM_STRING 1 Feature name with Eiffel name and NET name created from require
78. st count type count end Table 20 Feature parse Parsing public types This feature iterates through all types of a assembly ASSEMBLY and calls parse type 1 for every inspected public type 2 4 6 3 Parsing a type parse type is Parse a public type and all its members like method infos field infos constructor infos Generate a cw type with all its features cw procedure cw function cw attribute do cw ast factory cw type type 7 if has public constructors then inspect constructors 2 end if has public methods then inspect methods 3 end if has public fields then inspect fields 4 end ast extend type 5 end Table 21 Feature parse type Parsing public members of a type The feature parse type creates a Contract Wizard type TYPE from the NET type being parsed The NET ast factory AST FACTORY facilitates the creation of the Contract Wizard type TYPE It sets all required features is static is deferred is expanded and is enum 1 It parses the public constructors methods and fields members and calls the features inspect constructors 2 inspect methods 3 and inspect fields 4 depending on the type of the parsed member The feature inspect constructors iterates through every constructor of the NET type and calls the feature responsible for parsing it parse public constructor The feature inspect methods does the same f
79. structs e effected NET classes e deferred abstract classes e interfaces The Contract Wizard proxy class inherits from the same interfaces as its corresponding NET class Every Contract Wizard proxy class inherits from SYSTEM_OBJECT The Contract Wizard Proxy class represents effected NET class If the Contract Wizard proxy class represents an effected class it has Type_distinctor see Table 52 below in front of the keyword class It has a creation clause Creation_clause where it defines all creation procedures The Contract Wizard proxy class represents a deferred abstract VET class If the Contract Wizard proxy class represents a deferred class 1t has the Type_distinctor deferred in front of the keyword class The Reference in a deferred Contract Wizard proxy class points to a deferred NET class That is why it is not instantiated To use a deferred Contract Wizard proxy class you have to effect it The Contract Wizard proxy class represents an interface If the Contract Wizard proxy class represents an interface it has the Type_distinctor deferred in front of the keyword class It has no creation clause Creation_clause and no Reference Table 52 below specifies the syntax of a Contract Wizard proxy class Type distinctor deferred Dotnet class type Id Class name Dotnet class type Interface name Id Creation procedure names Creation procedure name Creation proced
80. t cw procedure cw function cw attribute cw creation procedure CD Deferred abstract class Effective concrete class inherits from f Effective implemented feature Figure 12 Contract Wizard AST factory classes I have implemented a factory factory pattern 3 to facilitate the creation of Contract Wizard AST nodes CW_PROCEDURE CW_FUNCTION CW_TYPE The factory consists of the following classes CW_AST_FACTORY CW_XML_AST_FACTORY and CW_DOTNET_AST_FACTORY see Figure 12 35 The class CW AST FACTORY implements the common features for XML AST FACTORY and DOTNET AST FACTORY The role of class DOTNET AST FACTORY 15 to create Contract Wizard AST nodes from NET members METHOD INFO FIELD INFO and NET types class TYPE corresponding to the NET type System Type The role of class XML AST FACTORY 15 to create Contract Wizard AST nodes CW PROCEDURE CW FUNCTION CW TYPE from XML nodes XML XML NODE corresponding to System Xml XmlNode Every XML node represents NET member a type or a contract precondition postcondition or invariant The class CW AST FACTORY implements the following features cw feature name cw type name precondition cw postcondition cw invariant see Figure 12 above They are described below 4 3 2 The feature precondition cw precondition a tag like tag an expression like expre
81. t The postcondition ensures that features is deferred and is static of PROCEDURE are set 3 4 4 6 The feature cw function cw function a function METHOD INFO CW FUNCTION is 1 Contract Wizard function AST node representing a NET function created from NET function a function require a function not void a function Void a function name not void a function name Void 40 local a_procedure CW_PROCEDURE a_return_type CW_TYPE do a_return_type cw_type a_function return_type a procedure cw procedure from method base a function 2 create Hesult make from procedure a procedure a return type Result set is property is property a function 3 ensure 4 result not void Result Void deferred set Result is deferred function is abstract property set Hesult is property is property a function static set Result is static function is static end Table 16 Feature cw function Generating a Contract Wizard function The feature cw function creates a Contract Wizard function from a NET function 1 It sets the procedure name the feature is deferred and the features static using the feature procedure from method base 2 Is static is set to true if the parsed procedure of type METHOD INFO corresponding to the NET System Reflection MethodInfo is static Is deferred is set to true if the parsed function is abstract It sets the f
82. t to be more complex to parse the NET assemblies and their XML representation than initially assumed which caused a delay for the other milestones Due to the delay we decided to move milestone M4 at the end of the project and continued directly with milestone M3 Instead of writing a basic GUI we decided to write a command line interface Milestone Scheduled Deadline Handed in M1 Software architecture 2003 06 13 Fr 2003 06 13 Fr M2 Parser 2003 07 04 Fr 2003 07 17 Fr M3 Contract Reader 2003 07 13 Fr 2003 08 06 Fr M4 Contract Handler 2003 07 25 Fr 2003 10 03 Fr M5 Code generator 2003 08 08 Fr 2003 09 01 Fr 6 Eiffel Compiler 2003 08 08 Fr 2003 10 03 Fr M7 Basic GUI 2003 08 15 Fr 2003 10 03 Fr M8 Demo 2003 08 22 Fr 2003 10 03 Fr M9 Intermediary report 2003 08 29 Fr 2003 10 02 Fr M10 Developer report 2003 10 06 Fr 2003 10 06 Fr M11 Thesis report 2003 10 11 Fr 2003 10 06 Fr Table 4 Deadlines 1 6 Tentative Schedule Figure 2 on the next page shows the tentative project schedule 14 Task Diploma Project 23 40 Presentation P1 Introductory presentation P2 Final presentation Design 1 Software architecture tentative 1 Software architecture real M3 DTD tentative M3 DTD real Implementation M2 Parser tentative M2 Parser real M2 N
83. tart with some basic concepts such as identifiers formal and actual arguments and types Based on them describe the syntax of creation procedures attributes procedures functions and finally I explain the syntax of a Contract Wizard proxy class 8 1 Identifier specification Table 37 below shows the specification of an identifier 1d Valid identifiers are for example a_name foo etc ld Letter Letter Digit Special character Letter FA BA ez la 27 Digk O e m Special character Table 37 Definition of identifiers Note Special characters are limited to characters provided by the NET framework 71 8 2 Type specification Table 38 below shows the production of a type Type There are four different types handled by Contract Wizard e Defined types Some types already exist in Eiffel For these types Contract Wizard prepends the prefix SYSTEM_ e Array types These types represent arrays The array type corresponds to the characters between the opening and the closing square bracket e Object types These types represent objects e Primitive types These are CLS compliant primitive types They are not the same in Eiffel and in NET they have different names Defined type SYSTEM STRING SYSTEM ARRAY SYSTEM CONSOLE SYSTEM DATE TIME SYSTEM DIRECTORY SYSTEM SYSTEM QUEUE SYSTEM RANDOM SYSTEM SORTED LIST SYSTEM STACK
84. the parsed type is an enumerated type The namespace is set because it is required for the class description of the proxy classes later 4 We also have to set all CLS compliant interfaces the type implements 5 The postcondition of cw type ensures that all Contract Wizard type features are set 6 38 4 4 3 The feature cw_interface cw interface interface CW INTERFACE is Contract Wizard type AST node created from NET argument argument require interface not void an interface Void local an interface name CW NAME a feature name CW NAME do create Result make an interface name 7 Result add speial name a feature name ensure result not void Result Void end Table 13 Feature cw interface Generating a Contract Wizard interface The feature interface creates a Contract Wizard interface with a Contract Wizard interface name CW NAME 1 and sets special interface feature names ToString Equals GetHashCode required by the Contract Wizard proxy class generator 2 4 4 4 The feature argument cw argument an argument PARAMETER INFO CW ARGUMENT is Contract Wizard type AST node created from NET argument argument require argument not void an argument Void argument name not void an argument name Void local an argument name CW NAME a type TYPE do an argument name cw feature name an argument name to string create Result
85. tract files and produces a Contract DS Furthermore like in point 1 a symbol DS is produced 3 Both the Contract DS and the symbol DS are handled by the Contract Handler and the basic GUI They produce a symbol DS containing information about the VET assembly and the contracts The Contract Handler and the basic GUI are also responsible for user contracts User contracts are appended to the Contract DS 4 The Code Generator traverses the symbol DS with contracts and produces proxy classes with contracts The proxy classes will contain metadata expressing where the Contract Files are located Another task of the Code generator is to generate XML files storing all information about contracts and to produce the ACE files needed by the Eiffel compiler 5 The Eiffel compiler is called by the GUI and compiles the generated proxy classes using the ACE files If the contracts are not correct an error message is displayed and the contracts can be corrected considering the error messages 1 1 3 Intended results Table 1 below shows the intended results e Implementation of a parser which parses assembly and produces a symbol DS e Implementation of a Name Resolver e Scope NET and Eiffel have different naming conventions hence different names for the same type or feature The Name Resolver keeps track of NET types and NET methods It uses the Eiffel 10 core algorithm for renaming Contracts e Implementation of a DTD t
86. ure Redefined feature f Deferred feature Figure 19 Class diagram of the Eiffel code generator The Eiffel code generator consists of the deferred classes CW_GENERATOR and CW_VISITOR and of the effected classes CW_EIFFEL_GENERATOR CW_EIFFEL_VISITOR and CW_EIFFEL_CONSTANTS CW_GENERATOR The class CW_GENERATOR specifies the features common to all its descendants CW EIFFEL GENERATOR see Figure 19 XML GENERATOR LACE GENERATOR The class GENERATOR and its descendants have a common creation procedure make It has two arguments ast of type LINKED LIST CW_TYPE and a directory path name STRING The argument a directory path name defines the path to the directory where the generated code is stored The data structure ast represents the parsed classes from the NET assembly 71 CW_VISITOR The class CW_VISITOR defines features common to its descendants EIFFEL VISITOR and CW XML VISITOR The last two classes are described below CW EIFFEL KEYWORD CONSTANTS The class EIFFEL KEYWORD CONSTANTS specifies the required constants for generating the Eiffel representation of Contract Wizard proxy classes The Eiffel visitor EIFFEL VISITOR The Eiffel code for the Contract Wizard proxy classes is generated using an Eiffel visitor EIFFEL VISITOR It specifies features for Eiffel code generation namely e visit creation procedure This feature generates Eiffel code for a Contr
87. ure cw_arguments cw_preconditions cw_postconditions gt lt ATTLIST cw_creation_procedure dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED gt lt ELEMENT cw_attribute type cw_preconditions cw_postconditions gt lt ATTLIST cw attribute dotnet name CDATA REQUIRED eiffel name CDATA REQUIRED is constant yes no REQUIRED value REQUIRED is static yes no REQUIRED gt lt ELEMENT cw_procedure cw_arguments cw_preconditions cw_postconditions gt lt ATTLIST cw_procedure dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED is deferred yes no REQUIRED is static yes no REQUIRED gt lt ELEMENT cw_function cw_arguments type cw_preconditions cw_postconditions gt lt ATTLIST cw_function dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED is deferred yes no REQUIRED is static yes no REQUIRED is property yes no REQUIRED 60 lt ELEMENT cw_argument type gt lt ATTLIST cw_argument dotnet_name CDATA REQUIRED eiffel name CDATA REQUIRED gt lt ELEMENT cw_invariant EMPTY gt lt ATTLIST cw_invariant tag CDATA IMPLIED expression CDATA REQUIRED gt lt ELEMENT cw precondition EMPTY gt lt ATTLIST cw_precondition tag CDATA IMPLIED expression CDATA REQUIRED gt lt ELEMENT cw_postcondition EMPTY gt lt ATTLIST cw_postcondition tag CDATA HIMPLIED expression CDATA REQUIRED gt lt ELEMENT int
88. ure name Creation procedure names 83 Creation clause create Creation procedure names Undefinitions undefine get hash code equals 1 to string end Redefinitions redefine get hash code 1 equals to string Interface Interface name Undefinitions Interfaces Interface Interfaces Class header Class distinctor class Class name inherit Interfaces SYSTEM OBJECT Redefinitions Undefinitions Creation clause Ref frozen Reference Dotnet class type Class body Features feature NONE Implementation Ref Contract Wizard proxy class Class header Class body end Table 52 Definition of a Contract Wizard proxy class 84 9 Contract Handler The Contract Handler provides the ability to insert contracts to the Symbol DS It can be easily accessed from the GUI or any other client The Contract Handler is implemented by the class CW_CONTRACT_HANDLER and offers the following basic features e add precondition Insert a precondition to the ast given a type name a feature name a tag and expression representing the precondition e add Insert postcondition to the ast given type name a feature name a tag and expression representing the postcondition e add invariant Insert an invariant to the ast given a type name a tag and a
89. ures functions invariants visit_type visit_creation_procedure visit_attribute visit_procedure generate_source generate generate_source store_source generate_and_store_class visit_type visit_creation_procedure visit_attribute visit_procedure visit_function Deferred abstract class visit_argument visit_precondition G Effective concrete class visit_postconditon visit_invariant Inherits from gt Client relation f Effective implemented feature Redefined feature f Deferred feature Figure 20 Class diagram of the XML code generator Figure 20 above shows how classes CW_VISITOR CW_XML_VISITOR CW_GENERATOR CW_XML_GENERATOR and CW_XML_CONSTANTS interact with each other The classes CW_GENERATOR and CW_VISITOR are the same as the ones discussed in section The Eiffel code generator CW_XML_GENERATOR The class CW XML GENERATOR generates XML code using the XML visitor CW_XML_VISITOR and stores it to the specified XML file CW_XML_CONSTANTS The class CW_XML_CONSTANTS specifies the XML tags required for generating XML proxy classes CW_XML_VISTOR The generation of XML code is similar to the generation of Eiffel code except that it uses an XML visitor instead of an Eiffel visitor in feature visit_class see Table 35 The class CW_XML_VISITOR is responsible for code generation It has the following features visit creat

Download Pdf Manuals

image

Related Search

Related Contents

MODEL P104-COM232-8 USER MANUAL  Restez sur la bonne voie avec l`Ikarus S  Kenroy Home 32166WH Instructions / Assembly  User's Guide  norme di sicurezza  Olympus M.ZUIKO DIGITAL 25mm 1:1.8  Landlord User Guide - Manchester Student Homes  DIRECT CARD PRINTER PR5300 Service Manual  Penpower TOOYA PRO User's Manual  LogiLink AA0058 screen protector  

Copyright © All rights reserved.
Failed to retrieve file