Java Modeling Language JML
1 Design by Contract View point
1.1 Goals
- Assigning blame across interfaces
- Details of method responsibilities
1.2 Information Hiding by Contracts
- A contract can be satisfied in many ways
- a method can have many implementation satisfying the contract;
- Different performances (time, space, etc);
- A contract hides the implementation details
- We can change implementations.
- Caller's code Will work for any implementation that satisfies the
contract
1.3 Techniques
- Strongest Class invariant
- Weakest Pre-conditions
requires
for public methods
- The called method assumes precondition holds
- Avoiding constantly checking arguments
- The caller guarantees precondition holds
- Strongest Post-conditions
ensures
for public methods
- The called guarantees postcondition
- The caller is guaranteed postcondition
2 Java Modeling Language (JML) Overview
- http://jmlspecs.sourceforge.net/
- JML is a behavioral (functionality) interface specification language
- Annotation language captures programmer design decisions
- JML is contained in annotations
//@ JML annotation
/*@ JML annotation @*/
javac
will ignore these
- JML annotation is a Java boolean expression …
- without any side effects
- extended with
\old
, \result
- extended with
\forall
, \exists
- JML keywords:
requires, ensures, invariant
- JML keywords:
pure, nonnull, assignable, signals
- JML annotation includes
model
fields
2.1 No Side Effects
- No use of
=, ++, --
, …
- Can only call pure methods.
2.2 Java extended syntax in JML
a ==> b
stands for a implies b
a <==> b
stands for a iff b
- a <=!=> b stands for !(a <==> b)
\old(E)
stands for the value of E in pre-state
\result
stands for the result of method
2.3 Quantifiers
- Universal ∀ JML
\forall
and existential ∃ JML \exists
- General quantifiers (
\sum, \product, \min, \max
)
- Numeric quantifier (
\num_of
)
- Ex:
(\forall Student s; juniors.contains(s); s.getAdvisor() != null)
2.5 Non-Null
- To state that some references must not be
null
…
private /*@ non null @*/ File[] files;
void createSubdir(/*@ non null @*/ String name) { ... }
2.6 Java assert
assert Expression1 ;
where Expression1 is a boolean
expression. When the system runs the assertion, it evaluates
Expression1 and if it is false throws an AssertionError with no
detail message.
assert Expression1 : Expression2 ;
where Expression1 is a boolean
expression. Expression2 is an expression that has a value. The
AssertionError constructor uses the string value of Expression2 to
generate a detailed message.
2.7 JML assert
//@ assert i > 0 && 0 < j && j < 5;
//@ assert (\forall int i; 0 <= i && i < n; a[i] != null);
- keyword assert also in Java (since Java 1.4)
- assert in JML is more powerful
2.8 assignable
- Frame properties limit possible side-effects of methods.
//@ requires amount >= 0;
//@ assignable balance;
//@ ensures balance == \old(balance) - amount;
public int debit(int amount) { ... }
debit
can only assign to the field balance.
NB this does not follow from the post-condition.
//@ assignable \everything
//@ assignable \nothing
2.9 pure
- A method without side-effects is said to be
pure
public /*@ pure @*/ int getBalance() { ... }
- Pure methods have implicitly
assignable \nothing
- Pure methods, and only pure methods, can be used in
specifications:
//@ invariant 0 <= getBalance() && getBalance() <= MAX_BALANCE
3 Model Variables
- variables to be used only in specifications
- Given value only by
represents
clauses
4 Tools for JML
4.2 ESC/Java2
- http://kindsoftware.com/products/opensource/ESCJava2/
- Improve the current software engineering process
- Can prove JML assertions at compile time.
- Effort must be made by the developer
- So far, only possible for small programs
- Input: a Java program annotated with JML assertions
- Powered by program semantics and automatic theorem proving
- Automatically check if the assertions are always true;
- Statically without any user interaction
- Reason about non-trivial properties (not just type-correctness)
- Its warnings are intended to be interpreted by the author of the
program
- It does not find all the errors, but reduces the cost of
finding some of them early
4.3 JACK: Java Applet Correctness Kit
6 References
- http://goverily.org/ Rather than requiring that programs be
verified in separate a posteriori analysis, Verily supports
construction via a series of Recipes, which are properties of an
application that are enforced at compile time. Recommended Visit.
- http://types.cs.washington.edu/checker-framework/ The Checker
Framework enhances Java’s type system to make it more powerful and
useful. This lets software developers detect and prevent errors in
their Java programs. The Checker Framework includes compiler
plug-ins ("checkers") that find bugs or verify their absence. It
also permits you to write your own compiler plug-ins. Recommended
Visit.
- http://openjml.org/ The Java Modeling Language (JML) is a language
used to describe the functional behavior of Java classes and
methods. http://sourceforge.net/projects/jmlspecs/ Recommended Visit.
- JML plugins for IntelliJ and Eclipse exist, but … ; e.g.,
./eclipse-jml-plugin-slides.pdf
- OpenJML: Software verification for Java 7 using JML, OpenJDK, and
Eclipse David R. Cok GrammaTech, Inc. Ithaca, NY, USA
cok@frontiernet.net http://arxiv.org/pdf/1404.6608.pdf, 2014.
Recommended Reading.
Copyright © 2015 •
www.wright.edu/~pmateti • 2015-07-20