1 / 25

Constraints in Java

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

bruceg
Télécharger la présentation

Constraints in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constraints in Java Markus Völter, MATHEMA AG

  2. 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)

  3. 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

  4. Examples for the problem II • Semantic consistency in subclasses: • The following sublass-overriding is legal:

  5. 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.

  6. 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!

  7. 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

  8. Constraints in Design (UML) • OCL (Object Constraint Language) can be used to do it!

  9. 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

  10. Constraints in Eiffel II • Eiffel also allows correct constraints in subclasses: • Require else • Ensure then

  11. 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

  12. 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...

  13. 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

  14. Using the API for Constraints

  15. Handle the invocations

  16. Handle the invocations

  17. 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()

  18. An Example (Interface) • The following is a simple Interface for vehicles:

  19. An Example (Implementation) • The following is a trivial implementation of the interface:

  20. An Example (Constraints) • Precondition: In accelerate(), delta must be > 0 • Invariant: Truck must never drive faster than 80 km/h

  21. An Example (Constraints II) • Postcondition: After decelerate, speed must be less than before:

  22. 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:

  23. A small disadvantage II • Client application needs to use this factory: • Constraint checking can turned on or off easily

  24. Constraint failure • If constraint fails, an Exception is thrown:

  25. Want to know more? Check out the current issue of Or ask questions! Thank you!

More Related