list of dots University of Ottawa logo / Université d'Ottawa

User Manual    [Previous]   [Next]   

Before and After Statements

You can request that certain code be run before or after various Umple-defined actions on attributes, associations, and the components of state machines as well as on user-defined methods.

Using 'before' allows you to enforce preconditions, such that the attribute or association will not be set if the precondition is not true. To reject setting a value, add 'return false'.

Code in a setMethod can access the value to be set by prefacing it by 'a' as in the second example or 'an'.

You can add before and after clauses to add code to generated methods of the form getX, setX, addX, removeX, getXs (to get all elements of an association), numberOfXs, indexOfX, where X is the name of the attribute or association. You can also add code before and after the constructor.

See also this example of using aspect-orientation to help interface Umple with existing libraries.

Example with "after" statement

class X
  after setA {
     setWhenWasASet(getA() + System.currentTimeMillis());

Load the above code into UmpleOnline


Example with "before" statement

class Person {
  before setName {
    if (aName != null && aName.length() > 20) { return false; }

Load the above code into UmpleOnline


Example with both "before" and "after" statements

class Operation {
  const Boolean DEBUG=true;
  before constructor {
    if (aQuery == null)
      throw new RuntimeException("Please provide a valid query");
  after constructor {
    if (DEBUG) { System.out.println("Created " + query); }

Load the above code into UmpleOnline



// Aspect oriented code injection: BeforeandAfterStatements
codeInjection- : [[beforeCode]] | [[afterCode]]

beforeCode : ( before | [=around] ) [[aspectBody]]

afterCode : after [[aspectBody]]