250 likes | 314 Views
This presentation explores the problem of semantic assumptions in Java interfaces and introduces the concept of constraints to ensure semantic consistency. It discusses preconditions, postconditions, and invariants in design, utilizing examples and comparisons with Eiffel. Solutions include manual implementation, using the Java Proxy API, and specifying constraints with naming conventions. Examples illustrate constraint handling in Java interfaces and implementations. Constraint failure results in exceptions. Learn more in the current issue or ask questions.
E N D
Constraints in Java Markus Völter, MATHEMA AG
The Problem • Goal: Separation of Responsibilities • Server class • Client class • Server class has well defined interface for use by clients • Includes operations, each operations consists of name and parameters • However, interfaces contain no semantic information (except for types, in some languages)
Examples for the problem • Assumptions about parameters: • Problem: The assumption is not visible in the interface • Use of Java Interfaces does not work !!! • Implementation can forget to check it • Therefore: Client cannot rely on it
Examples for the problem II • Semantic consistency in subclasses: • The following sublass-overriding is legal:
Examples for the problem IIb • But what about this one: • Problem: A semantic assumption of the super class is broken. • It can be argued that this should not be allowed.
The problem: conclusion • Why does the client assume certain semantics although they are not formally ensured? • It is ok to assume these semantics. • Then we need a way to ensure them. This is the goal of this presentation!
Solution (conceptual): Constraints • Preconditions: • Scope: Operation • Must be true before operation is executed • Postconditions • Scope: Operation • Must be true after the operation has been executed • Invariants: • Scope: Class • Must be true at any time
Constraints in Design (UML) • OCL (Object Constraint Language) can be used to do it!
Constraints in Eiffel • Eiffel provides Constraints: Programming by Contract • They can be specified on class interfaces • They have different names: • Preconditions: require clause • Postconditions: ensure clause • Invariants are also possible, also on interfaces
Constraints in Eiffel II • Eiffel also allows correct constraints in subclasses: • Require else • Ensure then
Semantic of Constraints • Preconditions: • Server requires them to be true • Client must ensure this • Runtime system checks it • Server implementation expects them to be true • Postconditions: • Server assures them to be true • Server implementation must ensure that • Runtime system checks it • Client expects them to be true
Constraints in Java • Either do it manually (ifs at the beginning of each method) • Use Precompiler and declarative statements • Use Aspects (a kind of precompiler, in some way) • Or, use the following approach...
The Java Proxy API (JDK 1.3) • GoF Proxy pattern • Proxy API can dynamically create Proxies for any class in a system (at runtime!) • Forwards any method invocation to an InvocationHandler
Specifying Constraints • For an interface X, the constraints are specified in a class Xconstraints • Naming conventions exist for methods: • For Operation{visibility} {RetType} op(params)precondition is called public void pre_op(params ) • For Operation {visibility} {RetType} op(params)postcondition is called public void post_op(Object retVal ) • The invariant is checked in invariant()
An Example (Interface) • The following is a simple Interface for vehicles:
An Example (Implementation) • The following is a trivial implementation of the interface:
An Example (Constraints) • Precondition: In accelerate(), delta must be > 0 • Invariant: Truck must never drive faster than 80 km/h
An Example (Constraints II) • Postcondition: After decelerate, speed must be less than before:
A small disadvantage • To allow Java to create the Proxies, a factory must be used to create instances of classes, of which the constraints should be checked:
A small disadvantage II • Client application needs to use this factory: • Constraint checking can turned on or off easily
Constraint failure • If constraint fails, an Exception is thrown:
Want to know more? Check out the current issue of Or ask questions! Thank you!