a survey on java modeling languages

29
A Survey on Java Modeling Languages Gergely Kovásznai, Eszterházy Károly College Wolfgang Schreiner, Johannes Kepler University Gábor Kusper, Eszterházy Károly College Gábor Guta, Johannes Kepler University János Sztrik, University of Debrecen

Upload: levi

Post on 09-Jan-2016

25 views

Category:

Documents


1 download

DESCRIPTION

A Survey on Java Modeling Languages. Gergely Kovásznai ,Eszterházy Károly College Wolfgang Schreiner ,Johannes Kepler University Gábor Kusper ,Eszterházy Károly College Gábor Guta ,Johannes Kepler University János Sztrik ,University of Debrecen. Goal. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A Survey on Java Modeling Languages

A Survey on Java Modeling Languages

Gergely Kovásznai, Eszterházy Károly CollegeWolfgang Schreiner, Johannes Kepler UniversityGábor Kusper, Eszterházy Károly CollegeGábor Guta, Johannes Kepler UniversityJános Sztrik, University of Debrecen

Page 2: A Survey on Java Modeling Languages

Goal

A specification language is used to express the concepts of the programmer on a high level

using pre-conditions and post-conditions, also called contracts, and invariants. The surveyed meta languages have also tools which

can detect whether the Java implementation breaks or not the high level description.

So we can see the clauses of the high level design as syntactical or semantical constraints.

Page 3: A Survey on Java Modeling Languages

Goal

Semantical constraint – Assertion

Syntactical constraintoutside Java

Syntactical constraintinside Java

Motivation: Design Patterns &OODesign Principles

Page 4: A Survey on Java Modeling Languages

Previous work

Austro – Hungarian Joint Research Project:Introducing Syntactical Constraints in Object-Oriented Programming Supporting Design Pattern Principles

Wolfgang Schreiner: A JML Specification of the Design Pattern "Proxy". RISC-Technical report, April 2009.

Gergely Kovasznai: Java Framework Implementing Design Patterns by the Use of JML and Contract4J. RISC-Technical report, 2009.

Wolfgang Schreiner: Supporting the Design Pattern "Object Structures as Plain Values". RISC-Technical report, September 2009.

Page 5: A Survey on Java Modeling Languages

Java 1.4: Assert

Syntax: assert Test ; where Test is a boolean expression. When the

system runs the assertion, it evaluates Test and if it is false throws an AssertionError with no detail message.

The second form of the assertion statement is: assert Test : ErrorMessage ;

Page 6: A Survey on Java Modeling Languages

Java 1.4: Assert

Example:public real division(int a, int b){

assert b!=0;…

}We cannot divide by zero.We can test this in runtime by an assert.

Page 7: A Survey on Java Modeling Languages

Java 1.4: Assert

To enable assertions use the -enableassertions, or -ea, switch.

To disable assertions use the -disableassertions, or -da, switch.

java –ea HelloWorld

Page 8: A Survey on Java Modeling Languages

Goal

Semantical constraint – Assertion

Syntactical constraintoutside Java

Syntactical constraintinside Java

Motivation: Design Patterns &OODesign Principles

Page 9: A Survey on Java Modeling Languages

JML: “Java Modeling Language”

JML is a behavioral interface specification language.

In JML one can specify for each class its interface, which consists of the names

and static information found in Java declarations,

its behavior, which tells how the module acts when used.

Page 10: A Survey on Java Modeling Languages

JML Example

public class BankAccount {//@ invariant 0<=balanceprivate short balance;

/*@ requires amount>=0;ensures balance== \old(balance-amount)&& \result==balance;

@*/public int debit(int amount) { ...}

}

Page 11: A Survey on Java Modeling Languages

JML as a Contract Language

Precondition:requires <predicate>

Postcondition:ensures <predicate>

Invariant:invariant <predicate>

A predicate may contain: Java literals method calls, field references Java operators \forall, \exists, \max, \min, \sum, \result, \old etc.

Page 12: A Survey on Java Modeling Languages

JML Example

public class IntMathOps { /*@ public normal_behavior

@ requires y >= 0;@ assignable \nothing;@ ensures 0 <= \result@ && \result * \result <= y@ && ((0 <= (\result + 1) * (\result + 1))@ ==> y < (\result + 1) * (\result + 1));@*/

public static int isqrt(int y){

return (int) Math.sqrt(y);}

}

Page 13: A Survey on Java Modeling Languages

JML Assert

Assert: Specifies a predicate that should hold at some point in the code:assert <predicate>

Example:for(n=0; n<a.length; n++)

if (a[n] == null) break;//@ assert(\forall int i; 0<=i && i<n; a[i]!=null);

Page 14: A Survey on Java Modeling Languages

JML Tools

ESC/Java2 does static analysisIowa State’s JML release:

jml BankAccount.java jmlc BankAccount.java

jmlrac BankAccount jmldoc BankAccount.java

OpenJMLJMLEclipse

Page 15: A Survey on Java Modeling Languages

More JML tools

http://www.eecs.ucf.edu/~leavens/JML/

Page 16: A Survey on Java Modeling Languages

Contract4J

Based on Aspect4J, Aspect Oriented Programming

Page 17: A Survey on Java Modeling Languages

Contract4J

For each class and interface with a contract use: @Contract Precondition:

@Pre(<predicate>) Postcondition:

@Post(<predicate>) Invariant:

@Invar(<predicate>) Predicate:

Java literals method calls, field references Java operators $this, $return, $old, $args[n] etc.

Page 18: A Survey on Java Modeling Languages

Contract4J Example

@Contract@Invar("$this.balance > = 0.0")interface BankAccount {

@Post("$return >= 0.0")float getBalance(); @Pre("amount >= 0.0")@Post("$this.balance == $old($this.balance)+amount && $return == $this.balance")float deposit(float amount);

}

Page 19: A Survey on Java Modeling Languages

Contract4J Tools

Contract4J for Eclipse

Page 20: A Survey on Java Modeling Languages

Goal

Semantical constraint – Assertion

Syntactical constraintoutside Java

Syntactical constraintinside Java

Motivation: Design Patterns &OODesign Principles

Page 21: A Survey on Java Modeling Languages

An OO Design Principle from GOF

Program to an interface,not an implementation.

This means in practice the following:A client of a class may know only the interface of its abstract ancestor.

,i.e., if the class has more services the client cannot call them, i.e., the ancestor is should be closed.

Page 22: A Survey on Java Modeling Languages

Closed class in Contract4J

New constraint: closed class.Def: A child of a closed class may not

have public methods.

Page 23: A Survey on Java Modeling Languages

Closed class in Contract4J

@Invar("!Tool.hasPublicMethod($this.getClass())")public abstract class

Decorator<R, A> implements IComponent<R, A> {...}…class Tool{

public static boolean hasPublicMethod(Class c){Method[] ms = c.getDeclaredMethods();for(Method m : ms){

int modifiers = m.getModifiers();if (Modifier.isPublic(modifiers)) return true;

}return false;

}}

Page 24: A Survey on Java Modeling Languages

Goal

Semantical constraint – Assertion

Syntactical constraintoutside Java

Syntactical constraintinside Java

Motivation: Design Patterns &OODesign Principles

Page 25: A Survey on Java Modeling Languages

Other Java Meta Languages

Modern Jass, is a design by contract tool that works with the latest versions of Java and is closely related to JML.

Page 26: A Survey on Java Modeling Languages

Modern Jass

@Invariant - define an invariant.@Pre/@Post - Pre/Post-conditions@SpecCase - A full method specification @Also - A container for multiple

specifications@NonNull - A flyweight annotation to

state that a reference is not null

Page 27: A Survey on Java Modeling Languages

Modern Jass

1: package foo;

2: import jass.modern.*;

3: public class Bar {

4: @Pre("args.length % 2 == 0")

5: public static void main(String[] args){

6: System.out.println("Hello,„ + args.length);

7: } }

Page 28: A Survey on Java Modeling Languages

Modern Jass

Compiling / Validating contracts:javac -cp .:jass.modern.core-

20070519.jar foo/Bar.javaRunning:java -javaagent:jass.modern.core-

20070519.jar foo.Bar

Page 29: A Survey on Java Modeling Languages

Thank you for your attention!

The side of our joint project:www.risc.uni-linz.ac.at/projects/syntactical