FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Java_Modeling_Language

Java Modeling Language

Contents
Tool support
References
External links

The Java Modeling Language (JML) is a specification language for Java programs, using Hoare style pre- and postconditions and invariants, that follows the design by contract paradigm. Specifications are written as Java annotation comments to the source files, which hence can be compiled with any Java compiler.

Various verification tools, such as a runtime assertion checker and the Extended Static Checker (ESC/Java) aid development.

/*@ <JML specification> @*/

Basic JML syntax provides the following keywords

requires
Defines a precondition on the method that follows.
ensures
Defines a postcondition on the method that follows.
signals
Defines a postcondition for when a given Exception is thrown by the method that follows.
signals_only
Defines what exceptions may be thrown when the given precondition holds.
assignable
Defines which fields are allowed to be assigned to by the method that follows.
pure
Declares a method to be side effect free (like assignable \nothing but can also throw exceptions). Furthermore, a pure method is supposed to always either terminate normally or throw an exception.
invariant
Defines an invariant property of the class.
loop_invariant
Defines a loop invariant for a loop.
also
Combines specification cases and can also declare that a method is inheriting specifications from its supertypes.
assert
Defines a JML assertion.
spec_public
Declares a protected or private variable public for specification purposes.

Basic JML also provides the following expressions

\result
An identifier for the return value of the method that follows.
\old(<expression>)
A modifier to refer to the value of the <expression> at the time of entry into a method.
(\forall <decl>; <range-exp>; <body-exp>)
The universal quantifier.
(\exists <decl>; <range-exp>; <body-exp>)
The existential quantifier.
a ==> b
a implies b
a <== b
a is implied by b
a <==> b
a if and only if b

as well as standard Java syntax for logical and, or, and not. JML annotations also have access to Java objects, object methods and operators that are within the scope of the method being annotated and that have appropriate visibility. These are combined to provide formal specifications of the properties of classes, fields and methods. For example, an annotated example of a simple banking class may look like

= 0 && balance <= MAX_BALANCE;\n \n //@ assignable balance;\n //@ ensures balance == 0;\n public BankingExample() {\n this.balance = 0;\n }\n \n //@ requires 0 < amount && amount + balance < MAX_BALANCE;\n //@ assignable balance;\n //@ ensures balance == \\old(balance) + amount;\n public void credit(final int amount) {\n this.balance += amount;\n }\n \n //@ requires 0 < amount && amount <= balance;\n //@ assignable balance;\n //@ ensures balance == \\old(balance) - amount;\n public void debit(final int amount) {\n this.balance -= amount;\n }\n \n //@ ensures isLocked == true;\n public void lockAccount() {\n this.isLocked = true;\n }\n \n //@ requires !isLocked;\n //@ ensures \\result == balance;\n //@ also\n //@ requires isLocked;\n //@ signals_only BankingException;\n public /*@ pure @*/ int getBalance() throws BankingException {\n if (!this.isLocked) {\n return this.balance;\n } else {\n throw new BankingException();\n }\n }\n}\n"}}'>publicclass BankingExample{ publicstaticfinalintMAX_BALANCE=1000; private/*@ spec_public @*/intbalance; private/*@ spec_public @*/booleanisLocked=false; //@ public invariant balance >= 0 && balance <= MAX_BALANCE; //@ assignable balance; //@ ensures balance == 0; publicBankingExample(){ this.balance=0; } //@ requires 0 < amount && amount + balance < MAX_BALANCE; //@ assignable balance; //@ ensures balance == \old(balance) + amount; publicvoidcredit(finalintamount){ this.balance+=amount; } //@ requires 0 < amount && amount <= balance; //@ assignable balance; //@ ensures balance == \old(balance) - amount; publicvoiddebit(finalintamount){ this.balance-=amount; } //@ ensures isLocked == true; publicvoidlockAccount(){ this.isLocked=true; } //@ requires !isLocked; //@ ensures \result == balance; //@ also //@ requires isLocked; //@ signals_only BankingException; public/*@ pure @*/intgetBalance()throwsBankingException{ if(!this.isLocked){ returnthis.balance; }else{ thrownewBankingException(); } } }

Full documentation of JML syntax is available in the JML Reference Manual.

Tool support

edit

A variety of tools provide functionality based on JML annotations. The Iowa State JML tools provide an assertion checking compiler jmlc which converts JML annotations into runtime assertions, a documentation generator jmldoc which produces Javadoc documentation augmented with extra information from JML annotations, and a unit test generator jmlunit which generates JUnit test code from JML annotations.

Independent groups are working on tools that make use of JML annotations. These include:

References

edit

External links

edit

View original on Wikipedia | Back | FazBrowse Home